Slide 1

Slide 1 text

Google+: klab.ca/+ Twitter: @pearlchen Introduction to Arduino Programming Pearl Chen

Slide 2

Slide 2 text

klab.ca Read about all the Arduino boards: http://arduino.cc/en/Main/Boards What is Arduino? It’s hardware...

Slide 3

Slide 3 text

klab.ca The Arduino IDE. Download the latest here: http://arduino.cc/en/Main/Software What is Arduino? And it’s software!

Slide 4

Slide 4 text

klab.ca Why use an Arduino? (Part 1) Analyze incoming data using computer logic Affect something physical (or virtual) Sense something happening in the physical world Scenario #1 The computer analyzes the weight of the pressure input to be larger than a sparrow’s weight. Squirrel?? A water hose is triggered and a spray of water is sent out in the direction of the intruder! A pressure sensor detects that something is trying to take seeds from a bird feeder. Examples The computer analyzes the levels to be lower than normal. The interface on a mobile phone app updates and warns the diabetic that they should eat an apple. A diabetic uses a glucose sensor to measure their blood-sugar level.

Slide 5

Slide 5 text

klab.ca Why use an Arduino? (Part 2) Affect something physical Notification of something happening in the virtual world Scenario #2 The computer analyzes the sender to be your best friend. A LED sitting on your desk blinks urgently to get your attention. A Twitter @mention to your Twitter handle is detected. Examples Analyze incoming data using computer logic

Slide 6

Slide 6 text

klab.ca Need more detail? Check out the Sparkfun buying guide: https://www.sparkfun.com/arduino_guide How to choose an Arduino Leonardo Mega LilyPad Chip ATmega32U4 ATmega2560 ATmega328 Speed 16MHz 16MHz 8MHz Memory 32Kb 256Kb 32Kb I/O Pins 20 54 9 or 14 Best used for general, all purpose larger projects wearables

Slide 7

Slide 7 text

klab.ca Let’s get coding!

Slide 8

Slide 8 text

klab.ca Example code • There are lots of built-in code samples to get you started: File > Examples ➡Open File > Examples > 01.Basic > BareMinimum • In most cases, the only thing you’ll need to do to get the examples working is to find & replace the pin numbers in the code to the actual pins used in your circuit. ➡ github.com/pchen/ArduinoLilyPadProtoSnap Example code geared specifically for LilyPad ProtoSnap pins:

Slide 9

Slide 9 text

klab.ca Functions A block of code, grouped with a name. • To define a function:
 
 void functionName() {
 
 } • To call/execute a function:
 
 functionName();

Slide 10

Slide 10 text

klab.ca setup() and loop() 2 functions are required for every Arduino program: • setup()
 Runs on power up or when reset button is pressed. • loop()
 Executes forever, as long as there is power and no code errors

Slide 11

Slide 11 text

klab.ca Verify and Upload • The 2 most used buttons on the Arduino IDE • Verify - make sure that there are no errors • Upload - compiles (checks for errors too) and writes program to microcontroller’s memory

Slide 12

Slide 12 text

klab.ca Comments When you need to make a note to yourself or others. • Ignored by the Arduino.   • // single line comment • /*
 multi
 line
 comment
 */

Slide 13

Slide 13 text

klab.ca Arduino code • Simplified version of Java. • .ino extension for Arduino version 1.0+ 
 .pde for older sketches • Called a “sketch” because it should feel like picking up a pencil and making a quick drawing.
 ➡Upload to board: 01.Basics > BlinkExternalLED
 (We’ll get to how this sketch is made soon...)

Slide 14

Slide 14 text

klab.ca Arduino code walk-through ➡Open 01.Basics > TurnOnBuiltInLED • Goal: Light up one (then two) of the external LilyPad LEDs


Slide 15

Slide 15 text

klab.ca Statements A single command. • Always end with a semicolon (;)
 (Much like all English sentences end with a period.)
 • Examples:
 delay(1000); // calling a function 
 int a = 1; //assigning a value to variable


Slide 16

Slide 16 text

