Upload
charla-bailey
View
222
Download
0
Embed Size (px)
Citation preview
switches & whiskers on the Arduino
living with the lab
lever arm switches mounted to Arduino
© 2012 David Hall
living with the lab
2
The content of this presentation is for informational purposes only and is intended only for students attending Louisiana Tech University.
The author of this information does not make any claims as to the validity or accuracy of the information or methods presented.
Any procedures demonstrated here are potentially dangerous and could result in injury or damage.
Louisiana Tech University and the State of Louisiana, their officers, employees, agents or volunteers, are not liable or responsible for any injuries, illness, damage or losses which may result from your using the materials or ideas, or from your performing the experiments or procedures depicted in this presentation.
If you do not agree, then do not view this content.
The copyright label, the Louisiana Tech logo, and the “living with the lab” identifier should not be removed from this presentation.
You may modify this work for your own purposes as long as attribution is clearly provided.
DISCLAIMER & USAGE
the switches
• long lever with three electrical terminals• can be wired as normally open (electricity flows only when switch pressed)• can be wired as normally closed (electricity stops flowing when switch pressed)• used with 0.183 inch quick disconnect terminals
3
living with the lab
4
living with the lab
wiringwhen the switch is not pressed, electricity can pass through the normally closed (NC) path
when the switch is pressed, electricity can pass through the normally open (NO) path
use a digital input to sense when switch is on or off
5
living with the lab
ANALOG
RESE
T3V
3
5V Gnd Vin 0 1 2 3 4 5
7 6 5 4 3 2 1 03 2 1 0 9 81 1 1 1AR
EFG
ND
DIGITAL
POWER
Arduino
10kW switch
switchstatus
voltage at pin 7
status ofdigital input 7
0V(no power applied)
5V(direct connection to power)
HIGH
LOW
closed(pressed)
open (not pressed)
digitalRead(7)
check status of pin 7 using . . .
6
living with the lab
why the 10kΩ resistor?
ANALOG
RESE
T3V
3
5V Gnd Vin 0 1 2 3 4 5
7 6 5 4 3 2 1 03 2 1 0 9 81 1 1 1AR
EFG
ND
DIGITAL
POWER
Arduino
10kW switch
using a large resistance limits the current so that we don’t waste electricity
𝑃=𝑉 ∙ 𝐼=𝑉 2
𝑅=
(5𝑉 )2
10,000Ω=0.0025W=2.5mW
. . . any resistor will do, but there’s no sense in wasting power
7
hooking things up on your robot
living with the lab
• space is tight, and it’s easy to break the quick disconnect terminals (can solder them if desired)
• connect wires to the common and normally open terminals on the switch
8
toolsa wire stripper and some sort of crimping tool are needed (can also crudely crimp using a vise)
living with the lab
9
completing the circuit on the breadboard
• to prevent wires from coming loose• to make it easier to see what’s going on (easier to troubleshoot)
living with the lab
try to keep the wiring tidy . . . violet leads go to +5V source
white and red leads provide digital input one 10kΩ resistor per switch is installed
10
programmingthis program causes the built-in LED on pin 13 to turn on when the switch is pressed
living with the lab
int whisker1=0;
void setup(){ pinMode(7,INPUT); pinMode(13,OUTPUT);}
void loop(){ whisker1=digitalRead(7); if(whisker1== HIGH) // whisker1==HIGH if whisker pressed { digitalWrite(13,HIGH); } // turn LED on pin13 on else { digitalWrite(13,LOW); } // turn LED on pin13 off}
this is our first time to use if … else control (see next slide)
11
if / elseif/else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together arduino.cc
living with the lab
if (x < 500) { // do Thing A } else if (x >= 1000) { // do Thing B } else { // do Thing C }
can use an unlimited number of else if blocks . . . or none, as in the program on the previous slide