Vous êtes sur la page 1sur 48

Arduino Camp

Picture Hardware Hacking


by nicholasjon- flickr
What will we learn?
A little about what computers are
made of.
How we tell computers what to do
How to build our own robot!


What is a computer?
Definition: An electronic device for
storing and processing data.
What is data?
What does it mean to process?
What are computers made of?


What is a computer made of?
Components
Electrical devices that together process
data
PCB Printed Circuit Board
A board that holds and connects these
components together
Wires which are flat on a board.

Lets put one together!
Build our own small circuit
The basis of a computer
Computers have millions of
components
Our circuit will only have 5

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 parts
together
Soldering is just heating up the metal
glue and letting it cool on both
components holding them to the board.
Ok lets break up and solder our
badges!
Programmers?
Definition
a person who writes computer programs; a
person who programs a device, especially
a computer.
Are any of you programmers?


We are all programmers
Giving people directions is a form of
programming.
Languages
English
Java
C
Spanish

What can programming do?


The Internet



Video Games
What can programming do?


Machines



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

Opening a Drink
Step 1

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!

Lets learn another language
Computers dont speak Engl
Computers speak computer languages
C
Java
Pascal
.Net
Language is much smaller
Less words
Stricter rules
Who are we going to talk to?
Arduino
Small computer that we can change/modify


Our first Arduino program
Blink a light
Make sure everything is working
Learn about Digital Output
Learn some basics of the Arduino language


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 bulb
doesnt get damaged.


Our first Arduino program
With an Arduino
GND = - (negative)
PIN 13 = + (positive)
Pin 13 is a digital OUTPUT


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 will
be on.
When pin 13 is OFF electricty will not flow the light will be
off.

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

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?
Our first Arduino program
Turn light ON
Wait 1 second
Turn light off
REPEAT!
Blink Program
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)

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?
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 with
the light on pin 13?
Lets take 20 minutes and try! Work with your partner, if you get stuck raise
your hand for help!

Blink Program
Blink Program
Blink Program
Blink Program
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.
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
How do we attach a button?
RS232-TTL Trace
Note Inversion
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 cause
errors)
Runs under the main run loop

Use the Bus Pirate
Open terminal 119200-8-N-1
m
3
5
1
1
1
1
(2)

Make this Better?
Problems?
Uses Run Loop
Timing is inaccurate
Interrupts Effect loop (priority)
Suggestions?
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
Button Setup
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
Interrupts
On Trigger stops what it is doing
Runs ISR
Returns to normal execution after ISR
PIN 2,3 = Interrupt 0,1

Interrupts
ReAssigning Interrupts
Use attachInterrupt() again
Starting / Stopping Interrupts
noInterrupts()
interrupts().
Removing Interrupts
detachInterrupt(interrupt_number)
Can be used within an ISR!

Problems?
Hack this example
Can you make it toggle?
Add Debounce
Add another button on pin 3?
Make a two stage toggle require button
1 then button 2 pressed in order to toggle.
(out of order presses does nothing)

Timers
Three timers
Capture/Compare Registers
Dont use low level yet
TimerOne Library

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 PWM
outputs for digital pins 3 and 11.
While all the timers are used only Timer0
has assigned timer ISR. This means we
can hijack Timer1 / Timer2 for our uses.

TimerOne
initialize(period)
The minimum period or highest frequency
this 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 change
the attached interrupt and both pwm
outputs' frequencies and duty cycles
simultaneously.
TimerOne
pwm(pin, duty, period)Generates a
PWM waveform on the specified pin.
Output pins for Timer1 are PORTB pins 1
and 2, so you have to choose between
these two, anything else is ignored. On
Arduino, these are digital pins 9 and 10,
so those aliases also work.




TimerOne
attachInterrupt(function, period)Calls a
function at the specified interval in
microseconds.
setPwmDuty(pin, duty)A fast shortcut for
setting the pwm duty
detachInterrupt()Disables the attached
interrupt.
disablePwm(pin)Turns PWM off for the
specified pin so you can use that pin for
something else.

Vous aimerez peut-être aussi