sami makerspace make: an electronics workshop. arduino basics credit to: sparkfun and linz craig,...

71
SAMI MAKERSPACE MAKE: AN ELECTRONI CS WORKSHO P

Upload: brianna-lamb

Post on 19-Jan-2016

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SAMI

MAKERSPACE

MA

K E : A

N E

L E CT R

ON

I CS W

OR

K S HO

P

Page 2: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

ARDUINO

BASICS

Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson,

Tai Johnson, and Eli Santistevan

Page 3: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

THIS PRESENTATION:Part One: Hardware

• Getting around and Arduino

• Getting connected

• The breadboard

• A simple circuit

Part Two: Software

• The integrated development environment

Part Three: Programming

• Inputs and outputs

• Some conventions

• First Program: Blink

• Variables

• Analog versus digital

• Pulse wave modulation

• Second Program: Fade

• Third Program: Button

• Conditional statements

• Serial communication

Page 4: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

PART

ONE: T

HE

HARDWARE

AR

DU

I NO

, U

SB

CA

BL E

, L A

PT

OP

, B

RE

AD

BO

AR

D

Page 5: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

GETTIN

G AROUND

AN ARDUIN

O

TH

E “

UN

O” M

OD

EL

Page 6: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,
Page 7: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

Analog INPUTS

Digital I\OPWM(3, 5, 6, 9, 10,

11)

PWR IN USB (to Computer)

SCL\SDA(I2C Bus)

POWER 5V / 3.3V /

GND

RESET

Page 8: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

LET’

S GET

CONNECTED!

Page 9: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

PLUG IN YOUR ARDUINO

Page 10: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

BREADBOARDS

RA

PI D

AN

D S

OL D

ER

- F RE

E P

RO

TO

TY

PI N

G!

Page 11: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

PROTOTYPING CIRCUITS SOLDERLESS BREADBOARDOne of the most useful tools in an

engineer or Maker’s toolkit. The three most important things:

• A breadboard is easier than soldering• A lot of those little holes are connected, which ones?• Sometimes breadboards break

Page 12: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

WHAT’S A BREADBOARD?

Page 13: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SOLDERLESS BREADBOARD

Each row (horiz.) of 5 holes are connected.

Vertical columns – called power bus are connected vertically

Page 14: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

A SIM

PLE

CIRCUIT

L I GH

T U

P A

N L

ED

US

I NG

TH

E A

RD

UI N

O

Page 15: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

USE THE BREADBOARD AND ARDUINO TO BUILT A SIMPLE CIRCUIT

Use the breadboard to wire up a single LED with a 330 Ohm Resistor (Orange-Orange-Brown).Note: the longer leg on the LED is the positive leg and the shorter leg is the negative

Page 16: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,
Page 17: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,
Page 18: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

PART

TW

O: THE

SOFTW

ARE

I NT

EG

RA

TE

D D

EV

EL O

PM

EN

T E

NV

I RO

NM

EN

T

Page 19: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

THE IN

TEGRAT

ED

DEVELOPM

ENT

ENVIRONMENT

Page 20: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

OPEN UP “ARDUINO” APPLICATION

Look for the icon on the desktop of the white clam-shell computers.

You can also download the application for free to your own computers (you may need to also install the proper drivers)

Page 21: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

ARDUINO: INTEGRATED DEVELOPMENT ENVIRONMENT (IDE)

Two required functions / methods / routines:

void setup()

{

// runs once

}

void loop()

{

// repeats

}

error & status messages

Page 22: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SETTINGS: TOOLS SERIAL PORT

Your computer communicates to the Arduino microcontroller via a serial port through a USB-Serial adapter.

Check to make sure that the drivers are properly installed.

Page 23: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SETTINGS: TOOLS BOARD

Next, double-check that the proper board is selected under the ToolsBoard menu.

Page 24: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

PART

3:

PROGRAMMIN

G

!C

OD

E F

OR

TH

E M

AS

SE

S

Page 25: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

CONCEPT: I

NPUTS

VS.

OUTPUTS

Page 26: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

CONCEPTS: INPUT VS. OUTPUTReferenced from the perspective of the microcontroller

(electrical board).

Inputs is a signal / information going into the board.

Output is any signal exiting the board.

Almost all systems that use physical computing will have some form of output.What are some examples of Outputs?

Page 27: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

CONCEPTS: INPUT VS. OUTPUT

