1 of 54

Code

http://arduino.cc/en/Reference/HomePage

2 of 54

The Arduino Environment

3 of 54

Board Type

4 of 54

Serial Port / COM Port

5 of 54

The Environment

6 of 54

Parts of the Sketch

7 of 54

Comments

    • Comments can be anywhere

8 of 54

Comments

    • Comments can be anywhere
    • Comments created with // or /* and */

9 of 54

Comments

    • Comments can be anywhere
    • Comments created with // or /* and */
    • Comments do not affect code

10 of 54

Comments

    • Comments can be anywhere
    • Comments created with // or /* and */
    • Comments do not affect code
    • You may not need comments, but think about the community!

11 of 54

Operators

The equals sign

= is used to assign a value

== is used to compare values

12 of 54

Operators

And & Or

&& is “and”

|| is “or”

13 of 54

Variables

Basic variable types:

Boolean

Integer

Character

14 of 54

Declaring Variables

Boolean: boolean variableName;

15 of 54

Declaring Variables

Boolean: boolean variableName;

Integer: int variableName;

16 of 54

Declaring Variables

Boolean: boolean variableName;

Integer: int variableName;

Character: char variableName;

17 of 54

Declaring Variables

Boolean: boolean variableName;

Integer: int variableName;

Character: char variableName;

String: stringName [ ];

18 of 54

Assigning Variables

Boolean: variableName = true;

or variableName = false;

19 of 54

Assigning Variables

Boolean: variableName = true;

or variableName = false;

Integer: variableName = 32767;

or variableName = -32768;

20 of 54

Assigning Variables

Boolean: variableName = true;

or variableName = false;

Integer: variableName = 32767;

or variableName = -32768;

Character: variableName = ‘A’;

or stringName = “SparkFun”;

21 of 54

Variable Scope�Where you declare your variables matters

22 of 54

Setup�void setup ( ) { }

The setup function comes before

the loop function and is necessary

for all Arduino sketches

23 of 54

Setup�void setup ( ) { }

The setup header will never change,

everything else that occurs in setup

happens inside the curly brackets

24 of 54

Setup�void setup ( ) { �pinMode (13, OUTPUT); }

Outputs are declare in setup, this is done by using the pinMode function

This particular example declares digital pin # 13 as an output, remember to use CAPS

25 of 54

Setup�void setup ( ) { Serial.begin;}

Serial communication also begins in setup

This particular example declares Serial communication at a baud rate of 9600. More on Serial later...

26 of 54

Setup, Internal Pullup Resistors�void setup ( ) { �digitalWrite (12, HIGH); }

You can also create internal pullup resistors in setup, to do so digitalWrite the pin HIGH

This takes the place of the pullup resistors currently on your circuit 7 buttons

27 of 54

Setup, Interrupts�void setup ( ) { �attachInterrupt (interrupt, function, mode) }

You can designate an interrupt function to Arduino pins # 2 and 3

This is a way around the linear processing of Arduino

28 of 54

Setup, Interrupts�void setup ( ) { �attachInterrupt (interrupt, function, mode) }

Interrupt: the number of the interrupt, 0 or 1, corresponding to Arduino pins # 2 and 3 respectively

Function: the function to call when the interrupt occurs

Mode: defines when the interrupt should be triggered

29 of 54

Setup, Interrupts�void setup ( ) { �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

30 of 54

If Statements

if ( this is true ) { do this; }

31 of 54

If

if ( this is true ) { do this; }

32 of 54

Conditional

if ( this is true ) { do this; }

33 of 54

Action

if ( this is true ) { do this; }

34 of 54

Else

else { do this; }

35 of 54

Basic Repetition

  • loop

  • For

  • while

36 of 54

Basic Repetition

void loop ( ) { }

37 of 54

Basic Repetition

void loop ( ) { }

38 of 54

Basic Repetition

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

39 of 54

Basic Repetition

void loop ( ) { }

The “loop” in the header is what the function is called, sometimes you make the name up, sometimes (like loop) the function already has a name

40 of 54

Basic Repetition

void loop ( ) { }

The “( )” in the header is where you declare any variables that you are “passing” (or sending) the function, the loop function is never “passed” any variables

41 of 54

Basic Repetition

void loop ( ) { }

42 of 54

Basic Repetition

for (int count = 0; count<10; count++)

{

//for action code goes here

//this could be anything

}

43 of 54

Basic Repetition

for (int count = 0; count<10; count++)

{

//for action code goes here

}

44 of 54

Basic Repetition

for (int count = 0; count<10; count++)

{

//for action code goes here

}

45 of 54

Basic Repetition

for (int count = 0; count<10; count++)

{

//for action code goes here

}

46 of 54

Basic Repetition

for (int count = 0; count<10; count++)

{

//for action code goes here

}

47 of 54

Basic Repetition

for (int count = 0; count<10; count++)

{

//for action code goes here

}

48 of 54

Basic Repetition

for (int count = 0; count<10; count++)

{

//for action code goes here

}

49 of 54

Basic Repetition

while ( count<10 )

{

//while action code goes here

}

50 of 54

Basic Repetition

while ( count<10 )

{

//while action code goes here

//should include a way to change count

//variable so the computer is not stuck

//inside the while loop forever

}

51 of 54

Basic Repetition

while ( count<10 )

{

//looks basically like a “for” loop

//except the variable is declared before

//and incremented inside the while

//loop

}

52 of 54

Basic Repetition

Or maybe:

while ( digitalRead(buttonPin)==1 )

{

//instead of changing a variable

//you just read a pin so the computer

//exits when you press a button

//or a sensor is tripped

}

53 of 54

Questions?

54 of 54

www.sparkfun.com

6175 Longbow Drive, Suite 200

Boulder, Colorado 80301