Arduino Software
Arduino Software
Arduino Software
Example
int buttonPin = 3;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop()
{
// ...
}
Reference Home
Corrections, suggestions, and new documentation should be posted to
the Forum.
The text of the Arduino reference is licensed under a Creative Commons
Attribution-ShareAlike 3.0 License. Code samples in the reference are
released into the public domain.
1
loop()
After creating a setup() function, which initializes and sets the initial values,
the loop() function does precisely what its name suggests, and loops
consecutively, allowing your program to change and respond. Use it to
actively control the Arduino board.
Example
Reference Home
Corrections, suggestions, and new documentation should be posted to
the Forum.
2
The text of the Arduino reference is licensed under a Creative Commons
Attribution-ShareAlike 3.0 License. Code samples in the reference are
released into the public domain.
certain condition has been reached, such as an input being above a certain
number. The format for an if test is:
if (someVariable > 50)
{
// do something here
}
The program tests to see if someVariable is greater than 50. If it is, the
program takes a particular action. Put another way, if the statement in
parentheses is true, the statements inside the brackets are run. If not, the
program skips over the code.
The brackets may be omitted after an if statement. If this is done, the next
line (defined by the semicolon) becomes the only conditional statement.
if (x > 120)
digitalWrite(LEDpin, HIGH);
if (x > 120){
digitalWrite(LEDpin1, HIGH);
digitalWrite(LEDpin2, HIGH);
} // all are correct
The statements being evaluated inside the parentheses require the use of
one or more operators:
3
Comparison Operators:
x == y (x is equal to y)
x != y (x is not equal to y)
x < y (x is less than y)
x > y (x is greater than y)
x <= y (x is less than or equal to y)
x >= y (x is greater than or equal to y)
Warning:
Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The
single equal sign is the assignment operator, and sets x to 10 (puts the value
10 into the variable x). Instead use the double equal sign (e.g. if (x ==
10) ), which is the comparison operator, and tests whether x is equal to 10
or not. The latter statement is only true if x equals 10, but the former
statement will always be true.
This is because C evaluates the statement if (x=10) as follows: 10 is
desired result when using an 'if' statement. Additionally, the variable x will
be set to 10, which is also not a desired action.
if can also be part of a branching control structure using the if...else]
construction.
Reference Home
Corrections, suggestions, and new documentation should be posted to
the Forum.
4
The text of the Arduino reference is licensed under a Creative Commons
Attribution-ShareAlike 3.0 License. Code samples in the reference are
released into the public domain.
if / else
if/else allows greater control over the flow of code than the
basic if statement, by allowing multiple tests to be grouped together. For
example, an analog input could be tested and one action taken if the input
was less than 500, and another action taken if the input was 500 or greater.
The code would look like this:
if (pinFiveInput < 500)
{
// action A
}
else
{
// action B
}
else can proceed another if test, so that multiple, mutually exclusive tests
can be run at the same time.
Each test will proceed to the next one until a true test is encountered. When
a true test is found, its associated block of code is run, and the program then
skips to the line following the entire if/else construction. If no test proves to
be true, the default else block is executed, if one is present, and sets the
default behavior.
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.
if (pinFiveInput < 500)
{
// do Thing A
}
5
else if (pinFiveInput >= 1000)
{
// do Thing B
}
else
{
// do Thing C
}
See also:
for statements
Description
6
The initialization happens first and exactly once. Each time through the loop,
the condition is tested; if it's true, the statement block, and the increment is
executed, then the condition is tested again. When the condition becomes
false, the loop ends.
Example
// Dim an LED using a PWM pin
int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10
void setup()
{
// no setup needed
}
void loop()
{
for (int i=0; i <= 255; i++){
analogWrite(PWMpin, i);
delay(10);
}
}
7
Coding Tips
The C for loop is much more flexible than for loops found in some other
computer languages, including BASIC. Any or all of the three header
elements may be omitted, although the semicolons are required. Also the
statements for initialization, condition, and increment can be any valid C
statements with unrelated variables, and use any C datatypes including
floats. These types of unusual for statements may provide solutions to some
rare programming problems.
For example, using a multiplication in the increment line will generate a
logarithmic progression:
for(int x = 2; x < 100; x = x * 1.5){
println(x);
}
Generates: 2,3,4,6,9,13,19,28,42,63,94
Another example, fade an LED up and down with one for loop:
void loop()
{
int x = 1;
for (int i = 0; i > -1; i = i + x){
analogWrite(PWMpin, i);
if (i == 255) x = -1; // switch direction at peak
delay(10);
}
}
See also
while
Reference Home
Corrections, suggestions, and new documentation should be posted to
the Forum.
8
The text of the Arduino reference is licensed under a Creative Commons
Attribution-ShareAlike 3.0 License. Code samples in the reference are
released into the public domain.
Example
switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
//do something when var equals 2
break;
default:
// if nothing else matches, do the default
// default is optional
}
9
Syntax
switch (var) {
case label:
// statements
break;
case label:
// statements
break;
default:
// statements
}
Parameters
See also:
while loops
10
Des c ri p tion
while loops will loop continuously, and infinitely, until the expression inside
the parenthesis, () becomes false. Something must change the tested
variable, or the while loop will never exit. This could be in your code, such
as an incremented variable, or an external condition, such as testing a
sensor.
S y n ta x
while(expression){
// statement(s)
}
Pa ra meters
expression - a (boolean) C statement that evaluates to true or false
E x amp l e
var = 0;
while(var < 200){
// do something repetitive 200 times
var++;
}
Reference Home
Corrections, suggestions, and new documentation should be posted to
the Forum.
The text of the Arduino reference is licensed under a Creative Commons
Attribution-ShareAlike 3.0 License. Code samples in the reference are
released into the public domain.
do - while
The do loop works in the same manner as the while loop, with the exception that the
condition is tested at the end of the loop, so the do loop will always run at least once.
do
{
// statement block
} while (test condition);
Example
do
{
11
delay(50); // wait for sensors to stabilize
x = readSensors(); // check the sensors
Reference Home
Corrections, suggestions, and new documentation should be posted to the Forum.
The text of the Arduino reference is licensed under a Creative Commons Attribution-
ShareAlike 3.0 License. Code samples in the reference are released into the public
domain.
break
break is used to exit from a do, for, or while loop, bypassing the normal
loop condition. It is also used to exit from aswitch statement.
E x amp l e
for (x = 0; x < 255; x ++)
{
digitalWrite(PWMpin, x);
sens = analogRead(sensorPin);
if (sens > threshold){ // bail out on sensor detect
x = 0;
break;
}
delay(50);
}
Reference Home
Corrections, suggestions, and new documentation should be posted to
the Forum.
The text of the Arduino reference is licensed under a Creative Commons
Attribution-ShareAlike 3.0 License. Code samples in the reference are
released into the public domain.
12
continue
The continue statement skips the rest of the current iteration of a loop
(do, for, or while). It continues by checking the conditional expression of
the loop, and proceeding with any subsequent iterations.
E x amp l e
digitalWrite(PWMpin, x);
delay(50);
}
Reference Home
Corrections, suggestions, and new documentation should be posted to
the Forum.
The text of the Arduino reference is licensed under a Creative Commons
Attribution-ShareAlike 3.0 License. Code samples in the reference are
released into the public domain.
return
13
E x amp l es:
A function to compare a sensor input to a threshold
int checkSensor(){
if (analogRead(0) > 400) {
return 1;
else{
return 0;
}
}
The return keyword is handy to test a section of code without having to
"comment out" large sections of possibly buggy code.
void loop(){
return;
goto
14
E x amp l e
for(byte r = 0; r < 255; r++){
for(byte g = 255; g > -1; g--){
for (byte b = 0; b < 255; b++){
if (analogRead(0) > 250){ goto bailout;}
// more statements ...
}
}
}
bailout:
15