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.
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() to make pin 13 a digital output.
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.
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)
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. Refer to example Arduino IDE code: SIK_CIRCUIT_1D-RGB NIGHT LIGHT for examples on how to use nested IF statements.
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
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) or numerical values. Some examples of logical operators are: == (equal to), > (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).
The Landing is a social site for Athabasca University staff, students and invited guests. It is a space where they can share, communicate and connect with anyone or everyone.
Unless you are logged in, you will only be able to see the fraction of posts on the site that have been made public. Right now you are not logged in.
If you have an Athabasca University login ID, use your standard username and password to access this site.
We welcome comments on public posts from members of the public. Please note, however, that all comments made on public posts must be moderated by their owners before they become visible on the site. The owner of the post (and no one else) has to do that.
If you want the full range of features and you have a login ID, log in using the links at the top of the page or at https://landing.athabascau.ca/login (logins are secure and encrypted)
Posts made here are the responsibility of their owners and may not reflect the views of Athabasca University.