Landing : Athabascau University

Arduino IDE Code Reference: Revision

Last updated April 14, 2019 - 5:58pm by Darren Hurren

This Wiki page will list some of the Arduino Integrated Development Environment (IDE) software's functions and code examples for reference. Most of this information has been copied from the Sparkfun inventors Package (SIK) or online resources. Some useful links will be included below.



Arduino IDE Code Reference:

void setup(){code to run at start} & void loop(){code to after in a continuous loop}

Every Arduino program needs these two functions. Code that goes in between the curly brackets {} of setup() runs once. The code in between the loop()curly brackets {} runs over and over until the RedBoard is reset or powered off.

Functions:

void function_name(){}

This is a definition of a simple function. When programmers want to use many lines of code over and over again, they write a function. The code inside the curly brackets “executes” whenever the function is “called” in the main program.

function_name();

This line calls a function by that you have earlier created in: void function_name() { }. Functions are used for when you need to call the functions multiple times so it can reuse the code that was written inside the curly brackets of that function.

Comments: //this is a commented line OR /*this is a commented paragraph*/

pinMode(13, OUTPUT);

In order to use one of the digital pins, you need to tell the Arduino whether it is an INPUT or OUTPUT. The example above is a built-in “function” called pinMode() where you first specify the pin number and then hot it will function (INPUt or OUTPUT). The example above makes pin 13 a digital output. You can also use one of the arduino's built-in internal 20kOhm as an INPUT. Use this by writing the function: pinMode(pin, INPUT_PULLUP);

digitalWrite(13, HIGH);

When you are using a digital pin as an OUTPUT, you can command it to be HIGH (output 5 volts) or LOW (output 0 volts). The example above sets pin 13 to output 5 volts.

digitalRead(pin);

This function checks to see if an input pin is reading HIGH (5V) or LOW (0V). Returns TRUE (1) or FALSE (0) depending on the reading.

analogWrite(9, 100);

Use this analogWrite function to set a output voltage between 0 and 5V in 255 increments, where 0 woud be 0V and 255 woud be 5V. You can only write (or output) to one of several PWM analog pins (3, 5, 6, 9, 10, 11) or you can output to a variable (ex. redPin if redPin = one of the analog pins). These pins are marked with a ~ on the arduino board. Only these pins can switch on and off fast enough to vary the voltage output between the two voltage extremes (0 and 5V in this case).

delay(2000);

Causes the program to wait on this line of code for the amount of time in between the brackets, represented in milliseconds (2000ms = 2s). After the time has passed, the program will continue to the next line of code.

Serial.begin(9600);

This is the Baud rate, which determines how fast the Arduino board will transmit data to, and receive data from the PC. This line of code tells the arduino board to start a serial communication with the computer. The Baud rate that you set in the code (ex. 9600 is shown in the example above) must match in the serial monitor in the Arduino IDE in order for you to properly view the incoming data.

tone(pin, frequency, duration);

The tone() function will output a specific frequency (in a PWM square wave) to a specific pin for a specified duration (in seconds?) (Note: a duration is not required, but if none is specified the square wave/tone will output contnuously until the function noTone() is called). The SIK says that tone() can be used on any PWM capable digital pin (marked with ~).

millis();

Use the internal built-in clock in the Aruino board to see how many milliseconds have passed since the Arduino was powered on.

break;

...

return;

...

randomSeed;

...

random;

...


 LOOPS:

while(TRUE){}

A while loop will loop continuously until the expression inside the parentheses () becomes false. The expression is a boolean statement that evaluates to TRUE or FALSE. Something must change the tested variable or the while loop will never exit. Example below:

var = 0;
while(var < 200){
  // do something repetitive 200 times
  var++;
}

for (int i = 0; i < 5; i++){};