Inputs is a signal / information going into the board.

Output is any signal exiting the board.

Examples: Buttons Switches, Light Sensors, Flex Sensors, Humidity Sensors, Temperature Sensors…

Examples: LEDs, DC motor, servo motor, a piezo buzzer, relay, an RGB LED

Page 28: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

YOU DON’T NEED TO SPEAK BINARY!

The Arduino IDE allows you to work in a “high-level programming language.” The code you write is readable to humans (with a little training)

The IDE does the hard work of making your code something that the chip on the Arduino understands:

Page 29: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SUB-ROUTINES/FUNCTIONS/COMMANDS

Arduino uses pre-made sub-routines (small programs) to make your life easier.

Programs like:

• delay()

• if()

• loop()

Most programs need an input (what they are acting on) and give an output (usually a numerical value)

The names of these programs (like most things in Arduino) are case sensitive, so type carefully!

digitalWrite() is not the same as digitalwrite()

Can you guess what these do?

Page 30: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

pinMode(pin, INPUT/OUTPUT);

ex: pinMode(13, OUTPUT);

digitalWrite(pin, HIGH/LOW);

ex: digitalWrite(13, HIGH);

delay(time_ms);

ex: delay(2500); // delay of 2.5 sec.

// NOTE: -> commands are CASE-sensitive

Page 31: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

digitalWrite()

analogWrite()

digitalRead()

if() statements / Boolean

analogRead()

Serial communication

BIG

6 C

ON

CEP

TS

Page 32: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SOME CONVENTIONS: CURLY BRACKETSWhen you call a sub-routine every thing that happens in the program should be contained in curly brackets:

Everything between the curly brackets {..} is part of the loop function.

Page 33: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SOME CONVENTIONS: SEMICOLONS

Any line of code that isn’t calling a sub-routine, or is a sub-routine that is only one line long should always end with a semicolon (;)

If you forget these, you will get errors when you try to “compile” the code!

Page 34: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SOME CONVENTIONS: COMMENTS

It’s easier to get lost in your own code than you may think.

Leave comments to yourself, especially before you do something “tricky.”

They are not needed to make the program work, but can save you a lot of trouble in the long run.

Page 35: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

FIRST

PROGRAM: B

LINK

L ET

’ S M

AK

E A

N L

ED

BL I N

K!

Page 36: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

PLAN YOUR ATTACK WITH A FLOWCHART

Initialize

Turn LED ON Wait

Turn LED OFF

Wait

Page 37: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

1. PUT IN THE “BARE MINIMUM”

The “setup” function runs once and will lock in any settings you want.

The “loop” function will run for as long as the Arduino has power!

Page 38: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

2. INITIALIZE THE LED PIN AS A “OUTPUT”

Use the “pinMode” command. This requires two inputs (or arguments). You must tell it what pin to act on (13 in this case) and whether that pin is an OUTPUT or INPUT (this is case sensitive).

Page 39: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

3. TURN ON THE LED FOR ONE SECOND

Page 40: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

4. TURN OFF THE LED FOR ONE SECOND

Page 41: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

5. REPEAT!

Once the end of the program is reached, it will go to the beginning of loop() and run it again. And again. And again….

Page 42: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SOME EXTRA CHALLENGES…

• Make the light blink with a 200 ms interval.

• Change the blink to mimic a heart beat.

• What’s the fastest blink your eye can detect?

• Add two, three, or four LED’s. (each LED will need its own 330 Ohm resistor).

Page 43: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

CONCEPT: V

ARIABLE

S

Page 44: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

CONCEPT: VARIABLES

• Declaring variables• Types of variables• Variable scope• Initializing variables• Variable rollover

Page 45: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

TYPES OF VARIABLES

8 bits 16 bits 32 bits

byte char

intunsigned int

longunsigned longfloat

Page 46: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

DECLARING VARIABLES AND SCOPE OF VARIABLES

Variable Scope

Global Vs.

Function-level

Page 47: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

INITIALIZING A VARIABLE: EXAMPLE

Compare this code our early “blink” program.

There is an “int” type variable declared. • An integer (no decimal)

The variable is named “ledPin”

ledPin is initialized to the value “5”

This is a “global” variable since it was declared outside of a function.• All other functions are able to “call”

ledPin and they can change its value

Notice how easy it would be to change the pin on your circuit that has the LED connected to it?

Page 48: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

