76
Arduino Programming (C) 2014 James Lewis [email protected] 1

Introduction to Arduino Programming

Embed Size (px)

Citation preview

Page 1: Introduction to Arduino Programming

Arduino Programming(C) 2014 James Lewis

[email protected]

1

Page 2: Introduction to Arduino Programming

Arduino Langauge

2

Machine Language (Binary Code)

Assembly (Readable Code)

C / C++ (Readable Code)

C++ Libs

C++ Libs

C++ Libs“Arduino”

Page 3: Introduction to Arduino Programming

Arduino Langauge

3

Machine Language (Binary Code)

Assembly (Readable Code)

C / C++ (Readable Code)

C++ Libs

C++ Libs

C++ Libs“Arduino”

Page 4: Introduction to Arduino Programming

Hello World (Blink)

4

Page 5: Introduction to Arduino Programming

Hello World (Blink)

5

Variable

Page 6: Introduction to Arduino Programming

Hello World (Blink)

6

Comments

Page 7: Introduction to Arduino Programming

Hello World (Blink)

7

Comments

Good Comment: // Blue LED for Activity Indicator

Bad Comment: // Pin 13

Page 8: Introduction to Arduino Programming

Hello World (Blink)

8

Functions

Page 9: Introduction to Arduino Programming

Hello World (Blink)

9

Functions

Page 10: Introduction to Arduino Programming

Hello World (Blink)

10

Instruction

Page 11: Introduction to Arduino Programming

Hello World (Blink)

11

Instruction

Page 12: Introduction to Arduino Programming

Hello World (Blink)

12

Function Call

Page 13: Introduction to Arduino Programming

Hello World (Blink)

13A

rguments

Page 14: Introduction to Arduino Programming

Hello World (Blink)

14

Page 15: Introduction to Arduino Programming

IDE Tools Menu

15

Page 16: Introduction to Arduino Programming

IDE Tools Menu

16

Page 17: Introduction to Arduino Programming

IDE Tools Menu

17

Page 18: Introduction to Arduino Programming

IDE Tools Menu

18

Page 19: Introduction to Arduino Programming

Arduino IDE

19

Type Stuff Here

Compiler Output

Page 20: Introduction to Arduino Programming

Arduino IDE

20

Type Stuff Here

Compiler Output

Serial Monitor

Page 21: Introduction to Arduino Programming

Arduino IDE

21

Type Stuff Here

Compiler Output

Serial MonitorVerify & Upload

Page 22: Introduction to Arduino Programming

Arduino IDE

22

Type Stuff Here

Compiler Output

Serial MonitorVerify & Upload

Board & Serial Port

Page 23: Introduction to Arduino Programming

Blink Exercise

• Load the Blink Example and program it to your board

• Change the values of delay() to see how it affects the behavior

23Check the correct board and serial port are selected in the tools menu!

Page 24: Introduction to Arduino Programming

Hello WorldSerial Example

24

Page 25: Introduction to Arduino Programming

Serial objects

25

Page 26: Introduction to Arduino Programming

Serial objects

26

Enables Serial

Baud

Rate

Page 27: Introduction to Arduino Programming

Serial objects

27

Variables Strings

Control Characters

Print and Println

Page 28: Introduction to Arduino Programming

Serial objects

28

Variables Strings

Control Characters

Print and Println

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

Page 29: Introduction to Arduino Programming

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

Page 30: Introduction to Arduino Programming

Variables

30

Page 31: Introduction to Arduino Programming

How Much Memory is in your Arduino?

31

Page 32: Introduction to Arduino Programming

Variable Types

32

Bits Unsigned Range Signed Range

byte 8 0 to 255 N/A

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

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

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

2,147,483,647

float 32 ±3.4028235E+38 n/a

double 32 n/a n/a

Page 33: Introduction to Arduino Programming

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

Page 34: Introduction to Arduino Programming

Variable Scope

34

Page 35: Introduction to Arduino Programming

Variable Scope

35

Global

Page 36: Introduction to Arduino Programming

Variable Scope

36

Global

Local to loop()

Page 37: Introduction to Arduino Programming

Variable Don’t!

37

Page 38: Introduction to Arduino Programming

Variable Don’t!

38

Page 39: Introduction to Arduino Programming

Variable Don’t!

39

Page 40: Introduction to Arduino Programming

analogReadings[]

arrays

40

0 1 2 3 4 5

Page 41: Introduction to Arduino Programming