This is an example of a for loop, which repeats a section of code a number of times using a counter that increases each loop iteration until it reaches a stop value. The for loop has three parameters: The first parameter is a start value. In this case i starts at 0. The second parameter is the stop condition. In this case the loop stops when i is no longer less than 5 (i < 5 is no longer true). The third parameter is an increment value. i++ is shorthand for increase i by 1 each time. You can also increment the value by different amounts. This would complete the loop 5 times.


 IF ELSE Statements:

if(logic statement using logical operators) {code to run if statement is true. Write nested if statements in here with their own curly brackets{}}

else {code to run if statement is false}

These IF ELSE statements work by first determining if the first IF logic statement (in the round brackets) is true and if that isn't true it will then run the code written beside the ELSE statement. You can use nested IF statements for even more options. In a nested IF statement, if the very first IF is NOT TRUE, then it jumps to the ELSE statement. You can also use ELSE IF statements to add another test condition. Where nested IF statements will automatically be skipped if the first IF statement is false, the ELSE IF statements will be tested after the IF statement is FALSE. If the ELSE IF statements are also FALSE the code will go to the next ELSE IF or ELSE statement (Note: that an else if block may be used with or without a terminating else block and vice versa. An unlimited number of such else if branches is allowed).

Refer to example Arduino IDE code: SIK_CIRCUIT_1D-RGB NIGHT LIGHT for examples on how to use nested IF statements, SIK_CIRCUIT_2B-DigitalTrumpet for examples on how to use ELSE IF statements.

Example Code:

if (temperature >= 70) {
//Danger! Shut down the system
}
else if (temperature >= 60 && temperature < 70) {
//Warning! User attention required
}
else {
//Safe! Continue usual tasks...
}

 


Variables:

 int variable_name;

 Integer Variable: A variable is a placeholder for values that may change in you code. You must "declare" a variable at the beginning of the code (before "void setup()"  before you can use them. Variables are case sensitive and good practice is to have the first word lowercase and the second word start with an uppercase letter to easily identify it as a variable (ex. int photoResistor).

char variable_name;

The char variable stores character values. When used along with the int variable is can be used as a shortcut so that you are only writing, for example a letter instead of a longer string of text or numbers. Examples can be foundd on the Arduino IDE in the code example: SIK_Circuit_2A-Buzzer

boolean variable_name;

This variable type is based on boolean logic where there is only two possible values; 1 or 0 (a.k.a. HIGH or LOW, ON or OFF, TRUE or FALSE). Using this variable helps save memory on your microcontroller if you only need to know if something is either TRUE or FALSE. Space in the microconntrollerr is reserved for each variable that is declared in the code and this depends on the variable type. 

long variable_name;

...


  Logical Operators:

Logical operators are used to make a comparison between two different values. Those values can be taken from variables (i.e. variables used to capture photoresistor values), digitalRead(pin) or numerical values.  Some examples of logical operators are: == (is equal to? This actualy means: Are these two values equal? Note: a single = means assigning a particular value to a variable), > (greater than), < (less then), >= (greater then OR equal to), <= (less than OR equall to) and && (AND; this can be used to combine logical operators). The logical operator compares the number or variable on the left to the number or variable on the right side of the operator (ex. '400 >= photoResistor' would be TRUE if the photoResistor value was 400 or less and FALSE if the value of photoResistor was less than 400).


Arrays:

array_name[array_size];

An array in basically a way to store a list of values, variables or pin numbers. To declare an array, first give it a suitable name (array_name as shown in the example above). Next, write the size of / number of positions/elements in the array or assign a list of variables to the array. An array must contain all the same type of variables and be declared as such. An array index / number starts at 0, not 1.

To call one of the values in an array, type the name of the array followed by the index of the value: array_name[index_#); (ex. array_name[0]; will call the first element/index of the array: array_name. array_name[1]; will call the second element/index etc.) 

Storing a sequencce of pin numbers in an array (ex. int led[ ] = {3,5,7,9};) allows you touse a loop to easily cycle through each pin connected to the LED's. 

 

 
 

 

 

History