Transcript Document
ProtoSnap Introduction to Arduino Casey Haskell, Pete Lewis, David Stillman, Jim Lindblom, Pete Dokter, Lindsay Levkoff, Trevor Zylstra
Overview of Class
History of Arduino/SFE What is the Protosnap?:
• Concept, design, hardware
Intro to Coding:
• Arduino software
Example Sketches:
• Digital Output, Digital Input • RGB LEDs • Analog Input, Analog Output
SparkFun Electronics
Sharing Ingenuity
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) Team Arduino: (Left to right) David Cuartielles, Massimo Banzi & Gianluca Martino
Numbers on the tabs indicate how they are pre wired to the arduino mini.
Arduino Mini
The Arduino Environment
The Environment
Parts of the Sketch
Board Type
Serial Port / COM Port
Digital Output • To Output a Digital signal (On or Off) use this code:
digitalWrite ( pinNumber , value );
• Where value is
HIGH
or
LOW
• Where pinNumber is the pin you want effect.
Now let's upload “Blink.pde”
Declaring Variables • Boolean:
boolean variableName;
• Integer:
int variableName;
• Character:
char variableName;
• String:
stringName [ ];
Assigning Variables • Boolean:
variableName = true;
or
variableName = false;
• Integer:
variableName = 32767;
or
variableName = -32768;
• Character:
variableName = ‘A’;
or
stringName = “SparkFun”;
Now let's add variables to “Blink.pde” (and for fun, let's try changing the delay time)
Digital Input • Useful when reading a button.
*The protosnap has a button pre-wired to pin 7.
• Add this to your setup:
pinMode ( 7, INPUT );
• Use this in your loop:
digitalRead ( 7 );
• Digital Input values are
HIGH
or
LOW
Button Schematic
Internal Pullup Resistors
Add this to your setup:
digitalWrite ( 7, HIGH );
This means...
your button will read HIGH when it is not pushed and LOW when it is pushed
If Statements
if ( this is true ) { do this; }
If
if ( this is true ) { do this; }
Conditional
if ( this is true ) { do this; }
Action
if ( this is true ) { do this; }
Else
else { do this; }
Now let's upload “Button.pde”
RGB LEDs are different...
To turn these LEDs on...
digitalWrite(3, LOW);
Analog Input • Useful to read a sensor that is not simply ON/OFF. (i.e. something like brightness, direction or speed)
*The protosnap has a light sensor wired to Pin A0.
• Use this in your loop:
analogRead ( A0 );
• Analog Input varies from 0 to 1023 on an Arduino
Before we use analogRead(), let's learn about Serial Communication.
This will be very useful to understand analogRead(); It will allow us to stream the values from the light sensor and watch the data change in real time.
Serial in Setup
Serial Communication: Serial Setup
void setup ( ) { pinMode ( 5, OUTPUT ); pinMode ( 6, OUTPUT ); pinMode ( 7, OUTPUT ); } Serial.begin ( 9600 ) ;
In this case the number 9600 is the baudrate at which the computer and Arduino communicate
Serial Monitor
Serial Communication: Sending a Message
void loop ( ) { //read the value from the sensor: sensorValue = analogRead (sensorPin); Serial.println(sensorValue); if(sensorValue < 90){ digitalWrite(5, LOW); } //code continues after this
Serial Communication
Now let's upload “Light_data_stream.pde”
Now let's do something with the light values!
Using “if statements” we can turn on different RGB LEDs when there is different amounts of light in the room.
Let's upload AnalogInput_Multi_Threshold.pde
Analog Output
Output is always Digital but… • To output a signal that pretends to be Analog use this code:
analogWrite ( pinNumber, value );
• Where value is a number 0 - 255 • Where pinNumber is the pin you want to effect.
Analog 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
* like setting an LEDs brightness • P.W.M. is available on Arduino pins # 3, 5, 6, 9, 10, and 11
Analog 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% 300 250 200 150 100 50 0 0 25 50 PWM Percentage 75 P.W.M. Signal @ 75% 100 6 5 4 3 2 1 0 0 P.W.M. Signal rising 25 50 PWM Percentage 75 100
Now let's upload “Analog_Blink.pde”
Basic Repetition
for (int count = 0; count<10; count++) { //for action code goes here //this could be anything }
Basic Repetition
for (int count = 0; count<10; count++) { //for action code goes here }
Basic Repetition
for (int count = 0; count<10; count++) { //for action code goes here }
Basic Repetition
for ( int count = 0; count<10; count++) { //for action code goes here }
Basic Repetition
for (int count = 0; count<10; count++) { //for action code goes here }
Basic Repetition
for (int count = 0; count<10; count++ ) { //for action code goes here }
Basic Repetition
for (int count = 0; count<10; count++) { //for action code goes here }
Now Let's try “Fading” an LED You can find it here...
Note, we need to change the variable “ledPin” to one of the RGB pins (5,6 or 3).
Oh, Snap! Personal Projects
• Dave’s Alarm System • Pete’s Guitar • Ben’s Project
Questions?
www.sparkfun.com
6175 Longbow Drive, Suite 200 Boulder, Colorado 80301