Arduino Kids Course

Embed Size (px)

Citation preview

  • 8/11/2019 Arduino Kids Course

    1/48

    Arduino Camp

  • 8/11/2019 Arduino Kids Course

    2/48

    Picture Hardware Hacking

    by nicholasjon- flickr

  • 8/11/2019 Arduino Kids Course

    3/48

    What will we learn?

    A little about what computers aremade of.

    How we tell computers what to do

    How to build our own robot!

  • 8/11/2019 Arduino Kids Course

    4/48

    What is a computer?

    Definition: An electronic device forstoring and processing data.

    What is data?

    What does it mean to process?

    What are computers made of?

  • 8/11/2019 Arduino Kids Course

    5/48

    What is a computer made of?

    Components

    Electrical devices that together processdata

    PCBPrinted Circuit Board

    A board that holds and connects thesecomponents together

    Wires which are flat on a board.

  • 8/11/2019 Arduino Kids Course

    6/48

    Lets put one together!

    Build our own small circuit

    The basis of a computer

    Computers have millions ofcomponents

    Our circuit will only have 5

  • 8/11/2019 Arduino Kids Course

    7/48

    Lets put one together!

    Place components on Board

    Solder them to board

    Soldering is just like hot glue but with metal

    Solder will carry electricity and hold partstogether

    Soldering is just heating up the metal

    glue and letting it cool on bothcomponents holding them to the board.

    Ok lets break up and solder ourbadges!

  • 8/11/2019 Arduino Kids Course

    8/48

    Programmers?

    Definition

    a person who writes computer programs; aperson who programs a device, especially

    a computer.

    Are any of you programmers?

  • 8/11/2019 Arduino Kids Course

    9/48

    We are all programmers

    Giving people directions is a form ofprogramming.

    Languages English

    Java

    C

    Spanish

  • 8/11/2019 Arduino Kids Course

    10/48

    What can programming do?

    The Internet

    Video Games

  • 8/11/2019 Arduino Kids Course

    11/48

    What can programming do?

    Machines

    Electronics

  • 8/11/2019 Arduino Kids Course

    12/48

    Getting Started

    Lets write a simple program Program me to open my drink!

    Things to think about

    What commands can you give me? Does the command you give me work all the

    time?

    How do I know if something went wrong?

    What do I do if something goes wrong.

  • 8/11/2019 Arduino Kids Course

    13/48

    Opening a Drink

    Step 1

  • 8/11/2019 Arduino Kids Course

    14/48

    Opening a Drink

    1.)Do you have a drink?

    If not then you are done.

    If so then go to step 2.

    2.)Pickup Drink.

    3.)Is drink already open? If yes then you are done!

    If no then goto step 4.

    4.)place finger under tab and pull.

    5.)Is drink now open? If yes you are done.

    If no then contact technical support!

  • 8/11/2019 Arduino Kids Course

    15/48

    Lets learn another language

    Computers dont speak Engl

    Computers speak computer languages C

    Java

    Pascal

    .Net

    Language is much smaller Less words

    Stricter rules

  • 8/11/2019 Arduino Kids Course

    16/48

    Who are we going to talk to?

    Arduino Small computer that we can change/modify

  • 8/11/2019 Arduino Kids Course

    17/48

    Our first Arduino program

    Blink a light Make sure everything is working

    Learn about Digital Output

    Learn some basics of the Arduino language

  • 8/11/2019 Arduino Kids Course

    18/48

    Our first Arduino program

    How does a light work Electricity flows from the +(positive) to the(negative)

    Along the way it makes things work (like light bulbs and motors!)

    LED (light emitting diode) is a type of light -> very efficient and small

    To much electricity would damage the light.

    We use a resistor to slow the flow of electricity to make sure the light bulbdoesnt get damaged.

  • 8/11/2019 Arduino Kids Course

    19/48

    Our first Arduino program

    With an Arduino GND = - (negative)

    PIN 13 = + (positive)

    Pin 13 is a digital OUTPUT

  • 8/11/2019 Arduino Kids Course

    20/48

    Our first Arduino program

    What does digital mean? Means it can have two states. ON or OFF

    Just like a switch

    ON means electricity flows

    OFF means no electricity flows

    When pin 13 is ON electricity will flow to the light and it willbe on.

    When pin 13 is OFF electricty will not flow the light will beoff.

    How do we tell the Arduino to turn on or off the light? PROGRAM!

  • 8/11/2019 Arduino Kids Course

    21/48

    Our first Arduino program

    Lets write in english first Think how we would blink a light if we were doing it

    Write it out in english (since humans are best at this language)

    Convert these steps into language the computer understands(translate)

    Can anyone tell me how to blink a light?

  • 8/11/2019 Arduino Kids Course

    22/48

    Our first Arduino program

    Turn light ON

    Wait 1 second

    Turn light off

    REPEAT!

  • 8/11/2019 Arduino Kids Course

    23/48

    Blink Program

  • 8/11/2019 Arduino Kids Course

    24/48

    Our first Arduino program

    Arduino Language Use a pin as an output = pinMode([PIN NUMBER], OUTPUT)

    Wait for some time = delay([VALUE]) Value = seconds * 1000 (milliseconds)

    Turn pin on or turn pin off = digitalWrite( [PIN NUMBER], [VALUE])

    Value = 1 or 0 (1 = on, 0 = off)

  • 8/11/2019 Arduino Kids Course

    25/48

    Our first Arduino program

    We have to give our directions to the computer Press the upload button in the top left of the Arduino program

    What Happens?

  • 8/11/2019 Arduino Kids Course

    26/48

    Make Blink do More?

    Can you make the light blink faster?

    Can you make the light blink slower?

    Can you make the light spend more time on than off?

    Plug the blue wire into pin 12, can you make that light blink in unison withthe light on pin 13?

    Lets take 20 minutes and try! Work with your partner, if you get stuck raiseyour hand for help!

  • 8/11/2019 Arduino Kids Course

    27/48

    Blink Program

  • 8/11/2019 Arduino Kids Course

    28/48

    Blink Program

  • 8/11/2019 Arduino Kids Course

    29/48

    Blink Program

  • 8/11/2019 Arduino Kids Course

    30/48

    Blink Program

  • 8/11/2019 Arduino Kids Course

    31/48

    More than just blinking!

    We can use the on off signal to do more than just blink

    We can control lots of other things than just lights. Motors

    Heaters

    Pumps

    Speakers

    And lots more

    Using control of all these together we can build amazing things lie Xboxes,Robots, and Cell phones.

  • 8/11/2019 Arduino Kids Course

    32/48

    What about INPUT

    We have learned about controlling external items. OUTPUT

    What about getting data from the environment. INPUT We can read if a pin is on or off

  • 8/11/2019 Arduino Kids Course

    33/48

    How do we attach a button?

  • 8/11/2019 Arduino Kids Course

    34/48

    RS232-TTL Trace

    Note Inversion

  • 8/11/2019 Arduino Kids Course

    35/48

    Lets Send Some Serial

    Open BB_Serial_8N1

    Using delayMicroseconds();

    Delay Bit time ~(1/9600)

    Start and Stop Bit

    Issues

    Skew

    Bit Banging (Interrupts etc can causeerrors)

    Runs under the main run loop

  • 8/11/2019 Arduino Kids Course

    36/48

    Use the Bus Pirate

    Open terminal 119200-8-N-1

    m

    3

    5

    1

    1

    11

    (2)

  • 8/11/2019 Arduino Kids Course

    37/48

    Make this Better?

    Problems?

    Uses Run Loop

    Timing is inaccurate

    Interrupts Effect loop (priority)

    Suggestions?

  • 8/11/2019 Arduino Kids Course

    38/48

    Solutions

    Interrupts

    Break normal loop and run ISR

    Ensure Priority

    Allow main run loop to do other tasks

    Timers

    Time based trigger

    Resolve issues of code timing

  • 8/11/2019 Arduino Kids Course

    39/48

    Button Setup

  • 8/11/2019 Arduino Kids Course

    40/48

    Interrupts

    Volatile

    attachInterrupt(p1, p2, p3)

    param1 = Which interrupt to listen for.

    param2 = Which code function to call

    param3 = condition to watch for

    LOW = at a LOW state

    RISING = changes from LOW to HIGH

    FALLING = changes from HIGH to LOW

    CHANGE = pin changes state

  • 8/11/2019 Arduino Kids Course

    41/48

    Interrupts

    On Trigger stops what it is doing

    Runs ISR

    Returns to normal execution after ISR

    PIN 2,3 = Interrupt 0,1

  • 8/11/2019 Arduino Kids Course

    42/48

    Interrupts

    ReAssigning Interrupts

    Use attachInterrupt() again

    Starting / Stopping Interrupts

    noInterrupts()

    interrupts().

    Removing Interrupts

    detachInterrupt(interrupt_number)

    Can be used within an ISR!

  • 8/11/2019 Arduino Kids Course

    43/48

    Problems?

    Hack this example

    Can you make it toggle?

    Add Debounce

    Add another button on pin 3?

    Make a two stage togglerequire button1 then button 2 pressed in order to toggle.

    (out of order presses does nothing)

  • 8/11/2019 Arduino Kids Course

    44/48

    Timers

    Three timers

    Capture/Compare Registers

    Dont use low level yet

    TimerOne Library

  • 8/11/2019 Arduino Kids Course

    45/48

    Timers

    Timers on Arduino

    Timer0(System timing, PWM 5 and 6) millis()

    Timer1(PWM 9 and 10)Used to drive PWM

    outputs for digital pins 9 and 10.

    Timer2 (PWM 3 and 11)Used to drive PWMoutputs for digital pins 3 and 11.

    While all the timers are used only Timer0has assigned timer ISR. This means wecan hijack Timer1 / Timer2 for our uses.

  • 8/11/2019 Arduino Kids Course

    46/48

    TimerOne

    initialize(period)

    The minimum period or highest frequencythis library supports is 1 microsecond or 1

    MHz. The maximum period is 8388480

    microseconds or about 8.3 seconds.

    Note that setting the period will changethe attached interrupt and both pwmoutputs' frequencies and duty cyclessimultaneously.

  • 8/11/2019 Arduino Kids Course

    47/48

    TimerOne

    pwm(pin, duty, period)Generates aPWM waveform on the specified pin.Output pins for Timer1 are PORTB pins 1

    and 2, so you have to choose betweenthese two, anything else is ignored. OnArduino, these are digital pins 9 and 10,

    so those aliases also work.

  • 8/11/2019 Arduino Kids Course

    48/48

    TimerOne

    attachInterrupt(function, period)Calls afunction at the specified interval inmicroseconds.

    setPwmDuty(pin, duty)A fast shortcut forsetting the pwm duty

    detachInterrupt()Disables the attached

    interrupt. disablePwm(pin)Turns PWM off for the

    specified pin so you can use that pin for