Cuauhtemoc Carbajal ITESM CEM Modified version of Sparkfun Slides - - PowerPoint PPT Presentation

cuauhtemoc carbajal itesm cem
SMART_READER_LITE
LIVE PREVIEW

Cuauhtemoc Carbajal ITESM CEM Modified version of Sparkfun Slides - - PowerPoint PPT Presentation

Cuauhtemoc Carbajal ITESM CEM Modified version of Sparkfun Slides http://learn.sparkfun.com/resources/Presentations/Intro%20to%20Arduino%20Slides/ Arduino Board Strong Friend Created in Ivrea, Italy in 2005 by Massimo Banzi & David


slide-1
SLIDE 1

Cuauhtemoc Carbajal ITESM CEM

Modified version of Sparkfun Slides

http://learn.sparkfun.com/resources/Presentations/Intro%20to%20Arduino%20Slides/

slide-2
SLIDE 2

Arduino Board

“Strong Friend” Created in Ivrea, Italy in 2005 by Massimo Banzi & David Cuartielles Open Source Hardware Atmel Processor Coding is accessible (C++, Processing, ModKit and MiniBloq)

slide-3
SLIDE 3

Getting Started

  • Materials:

Arduino Uno, Analog I/O, Digital I/O, Serial, handouts

  • Applications:

Arduino IDE for programming board

slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

Arduino Shields

PCB Built Shield Inserted Shield

slide-7
SLIDE 7

Arduino Shields

Micro SD MP3 Trigger Joystick

slide-8
SLIDE 8

Output

Output is always Digital

To Output a Digital signal (On or Off) use this code: digitalWrite ( pinNumber , value ); Where value is HIGH or LOW To output a signal that pretends to be Analog use this code: analogWrite ( pinNumber, value ); Where value is a number 0 - 255

slide-9
SLIDE 9

Circuit 1: Electrical

Remember: the longer leg on the LED is the positive leg and the shorter leg is the negative

Image created in Fritzing

slide-10
SLIDE 10

Output

Output is always Digital

Using a Digital signal that pretends to be an Analog signal is called Pulse Width Modulation Use Pulse Width Modulation, or P.W.M., for anything that requires a signal between HIGH and LOW P.W.M. is available on Arduino pins # 3, 5, 6, 9, 10, and 11

slide-11
SLIDE 11

Output

Output is always Digital, even when it’s P.W.M. For P.W.M. the Arduino pin turns on then off very fast

P.W.M. Signal @ 25% P.W.M. Signal @ 75% P.W.M. Signal rising

slide-12
SLIDE 12

Analog Input

  • To connect an analog Input to your Arduino use

Analog Pins # 0 - 5

  • To get an analog reading:

analogRead ( pinNumber );

  • Analog Input varies from 0 to 1023 on an Arduino
slide-13
SLIDE 13

Circuit 8: Electrical

Image created in Fritzing

slide-14
SLIDE 14

Analog Sensors

Examples:

Sensors Variables Values Signals Mic Volume Decibels Voltage Photoresistor Light Photons Voltage Potentiometer Dial Position Resistance Voltage Temp Sensor Temperature Celsius Voltage Flex Sensor Bend Resistance Voltage Accelerometer

Motion/Tilt/Acceleration Acceleration Voltage

slide-15
SLIDE 15

Digital Input

  • To connect digital input to your Arduino use Digital

Pins # 0 – 13 (Although pins # 0 & 1 are also used for serial)

  • Digital Input needs a pinMode command:

pinMode ( pinNumber, INPUT ); Make sure to use caps for INPUT

  • To get a digital reading: digitalRead ( pinNumber );
  • Digital Input values are only HIGH (On) or LOW (Off)
slide-16
SLIDE 16

Circuit 7: Electrical

Image created in Fritzing

slide-17
SLIDE 17

Code

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

slide-18
SLIDE 18

The Arduino Environment

slide-19
SLIDE 19

Board Type

slide-20
SLIDE 20

Serial Port / COM Port

slide-21
SLIDE 21

The Environment

slide-22
SLIDE 22

Parts of the Sketch

slide-23
SLIDE 23

Comments

  • Comments can be anywhere
slide-24
SLIDE 24

Comments

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

and */

slide-25
SLIDE 25

Comments

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

and */

  • Comments do not affect code
slide-26
SLIDE 26

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!

slide-27
SLIDE 27

Operators

The equals sign = is used to assign a value == is used to compare values

slide-28
SLIDE 28

Operators

And & Or && is “and” || is “or”

slide-29
SLIDE 29

Variables

Basic variable types: Boolean Integer Character

slide-30
SLIDE 30

Declaring Variables

Boolean: boolean variableName;

slide-31
SLIDE 31

Declaring Variables

Boolean: boolean variableName; Integer: int variableName;

slide-32
SLIDE 32

Declaring Variables

Boolean: boolean variableName; Integer: int variableName; Character: char variableName;

slide-33
SLIDE 33

Declaring Variables

Boolean: boolean variableName; Integer: int variableName; Character: char variableName; String: stringName [ ];

slide-34
SLIDE 34

Assigning Variables

Boolean: variableName = true;

  • r variableName = false;
slide-35
SLIDE 35