klab.ca Variables • Define a variable:
 datatype variableName; • Examples:
 boolean isSaturday = true;
 int myNumber = 1;
 float pi = 3.14;
 char firstInitial = ‘P’;
 char myName[] = “Pearl”;
 String myName = “Pearl Chen”; Common Data Types boolean true or false int whole number float decimal number char single character char[] or String group of characters A named container that holds a value for later use.

Slide 17

Slide 17 text

klab.ca pinMode() and digitalWrite() for output (Part 1) • pinMode( pin#, OUTPUT );
 Set a pin to send outgoing commands • digitalWrite( pin#, HIGH );
 Output to a pin: HIGH / 5 volts / “on” / 1 • Goal: Light up one (then two) of the external LilyPad LEDs
 Solution: 01.Basics > TurnOnExternalLED

Slide 18

Slide 18 text

klab.ca pinMode() and digitalWrite() for output (Part 2) • pinMode( pin#, OUTPUT );
 Set a pin to send outgoing commands • digitalWrite( pin#, LOW );
 Output to a pin: LOW / 0 volts / “off” / 0 • delay( milliseconds );
 Pause for a set number of milliseconds before executing next line of code. (Note: 1 sec = 1000 ms) • Goal: Try to make the built in LED blink. 
 Solution: 01.Basics > BlinkBuiltInLED

Slide 19

Slide 19 text

klab.ca pinMode() and digitalRead() for input • pinMode( pin#, INPUT_PULLUP );
 Set a pin be used as a digital input pin • digitalRead( pin# );
 Listen for input values on a pin. Since it’s a digital signal, it will be either HIGH (1) or LOW (0) • (The input will "float" and randomly return either HIGH or LOW without a resistor wired to ground in a circuit. Arduino has a built-in “pull-up resistor” which can be activated using pinMode() as above. More info here.) ➡Open 02.Digital > ButtonBuiltInLED ➡Open 02.Digital > SwitchBuiltInLED

Slide 20

Slide 20 text

klab.ca Conditionals (Examples) • From ButtonBuiltInLED:
 
 buttonState = digitalRead(buttonPin);
 
 if (buttonState == LOW) {
 // turn LED on
 }
 else {
 // turn LED off
 }

Slide 21

Slide 21 text

klab.ca Conditionals • “if” test:
 if (condition test) {
 //statements if true
 } • “if/else” test:
 if (condition test) {
 //statements if true
 }else{
 //statements if false
 } Comparison Operators == equal to != not equal to < less than <= less than or 
 equal to > greater than >= greater than or 
 Logic branching -- allows for different outcomes depending on the condition being tested.

Slide 22

Slide 22 text

klab.ca analogWrite() for analog outputs • Analog output pins: 3, 5, 6, 9, 10, 11 • pinMode( pin#, OUTPUT );
 Setting pinMode() not necessary for analog -- but won’t hurt. • analogWrite( pin#, dutyCycle );
 Output to a pin a duty cycle between 0 (always off) and 255 (always on). • Goal: Light up external LilyPad LEDs on pin 5 and 6 at brightness steps between 0 and 255
 Solution: 03.Analog > DimExternalLEDs

Slide 23

Slide 23 text

klab.ca analogRead() for analog input • pinMode( pin#, INPUT );
 Setting pinMode() not necessary for analog -- but won’t hurt. • analogRead( pin# );
 Value will be between 0 and the components limit. • Solution: 03.Analog > TempSensor • Also try: 03.Analog > ToneTheremin

Slide 24

Slide 24 text

klab.ca Serial for debugging • in setup() use Serial.begin( baudRate ) • in loop() use Serial.print() or Serial.println() • To see the serial monitor console, go to Tools > Serial Monitor while your LilyPad is hooked up to your computer via USB. Make sure that the baud rate in the lower righthand corder is also the same as that in Serial.begin().

Slide 25

Slide 25 text

klab.ca Coding Challenge Write an Arduino sketch that keeps “score”. • Light up an LED each time you click on a button. • When you reach 5 points: buzzzzzzzz! (You win.) • Bonus: Try to remove points when an opponent blocks your light sensor.

Slide 26

Slide 26 text

klab.ca Thank you! ! Oh, and check out some upcoming Arduino workshops! Sign up for mailing list on karma-laboratory.com.