Arduino Coding Guide

From EG1004 Lab Manual
Revision as of 17:16, 6 February 2020 by Dmulay (talk | contribs) (→‎Functions)
Jump to: navigation, search

Arduino Coding Guide

Arduino Programming

The Arduino programming language is based on C/C++, but it is designed to be simpler and easier to learn. The most intuitive way to think about programming is like building with LEGO blocks: certain rules must be followed and different building blocks can be used to build bigger parts.

General Syntax

  • Every line must either end with a semicolon ‘;’ unless it’s a conditional, loop, or function
  • Conditionals, loops, and functions are enclosed in curly braces {}
  • Comments start with a //
    • Comments are text that the program ignores
    • Used to label and explain code

Datatypes

Datatypes are the different kinds of data values that can be used, manipulated and stored using C++. The table below includes the most basic and widely used datatypes.

Operators

Operators perform operations on variables and constants. The results of these operations are usually stored in a variable. The table below includes common operators.

Constants and Variables

Constants and variables hold data according to their datatype. They need to be given a name so that they can be referred to later. Constants hold data that will NOT change while a program is running. Constants usually contain pin numbers or sensor threshold values. Variables contain data that WILL change while a program is running. Variables usually contain sensor values and other values that need to have mathematical operations done on them. Below is an example of how to create different constants and variables.

Figure 1: Constants & Variables

Conditional Statements

Conditional statements run code enclosed by their curly brackets when a condition is met.

Figure 2: Conditional statements

Loops

Loops run the code enclosed by their curly brackets a specific amount of times or until a condition is met. While-loop While-loops are used to perform a task until a condition is met For-loop For-loops are used when you want something to run a specific number of times. Although they seem complicated at first, the structure of most for-loops is the same. In the parentheses, the first part sets a variable (usually ‘i’ for ‘index’) to a value used to begin a count, the middle is the condition when the loop stops, and the third part is where you increment or decrement the counting variable.

Figure 3: While and for-loops

Functions

Functions are predefined sections of code that can be called multiple times to perform a defined task. These are typically used when a programmer needs to perform the same task multiple times. Figure 4 shows the anatomy of a function.

Figure 4: Function Prototype

The header portion is the function declaration. This tells the code what the function will return (return type), what the name of the function is (function name), and any parameters needed.The return type can be any valid Arduino C data type. If the function will not return any values, the return type should be void. Parameters are optional when creating a function.

For example, the following function is called addOne. This function takes in an integer, and returns that integer value plus one.

Figure 5: addOne() Function


An example use of this function is below. After calling the function addOne(4), the value of the integer number would be 5.

Figure 6: Using the addOne() function

Commonly Used Arduino Functions

These functions are included with Arduino C to be used with the Arduino board.

picture

Arduino Libraries

Arduino Libraries are commonly used for robot movement and can be helpful with various sensors. Libraries are files that are already written in order to provide your sketch with extra functionality through various functions.

To use an existing library in a sketch simply go to the Sketch menu, choose "Import Library", and pick from the libraries available. This will insert an #include statement at the top of the sketch for each header (.h) file in the library's folder. These statements make the public functions and constants defined by the library available to your sketch. They also signal the Arduino environment to link that library's code with your sketch when it is compiled or uploaded.

To install a third party library download the libraries zip file to the Arduino folder of your computer. Extract the library to its own folder. Arduino must be restarted in order to use the library.

Additional Resources

These are some third party resources and tutorials with additional information on working with Arduino: