Arduino Coding Guide

From EG1004 Lab Manual
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.

Datatype What it stores (examples) Default value Notes
Boolean A true value (1, TRUE, HIGH) or
a false value (0, FALSE, LOW)
0, FALSE, LOW English boolean values such as TRUE or LOW must be in all caps
int An integer number (-5, 15, 1047, etc.) 0 Can be positive or negative
double A decimal number (-0.5, 123.77, etc.) 0 Can be positive or negative
float A decimal number (-0.5, 123.77, etc.) 0 Can be positive or negative
char A single character (‘c’, ‘A’, ‘5’, ‘?’, etc.) Indeterminate Must be enclosed in single quotes
string A sequence of characters (“Hello World!”,
“10”, “157+5”, etc.)
Empty (“”) Must be enclosed in double quotes

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.

Operator What it does Notes
= Assigns a value to a variable
+ Adds two or more values
- Subtracts two or more values
* Multiplies two or more values
/ Divides two or more values
++ Increment by 1 Usually used in loops
-- Decrement by 1 Usually used in loops
== Checks if two value are equal Usually used in conditionals
!= Checks if two value are not equal Usually used in conditionals
> or < Less than/Greater than comparison Usually used in conditionals
<= or >= Less than/greater than or equal to comparison Usually used in conditionals
&& or || Boolean AND or Boolean OR Used to cascade multiple Boolean operations Usually used in conditionals

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.

Function What it does
pinMode(pin,mode) Sets a pin as an input or output
digitalWrite(pin, value) Sets a digital output pin to HIGH or LOW
digitalRead(pin) Reads a digital input pin as HIGH or LOW
analogWrite(pin, value) Sets an analog output pin to a value 0-1023
analogRead(pin) Reads an analog output pin as a value 0-1023
delay(milliseconds) Pauses the program for a certain amount of time
Serial.begin(value) Begins the Serial Monitor with a baud rate of value
Serial.print(value) Prints the value (variable) to the Serial Monitor.

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: