Arduinoprogrammingr1 150417185248 Conversion Gate02

Download as pdf or txt
Download as pdf or txt
You are on page 1of 76

Arduino Programming

(C) 2014 James Lewis


[email protected]

1
Arduino Langauge
C++ C++ C++
“Arduino”
Libs Libs Libs

C / C++
(Readable Code)

Assembly
(Readable Code)

Machine Language
(Binary Code)

2
Arduino Langauge
C++ C++ C++
“Arduino”
Libs Libs Libs

C / C++
(Readable Code)

Assembly
(Readable Code)

Machine Language
(Binary Code)

3
Hello World (Blink)

4
Hello World (Blink)

Variable

5
Hello World (Blink)

Comments

6
Hello World (Blink)

Comments

Good Comment:
// Blue LED for Activity Indicator

Bad Comment:
// Pin 13
7
Hello World (Blink)

Functions

8
Hello World (Blink)

Functions

9
Hello World (Blink)

Instruction

10
Hello World (Blink)

Instruction

11
Hello World (Blink)
Function Call

12
Hello World (Blink)
Arguments

13
Hello World (Blink)

14
IDE Tools Menu

15
IDE Tools Menu

16
IDE Tools Menu

17
IDE Tools Menu

18
Arduino IDE

Type Stuff Here

Compiler Output

19
Arduino IDE
Serial Monitor

Type Stuff Here

Compiler Output

20
Arduino IDE
Verify & Upload Serial Monitor

Type Stuff Here

Compiler Output

21
Arduino IDE
Verify & Upload Serial Monitor

Type Stuff Here

Compiler Output

Board & Serial Port


22
Blink Exercise

• Load the Blink Example and


program it to your board


• Change the values of delay()


to see how it affects the
behavior

Check the correct board and serial port are selected in the tools menu!
23
Hello World
Serial Example

24
Serial objects

25
Serial objects

Enables Serial

Baud
Rate

26
Serial objects

Print
and Println

Variables
Strings
Control Characters

27
Serial objects

Print
and Println

Variables
Strings
Control Characters

NOTE: Strings and Variables28Can’t be used on the same line


Hello World (Serial)
• Load up the serial code to the
right


• Exercise:

• Change the 2000 in delay


into a variable.

• Print value of variable on


same line as “Hello World”

29
Variables
30
How Much Memory is
in your Arduino?

31
Variable Types
Bits Unsigned Range Signed Range

byte 8 0 to 255 N/A

0 to 255
char 8 ‘A’..’b’..’X’
N/A

int 16 0 to 65535 -32,767 to 32,766

-2,147,483,648 to
long 32 0 to 4,294,967,295
2,147,483,647

float 32 ±3.4028235E+38 n/a

double 32 n/a n/a

32
Variable Do and Don’t
• DO Use Descriptive Names

• “BlueLED”, “ActivityIndicator”

• DON’T Use Bad Names

• “Integer”, “Pin13”

• DO Stick to a naming convention

• Variables are Case Sensitive!

• DON’T use same name for Local and Global Variables

33
Variable Scope

34
Variable Scope
Global

35
Variable Scope
Global

Local to loop()

36
Variable Don’t!

37
Variable Don’t!

38
Variable Don’t!

39
arrays
0 1 2 3 4 5

analogReadings[]

40
arrays
0 1 2 3 4 5

analogReadings[]

Size

Elements

41
arrays
0 1 2 3 4 5

analogReadings[]

Size arrays are 



0-index based. 

So last element

is always 

“1 less”

than the size!
Elements

42
Pin Functions
43
pinMode()
Analog Digital
(A0..A5) (0..13)

Digital Input, Digital Input,


INPUT
Pull-Up Off Pull-Up Off

Digital Input, Digital Input,


INPUT_PULLUP
Pull-Up On Pull-Up On

OUTPUT Digital Output Digital Output

Analog Pins can be used as Digital Pins


pinMode(INPUT, Ax) isn’t necessary for analogRead()
44
digitalRead() & digitalWrite()

45
Pull-Up Resistor

INPUTs almost always need a Pull-Up or Pull-Down


pinMode(INPUT_PULLUP) Turns on the Internal Pull-Up Resistor

http://www.baldengineer.com/tutorials/arduino-pull-ups/
46
I/O Exercise

• Objective: Understand Inputs


and Outputs


• Exercise: “Fix” the random


LED

47
analogRead()
Analog Digital
Convert
Signal Value

Voltage 10-bits, stored as 8-bits

0v to 5v Values 0 to 1023

5 volts
= 4.887mV per Step
1023 Steps

Calling analogRead() on an Analog Pin,


automatically converts to Input
48
analogWrite()

Pulse Width Modulation (PWM) Actual Analog

analogWrite() isn’t Analog (Except on the Due)


Uno Pins: 493, 5, 6, 9, 10, 11
Analog Exercise

• Connect Pin 6 to Analog 0



Run this code


• Then, disconnect from Pin 6 



While Running the Code

Remember to open the Serial Monitor! 50


Analog Exercise

• Connect Pin 6 to Analog 0



Run this code


• Then, disconnect from Pin 6 



While Running the Code

Remember to open the Serial Monitor! 51


A0, A1, A2..A5 are integers!
functions()
52
Functions
Getting Data Back
Functions
Getting Data Back

Tip: The Arduino IDE, doesn’t require “prototyping”


Functions
Getting Data Back

Arguments
Function Name
Return Type

“Return”
Functions
Returning Nothing

Return Type

If the function doesn’t return anything, declare it as void

56
Function Exercise

• “Re-Write” the built-in Blink


Example to use a Function

• Exercise: add a “argument” to


adjust the delay time

57
Control Structures
58
if-statements

59
if-statements

60
if-statements

61
if-statements

62
control operators
== Equal to || OR

>
 Greater than


&& AND
>= (or equal)

< Less Than


| Bitwise OR
<= (or equal)

!= Not Equal to & Bitwise AND

63
#1 if-statement mistake

64
#1 if-statement mistake

= != ==
65
for() loop example

66
for() loop

67
for() loop

Control Variable

68
for() loop

Control Variable

Condition

69
for() loop
Increment

Control Variable

Condition

70
Array and For Exercise

• Use an Array and two for-


loops to read analog inputs,
then display then


• Notice the difference in


brackets between the two
loops

71
while() loop

72
while() loop
Condition

73
while() loop
Condition

Loop conditions are same as “if conditions”


74
while() and Serial

• This program echoes


whatever is on the serial buffer
back out

75
More information?


Visit


baldengineer.com

76

You might also like