analogReadings[]

arrays

41

0 1 2 3 4 5

Size

Elements

Page 42: Introduction to Arduino Programming

analogReadings[]

arrays

42

0 1 2 3 4 5

Size

Elements

arrays are 0-index based.

So last element is always “1 less”

than the size!

Page 43: Introduction to Arduino Programming

Pin Functions

43

Page 44: Introduction to Arduino Programming

pinMode()

44

Analog(A0..A5)

Digital(0..13)

INPUT Digital Input, Pull-Up Off

Digital Input, Pull-Up Off

INPUT_PULLUP Digital Input, Pull-Up On

Digital Input, Pull-Up On

OUTPUT Digital Output Digital Output

Analog Pins can be used as Digital Pins pinMode(INPUT, Ax) isn’t necessary for analogRead()

Page 45: Introduction to Arduino Programming

digitalRead() & digitalWrite()

45

Page 46: Introduction to Arduino Programming

Pull-Up Resistor

46

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/

Page 47: Introduction to Arduino Programming

I/O Exercise

• Objective: Understand Inputs and Outputs

• Exercise: “Fix” the random LED

47

Page 48: Introduction to Arduino Programming

analogRead()

48

Analog Signal

Convert Digital Value

10-bits, stored as 8-bits

Values 0 to 1023

Voltage

0v to 5v

5 volts

1023 Steps= 4.887mV per Step

Calling analogRead() on an Analog Pin, automatically converts to Input

Page 49: Introduction to Arduino Programming

analogWrite()

49

analogWrite() isn’t Analog (Except on the Due) Uno Pins: 3, 5, 6, 9, 10, 11

Actual AnalogPulse Width Modulation (PWM)

Page 50: Introduction to Arduino Programming

Analog Exercise

• Connect Pin 6 to Analog 0 Run this code

• Then, disconnect from Pin 6 While Running the Code

50Remember to open the Serial Monitor!

Page 51: Introduction to Arduino Programming

Analog Exercise

• Connect Pin 6 to Analog 0 Run this code

• Then, disconnect from Pin 6 While Running the Code

51Remember to open the Serial Monitor! A0, A1, A2..A5 are integers!

Page 52: Introduction to Arduino Programming

functions()

52

Page 53: Introduction to Arduino Programming

Functions Getting Data Back

Page 54: Introduction to Arduino Programming

Functions Getting Data Back

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

Page 55: Introduction to Arduino Programming

Functions Getting Data Back

Return Type

Argum

ents

“Ret

urn”

Function Nam

e

Page 56: Introduction to Arduino Programming

Functions Returning Nothing

56

Return Type

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

Page 57: Introduction to Arduino Programming

Function Exercise

• “Re-Write” the built-in Blink Example to use a Function

• Exercise: add a “argument” to adjust the delay time

57

Page 58: Introduction to Arduino Programming

Control Structures

58

Page 59: Introduction to Arduino Programming

if-statements

59

Page 60: Introduction to Arduino Programming

if-statements

60

Page 61: Introduction to Arduino Programming

if-statements

61

Page 62: Introduction to Arduino Programming

if-statements

62

Page 63: Introduction to Arduino Programming

control operators

63

== Equal to

>>=

Greater than (or equal)

<<=

Less Than (or equal)

!= Not Equal to

|| OR

&& AND

| Bitwise OR

& Bitwise AND

Page 64: Introduction to Arduino Programming

#1 if-statement mistake

64

Page 65: Introduction to Arduino Programming

#1 if-statement mistake

65

= != ==

Page 66: Introduction to Arduino Programming

for() loop example

66

Page 67: Introduction to Arduino Programming

for() loop

67

Page 68: Introduction to Arduino Programming

for() loop

68

Control Variable

Page 69: Introduction to Arduino Programming

for() loop

69

Control Variable

Condition

Page 70: Introduction to Arduino Programming

for() loop

70

Control Variable

Condition

Increment

Page 71: Introduction to Arduino Programming

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

Page 72: Introduction to Arduino Programming

while() loop

72

Page 73: Introduction to Arduino Programming

while() loop

73

Condition

Page 74: Introduction to Arduino Programming

while() loop

74

Condition

Loop conditions are same as “if conditions”

Page 75: Introduction to Arduino Programming

while() and Serial

• This program echoes whatever is on the serial buffer back out

75

Page 76: Introduction to Arduino Programming

76

More information?

Visit

baldengineer.com