Assigning Variables

Boolean: variableName = true;

  • r variableName = false;

Integer: variableName = 32767;

  • r variableName = -32768;
slide-36
SLIDE 36

Assigning Variables

Boolean: variableName = true;

  • r variableName = false;

Integer: variableName = 32767;

  • r variableName = -32768;

Character: variableName = ‘A’;

  • r stringName = “SparkFun”;
slide-37
SLIDE 37

Variable Scope

Where you declare your variables matters

slide-38
SLIDE 38

Setup void setup ( ) { }

The setup function comes before the loop function and is necessary for all Arduino sketches

slide-39
SLIDE 39

Setup void setup ( ) { }

The setup header will never change, everything else that occurs in setup happens inside the curly brackets

slide-40
SLIDE 40

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

  • utput, remember to use CAPS
slide-41
SLIDE 41

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...

slide-42
SLIDE 42

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

slide-43
SLIDE 43

Setup, Interrupts

void setup ( ) { attachInterrupt (interrupt, function, mode) }

Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). This is a way around the linear processing of Arduino

slide-44
SLIDE 44

Attach Interrupt Example

interrupt # ISR mode

slide-45
SLIDE 45

Setup, Interrupts

void setup ( ) { attachInterrupt (interrupt, function, mode) }

Interrupt: the number of the interrupt, 0

  • r 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

slide-46
SLIDE 46

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

slide-47
SLIDE 47

If Statements

if ( this is true ) { do this; }

slide-48
SLIDE 48

If

if ( this is true ) { do this; }

slide-49
SLIDE 49

Conditional

if ( this is true ) { do this; }

slide-50
SLIDE 50

Action

if ( this is true ) { do this; }

slide-51
SLIDE 51

Else

else { do this; }

slide-52
SLIDE 52

Basic Repetition

  • loop
  • For
  • while
slide-53
SLIDE 53

Basic Repetition

void loop ( ) { }

slide-54
SLIDE 54

Basic Repetition

void loop ( ) { }

slide-55
SLIDE 55

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

slide-56
SLIDE 56

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

slide-57
SLIDE 57

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

slide-58
SLIDE 58

Basic Repetition

void loop ( ) { }

slide-59
SLIDE 59

Basic Repetition

for (int count = 0; count<10; count++) { //for action code goes here //this could be anything }

slide-60
SLIDE 60

Basic Repetition

for (int count = 0; count<10; count++) { //for action code goes here }

slide-61
SLIDE 61

Basic Repetition

for (int count = 0; count<10; count++) { //for action code goes here }

slide-62
SLIDE 62

Basic Repetition

for (int count = 0; count<10; count++) { //for action code goes here }

slide-63
SLIDE 63

Basic Repetition

for (int count = 0; count<10; count++) { //for action code goes here }

slide-64
SLIDE 64

Basic Repetition

for (int count = 0; count<10; count++) { //for action code goes here }

slide-65
SLIDE 65

Basic Repetition

for (int count = 0; count<10; count++) { //for action code goes here }

slide-66
SLIDE 66

Basic Repetition

while ( count<10 ) { //while action code goes here }

slide-67
SLIDE 67

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 }

slide-68
SLIDE 68

Basic Repetition

while ( count<10 ) { //looks basically like a “for” loop //except the variable is declared before //and incremented inside the while //loop }

slide-69
SLIDE 69

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 }

slide-70
SLIDE 70

Programming, Serial and Virtual Prototyping

slide-71
SLIDE 71

Code

if ( You like semicolons ) { Stay here for intro to Arduino code } else { Join the MODKit group for GUI code }

slide-72
SLIDE 72

Serial Communication

Serial Communication is the transferring and receiving of information between two machines, the Arduino dedicates pin # 0 to receiving information and pin 1 to transferring information

slide-73
SLIDE 73

Serial in Setup

slide-74
SLIDE 74

Serial Monitor

slide-75
SLIDE 75

Serial Communication

slide-76
SLIDE 76

Serial Activity with Circuit 7

  • Communication
  • Troubleshooting circuits
  • Debugging Code
slide-77
SLIDE 77

Serial Communication: Serial Setup

void setup ( ) { Serial.begin ( 9600 ) ; }

In this case the number 9600 is the baudrate at which the computer and Arduino communicate

slide-78
SLIDE 78

Serial Communication: Sending a Message

void loop ( ) { Serial.print ( “Constructivism & “ ) ; Serial.println ( “Mechatronics” ) ; }

slide-79
SLIDE 79

Serial Communication: Serial Debugging

void loop ( ) { int xVar = 10 ; Serial.print ( “Variable xVar is “ ) ; Serial.println ( xVar ) ; }

slide-80
SLIDE 80

Serial Communication: Serial Troubleshooting

void loop ( ) { Serial.print ( “Digital pin 9 reads “ ) ; Serial.println ( digitalRead ( 9 ) ) ; }

slide-81
SLIDE 81

Serial Communication: Circuit 7 code

void loop ( ) { buttonState = digitalRead(inputPin); if (buttonState== HIGH){ digitalWrite (ledPin, LOW) } else { digitalWrite(ledPin, HIGH);

Serial.print (“button state is “); Serial.println ( buttonState );

} }