CONCEPT: D

IGITA

L VERSUS

ANALOG

Page 49: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

CONCEPTS: ANALOG VS. DIGITAL

Microcontrollers are digital devices – ON or OFF. Also called – discrete.

Analog signals are anything that can be a full range of values. What are some examples?

How can we get 3V from a microcontroller that can only be on (5V) or off (0V)?5 V

0 V

5 V

0 V

Page 50: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

CONCEPT: P

WM

PU

L SE

WA

VE

MO

DU

L AT

I ON

Page 51: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

CONCEPT: PULSE WAVE MODULATION

To create an analog signal, the digital microcontroller uses a technique called PWM. By varying the duty cycle, we can mimic an “average” analog voltage.

Pulse Width Modulation (PWM)

Page 52: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SECOND PROGRAM: F

ADE

L ET

’ S D

I M T

HA

T L

ED

!

Page 53: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

analogWrite(pin, val);

pin – refers to the OUTPUT pin (limited to pins 3, 5, 6, 9, 10, 11.) – denoted by a ~ symbol

val – 8 bit value (0 – 255).

0 => 0V | 255 => 5V

PROJECT #2 – FADINGINTRODUCING A NEW COMMAND…

Page 54: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

THIR

D PROGRAM:

BUTTON

CO

NT

RO

L TH

AT

LE

D W

I TH

DI G

I TA

L IN

PU

T!

Page 55: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

USE AN EXAMPLE PROGRAM

In Arduino, open up:

File Examples 02.Digital Button

Page 56: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

DIGITAL INPUT: BUTTON

to Digital Pin 2

Page 57: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

DIGITAL INPUT: BUTTON

This is just like our 1st circuit!

Page 58: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

DIGITAL INPUT• Connect digital input to your Arduino using Pins # 0 – 13

(Although pins # 0 & 1 are also used for programming)

• Digital Input needs a pinMode command:

pinMode (pinNumber, INPUT);

Make sure to use ALL CAPS for INPUT

• To get a digital reading:

int buttonState = digitalRead (pinNumber);

• Digital Input values are only HIGH (On) or LOW (Off)

Page 59: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

DIGITAL SENSORS• Digital sensors are more straight forward

than Analog

• No matter what the sensor there are only two settings: On and Off

• Signal is always either HIGH (On) or LOW (Off)

• Voltage signal for HIGH will be a little less than 5V on your Uno

• Voltage signal for LOW will be 0V on most systems

Page 60: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,
Page 61: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

USING C

ONDITIO

NAL

STATE

MENTS

I F T

HI S

, T

HE

N T

HA

T!

Page 62: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

CONDITIONAL STATEMENTS: IF()

Page 63: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

void loop()

{

int buttonState = digitalRead(5);

if(buttonState == LOW)

{ // do something

}

else

{ // do something else

}

}

CONDITIONAL STATEMENTS: IF()

DIG INPUT

Page 64: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

BOOLEAN OPERATORS

<Boolean> Description

( ) == ( ) is equal?( ) != ( ) is not equal?( ) > ( ) greater than( ) >= ( ) greater than or equal( ) < ( ) less than( ) <= ( ) less than or equal

Page 65: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SERIAL C

OMMUNICAT

ION

KE

EP

I NG

AN

EY

E O

N Y

OU

R P

RO

GR

AM

Page 66: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

USING SERIAL COMMUNICATIONMethod used to transfer data between two

devices.

Arduino dedicates Digital I/O pin # 0 to receiving and Digital I/O pin #1 to transmit.

Data passes between the computer and Arduino through the USB cable. Data is transmitted as a string of zeros (‘0’) and ones (‘1’).

Page 67: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SERIAL MONITOR & ANALOGREAD()

Initializes the Serial Communication

9600 baud data rate

prints data to serial bus

Page 68: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

SERIAL MONITOR & ANALOGREAD()

Opens up a Serial Terminal Window

Page 69: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

EXAMPLE

void loop ( )

{

Serial.print(“Hands on “) ;

Serial.print(“Learning ”) ;

Serial.println(“is Fun!!!”) ;

}

Serial.print adds to the same line. Serial.println adds to the

line, then “returns” (starts a new line).

Page 70: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,
Page 71: SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,

USING SERIAL COMMUNICATION TO DEBUG AND TROUBLESHOOTLet’s use serial communication to watch what is happening in our “button” program.