4/17/2019
The Arduino Environment
Code
http://arduino.cc/en/Reference/HomePage
Board Type Serial Port / COM Port
1
4/17/2019
The Environment Parts of the Sketch
Comments Comments
• Comments can be anywhere • Comments can be anywhere
• Comments created with // or /*
and */
2
4/17/2019
Comments Comments
• Comments can be anywhere • Comments can be anywhere
• Comments created with // or /* • Comments created with // or /*
and */ and */
• Comments do not affect code • Comments do not affect code
• You may not need comments,
but think about the community!
Operators Operators
The equals sign And & Or
= is used to assign a value && is “and”
== is used to compare values || is “or”
3
4/17/2019
Variables Declaring Variables
Basic variable types: Boolean: boolean variableName;
Boolean
Integer
Character
Declaring Variables Declaring Variables
Boolean: boolean variableName; Boolean: boolean variableName;
Integer: int variableName; Integer: int variableName;
Character: char variableName;
4
4/17/2019
Declaring Variables Assigning Variables
Boolean: boolean variableName; Boolean: variableName = true;
or variableName = false;
Integer: int variableName;
Character: char variableName;
String: stringName [ ];
Assigning Variables Assigning Variables
Boolean: variableName = true; Boolean: variableName = true;
or variableName = false; or variableName = false;
Integer: variableName = 32767; Integer: variableName = 32767;
or variableName = -32768; or variableName = -32768;
Character: variableName = ‘A’;
or stringName = “SparkFun”;
5
4/17/2019
Variable Scope Setup
Where you declare your variables matters void setup ( ) { }
The setup function comes before
the loop function and is necessary
for all Arduino sketches
Setup
Setup
void setup ( ) {
void setup ( ) { }
pinMode (13, OUTPUT); }
The setup header will never change, Outputs are declare in setup, this is
everything else that occurs in setup done by using the pinMode function
This particular example declares digital pin # 13 as an
happens inside the curly brackets output, remember to use CAPS
6
4/17/2019
Setup, Internal Pullup Resistors
Setup
void setup ( ) {
void setup ( ) { Serial.begin;}
digitalWrite (12, HIGH); }
Serial communication also begins in
setup You can also create internal pullup resistors in setup, to
This particular example declares Serial communication do so digitalWrite the pin HIGH
at a baud rate of 9600. More on Serial later... This takes the place of the pullup resistors currently on
your circuit 7 buttons
Setup, Interrupts Setup, Interrupts
void setup ( ) { void setup ( ) {
attachInterrupt (interrupt, function, attachInterrupt (interrupt, function,
mode) } mode) }
Interrupt: the number of the interrupt, 0
You can designate an interrupt or 1, corresponding to Arduino pins # 2
function to Arduino pins # 2 and 3 and 3 respectively
Function: the function to call when the
This is a way around the linear interrupt occurs
processing of Arduino
Mode: defines when the interrupt should
be triggered
7
4/17/2019
Setup, Interrupts If Statements
void setup ( ) {
if ( this is true ) { do this; }
attachInterrupt (interrupt, function,
mode) }
•LOW whenever pin state is low
•CHANGE whenever pin changes value
•RISING whenever pin goes from low to
high
•FALLING whenever pin goes from low to
high
Don’t forget to CAPITALIZE
If Conditional
if ( this is true ) { do this; } if ( this is true ) { do this; }
8
4/17/2019
Action Else
if ( this is true ) { do this; } else { do this; }
Basic Repetition Basic Repetition
void loop ( ) { }
• loop
• For
• while
9
4/17/2019
Basic Repetition Basic Repetition
void loop ( ) { } void loop ( ) { }
The “void” in the header is what
the function will return (or spit out)
when it happens, in this case it
returns nothing so it is void
Basic Repetition Basic Repetition
void loop ( ) { } void loop ( ) { }
The “loop” in the header is what the The “( )” in the header is where you
function is called, sometimes you make declare any variables that you are
the name up, sometimes (like loop) the “passing” (or sending) the function, the
function already has a name loop function is never “passed” any
variables
10
4/17/2019
Basic Repetition Basic Repetition
void loop ( ) { } for (int count = 0; count<10; count++)
{
//for action code goes here
//this could be anything
}
Basic Repetition Basic Repetition
for (int count = 0; count<10; count++) for (int count = 0; count<10; count++)
{ {
//for action code goes here //for action code goes here
} }
11
4/17/2019
Basic Repetition Basic Repetition
for (int count = 0; count<10; count++) for (int count = 0; count<10; count++)
{ {
//for action code goes here //for action code goes here
} }
Basic Repetition Basic Repetition
for (int count = 0; count<10; count++) for (int count = 0; count<10; count++)
{ {
//for action code goes here //for action code goes here
} }
12
4/17/2019
Basic Repetition Basic Repetition
while ( count<10 ) while ( count<10 )
{ {
//while action code goes here //while action code goes here
} //should include a way to change count
//variable so the computer is not stuck
//inside the while loop forever
}
Basic Repetition Basic Repetition
Or maybe:
while ( count<10 )
{ while ( digitalRead(buttonPin)==1 )
//looks basically like a “for” loop {
//except the variable is declared before //instead of changing a variable
//and incremented inside the while //you just read a pin so the computer
//loop //exits when you press a button
} //or a sensor is tripped
}
13
4/17/2019
Questions?
www.sparkfun.com
6175 Longbow Drive, Suite 200
Boulder, Colorado 80301
14