User Tools

Site Tools


tutorials:learn:arduino:lesson5.html

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorials:learn:arduino:lesson5.html [2010/10/21 16:28]
daigo
tutorials:learn:arduino:lesson5.html [2016/01/28 18:05] (current)
Line 28: Line 28:
  
 {{  http://​www.ladyada.net/​images/​arduino/​lightswitch.gif?​nolink&​628x216 ​ |}} {{  http://​www.ladyada.net/​images/​arduino/​lightswitch.gif?​nolink&​628x216 ​ |}}
 +\\
 +<class center>​On the left, the switch is **open** and no current flows. On the right, the switch is **closed**, current flows and the light turns on. </​class>​
  
-On the left, the switch is **open** and no current flows. On the right, the switch is **closed**, current flows and the light turns on.  +{{  http://​www.ladyada.net/​images/​arduino/​Light_switch_inside.jpg?​nolink&​182x290 ​ |}}  
- +<class center> ​(thanks wikipedia!) ​   ​</​class>​
-{{  http://​www.ladyada.net/​images/​arduino/​Light_switch_inside.jpg?​nolink&​182x290 ​ |}} \\ (thanks wikipedia!) ​   ​+
  
 In this photo, you can see the internals of a light switch. The two wires connect to the top and bottom. The flat bar that goes verically down the middle is what is physically moved to connect or disconnect. In this photo, you can see the internals of a light switch. The two wires connect to the top and bottom. The flat bar that goes verically down the middle is what is physically moved to connect or disconnect.
Line 55: Line 56:
  
   ***Find 5 things around the house that have switches. Whats the average number of switches per device? **    ***Find 5 things around the house that have switches. Whats the average number of switches per device? ** 
 +
 ==== Light switch ​ ==== ==== Light switch ​ ====
  
Line 60: Line 62:
 We're going to make our first test of the pushbutton by having it turn on and off an LED light We're going to make our first test of the pushbutton by having it turn on and off an LED light
  
-{{  http://​www.ladyada.net/​images/​arduino/​switch1.png?​nolink&​115x272 ​ |}}  ​\\ //Fig 5.1 You'll note that the schematic symbol for a pushbutton switch ​ is a little bit different than the one above//+{{  http://​www.ladyada.net/​images/​arduino/​switch1.png?​nolink&​115x272 ​ |}}  ​ 
 +<class center> ​//Fig 5.1 You'll note that the schematic symbol for a pushbutton switch ​ is a little bit different than the one above//</​class>​
  
 Get out your red LED and 1.0KΩ resistor, as well as the tiny pushbutton and build the schematic onto your protoshield:​ Get out your red LED and 1.0KΩ resistor, as well as the tiny pushbutton and build the schematic onto your protoshield:​
Line 135: Line 138:
 Serial.println(var); ​              // print out the value stored in var</​code>​Now use a wire to alternate between connecting **Pin 2** to 5V and Ground through a 100Ω resistor, and watch the serial monitor. ​ Serial.println(var); ​              // print out the value stored in var</​code>​Now use a wire to alternate between connecting **Pin 2** to 5V and Ground through a 100Ω resistor, and watch the serial monitor. ​
  
-{{  http://​www.ladyada.net/​images/​arduino/​basicswitch.png?​nolink&​294x182 ​ |}} \\ //Fig 5.2//+{{  http://​www.ladyada.net/​images/​arduino/​basicswitch.png?​nolink&​294x182 ​ |}}  
 +<class center> ​//Fig 5.2//</​class>​
  
-//[[http://​www.ladyada.net/​images/​arduino/​tiehigh.jpg|{{ http://​www.ladyada.net/​images/​arduino/​tiehigh_t.jpg?​nolink&​500x306 |}}]]  ​\\ ​Switch input tied HIGH (5v) //+[[http://​www.ladyada.net/​images/​arduino/​tiehigh.jpg|{{ http://​www.ladyada.net/​images/​arduino/​tiehigh_t.jpg?​nolink&​500x306 |}}]] 
 +<class center>//​Switch input tied HIGH (5v) //</​class>​
  
-// \\ [[http://​www.ladyada.net/​images/​arduino/​tielow.jpg|{{ http://​www.ladyada.net/​images/​arduino/​tielow_t.jpg?​nolink&​500x307 |}}]]  ​\\ ​Switch input tied LOW (ground) ​   //+[[http://​www.ladyada.net/​images/​arduino/​tielow.jpg|{{ http://​www.ladyada.net/​images/​arduino/​tielow_t.jpg?​nolink&​500x307 |}}]] 
 +<class center> //Switch input tied LOW (ground) ​   //</​class>​
  
  
Line 166: Line 172:
  
 ^{{ http://​www.ladyada.net/​images/​arduino/​nopulldown.png?​nolink&​184x227 |}}^{{ http://​www.ladyada.net/​images/​arduino/​nopullup.png?​nolink&​184x197 |}}^ ^{{ http://​www.ladyada.net/​images/​arduino/​nopulldown.png?​nolink&​184x227 |}}^{{ http://​www.ladyada.net/​images/​arduino/​nopullup.png?​nolink&​184x197 |}}^
-//Fig 5.3Our two alternative switch wiring possibilities //For example, in these schematics we can connect and disconnect **pin 2** to 5V, or  we can connect and disconnect **pin 2** to ground. In both cases, as long as the button is held down, the pin is connected to a **valid input ** voltage. When the button is released, though, **pin 2** is not connected to //​anything//​. This is called a **floating input** voltage. Basically, it's invalid input!+<class center>//Fig 5.3Our two alternative switch wiring possibilities //</​class>​ 
 +For example, in these schematics we can connect and disconnect **pin 2** to 5V, or  we can connect and disconnect **pin 2** to ground. In both cases, as long as the button is held down, the pin is connected to a **valid input ** voltage. When the button is released, though, **pin 2** is not connected to //​anything//​. This is called a **floating input** voltage. Basically, it's invalid input!
  
 Try building up one of these schematics, and trying out the switch testing sketch above. When the button is held down you should definately get the right printout. When its released, it may keep the old value, or it may change, but its certainly not reliable! Try building up one of these schematics, and trying out the switch testing sketch above. When the button is held down you should definately get the right printout. When its released, it may keep the old value, or it may change, but its certainly not reliable!
  
-[[http://​www.ladyada.net/​images/​arduino/​testswitchnopulldown.jpg|{{ ​ http://​www.ladyada.net/​images/​arduino/​testswitchnopulldown_t.jpg?​nolink&​500x311 ​ |}}]] \\ //Wiring when the switch is connected to 5V //+[[http://​www.ladyada.net/​images/​arduino/​testswitchnopulldown.jpg|{{ ​ http://​www.ladyada.net/​images/​arduino/​testswitchnopulldown_t.jpg?​nolink&​500x311 ​ |}}]]  
 +<class center> ​//Wiring when the switch is connected to 5V //</​class>​
  
-[[http://​www.ladyada.net/​images/​arduino/​testswitchnopullup.jpg|{{ ​ http://​www.ladyada.net/​images/​arduino/​testswitchnopullup_t.jpg?​nolink&​500x305 ​ |}}]] \\ //Wiring when switch is connected to ground//+[[http://​www.ladyada.net/​images/​arduino/​testswitchnopullup.jpg|{{ ​ http://​www.ladyada.net/​images/​arduino/​testswitchnopullup_t.jpg?​nolink&​500x305 ​ |}}]]  
 +<class center> ​//Wiring when switch is connected to ground//</​class>​
  
 One solution is to get a switch that alternates connections,​ like this one, diagrammed here. One solution is to get a switch that alternates connections,​ like this one, diagrammed here.
  
-{{  http://​www.ladyada.net/​images/​arduino/​simplespdt.png?​nolink&​200x242 ​ |}}  ​\\ ​//Fig 5.4// +{{  http://​www.ladyada.net/​images/​arduino/​simplespdt.png?​nolink&​200x242 ​ |}}  
 + <​class center>//Fig 5.4//</​class> ​
  
 The problem is, these switches are suprisingly complex and 10 times more expensive than a little tactile button! Instead we use a trick called a **pull-down** resistor. The problem is, these switches are suprisingly complex and 10 times more expensive than a little tactile button! Instead we use a trick called a **pull-down** resistor.
  
-{{  http://​www.ladyada.net/​images/​arduino/​pulldown.png?​nolink&​155x302 ​ |}} \\ //Fig 5.5 //+{{  http://​www.ladyada.net/​images/​arduino/​pulldown.png?​nolink&​155x302 ​ |}} 
 +<class center>//Fig 5.5 //</​class>​
  
 The pull-down resistor here is the 10K resistor. When the switch is held down, the 100Ω resistor is connected directly to 5V. When the switch is released, the 100Ω resistor is connected to the 10K resistor which **pulls it down** to ground. ​ The pull-down resistor here is the 10K resistor. When the switch is held down, the 100Ω resistor is connected directly to 5V. When the switch is released, the 100Ω resistor is connected to the 10K resistor which **pulls it down** to ground. ​
Line 194: Line 205:
 You can also use the switch to connect the input to ground, and use a resistor as a **pull-up** resistor. You can also use the switch to connect the input to ground, and use a resistor as a **pull-up** resistor.
  
-{{  http://​www.ladyada.net/​images/​arduino/​pullup.png?​nolink&​155x302 ​ |}} \\ //Fig 5.6// +{{  http://​www.ladyada.net/​images/​arduino/​pullup.png?​nolink&​155x302 ​ |}}  
 +<class center> ​//Fig 5.6// </​class>​
  
 Try this schematic as well, and verify for yourself that the button is now reliable. Try this schematic as well, and verify for yourself that the button is now reliable.
Line 222: Line 234:
  
  
-Here is a small part of the Arduino schematic, [[http://​www.arduino.cc/​en/​uploads/​Main/​Arduino-Diecimila-schematic.pdf|(you can see the whole thing here)]][[http://​www.arduino.cc/​en/​uploads/​Main/​Arduino-Diecimila-schematic.pdf|{{ ​ http://​www.ladyada.net/​images/​arduino/​arduinoresetschem.jpg?​nolink&​242x285 ​ |}}]] \\ //Fig 5.7// +Here is a small part of the Arduino schematic, [[http://​www.arduino.cc/​en/​uploads/​Main/​Arduino-Diecimila-schematic.pdf|(you can see the whole thing here)]][[http://​www.arduino.cc/​en/​uploads/​Main/​Arduino-Diecimila-schematic.pdf|{{ ​ http://​www.ladyada.net/​images/​arduino/​arduinoresetschem.jpg?​nolink&​242x285 ​ |}}]]  
 +<class center> ​//Fig 5.7// </​class>​
  
 There is a switch and a resistor (Europeans use a rectangle instead of a squiggly for resistors), they are both connected to a pin on the Arduino microcontroller called RESET (in the bottom right corner) ​ There is a switch and a resistor (Europeans use a rectangle instead of a squiggly for resistors), they are both connected to a pin on the Arduino microcontroller called RESET (in the bottom right corner) ​
Line 247: Line 260:
 Here is how we will wire up the switch and LED.  Here is how we will wire up the switch and LED. 
  
-{{  http://​www.ladyada.net/​images/​arduino/​digitallight.png?​nolink&​289x272 ​ |}} \\ Fig 5.8 +{{  http://​www.ladyada.net/​images/​arduino/​digitallight.png?​nolink&​289x272 ​ |}}  
 +<class center>Fig 5.8 </​class>​
  
 Build this schematic on your protoshield Build this schematic on your protoshield
Line 334: Line 348:
 **Now its your turn:** add another red LED and resistor to pin 11, modify the sketch so that when the button is pressed one LED is lit and the other one is off and when the button is released the first LED is off and the second ​ LED is lit. **Now its your turn:** add another red LED and resistor to pin 11, modify the sketch so that when the button is pressed one LED is lit and the other one is off and when the button is released the first LED is off and the second ​ LED is lit.
  
-{{  http://​www.ladyada.net/​images/​arduino/​2ledswitch.png?​nolink&​409x272 ​ |}} \\ //Fig 5.9//+{{  http://​www.ladyada.net/​images/​arduino/​2ledswitch.png?​nolink&​409x272 ​ |}}  
 +<class center>//Fig 5.9//</​class>​
  
 Try to wire up the protoshield just from the schematic. If you're having trouble, [[http://​www.ladyada.net/​images/​arduino/​2ledswitch.jpg|click here for a photo of the parts wired up]]. Try to wire up the protoshield just from the schematic. If you're having trouble, [[http://​www.ladyada.net/​images/​arduino/​2ledswitch.jpg|click here for a photo of the parts wired up]].
Line 498: Line 513:
 }</​code>​ }</​code>​
  
-We've added one new thing in this sketch, which is the ++ operator. ​ Simply, the statement "​buttonPresses++"​ **increments** (adds 1 to) the buttonPresses variable.This is a shortcut for "​buttonPresses = buttonPresses + 1"​. ​   **Quick Quiz! ** +We've added one new thing in this sketch, which is the ++ operator. ​ Simply, the statement "​buttonPresses++"​ **increments** (adds 1 to) the buttonPresses variable.This is a shortcut for "​buttonPresses = buttonPresses + 1"​. ​   ​ 
-  ***Modify the sketch so that message is only printed when the button is released, not when it's pressed. ​\\ **//​Highlight the text below to see the answer \\ //Change the "val == LOW" test to "val == HIGH"  + 
-  ***Modify the sketch so its a countdown device! \\ Step 1. **Have the buttonPresses variable start at 10. ** \\ Step 2. **Every time the button is pressed, //​decrement//​ the buttonPresses variable (use the -- operator, which does the opposite of ++). ** \\ Step 3. **Once you have that working, have the Arduino print out "We have //x// presses to go till takeoff!"​ where x is the number of presses remaining, but only if the number of presses left is larger than 0 (check the conditional test table above to see how to test if a variable is larger than a number)** \\ Step 4. **Once you have that working, make the Arduino print out "​EXPLODE!"​ on the last button press.** \\  \\ {{ http://​www.ladyada.net/​images/​arduino/​takeoff.jpg?​nolink&​676x249 |}} \\ **//​Highlight the text below to see one possible answer \\ //<​code ​C>/*+**Quick Quiz! ** 
 + 
 +**Modify the sketch so that message is only printed when the button is released, not when it's pressed. **\\ //Highlight the text below to see the answer \\ // 
 +<class white>Change the "val == LOW" test to "val == HIGH" ​</​class>​ 
 +**Modify the sketch so its a countdown device! \\ Step 1. **Have the buttonPresses variable start at 10. ** \\ Step 2. **Every time the button is pressed, //​decrement//​ the buttonPresses variable (use the -- operator, which does the opposite of ++). ** \\ Step 3. **Once you have that working, have the Arduino print out "We have //x// presses to go till takeoff!"​ where x is the number of presses remaining, but only if the number of presses left is larger than 0 (check the conditional test table above to see how to test if a variable is larger than a number)** \\ Step 4. **Once you have that working, make the Arduino print out "​EXPLODE!"​ on the last button press.** \\  \\ {{ http://​www.ladyada.net/​images/​arduino/​takeoff.jpg?​nolink&​676x249 |}} \\ **//​Highlight the text below to see one possible answer \\ // 
 +<class white> 
 +<​code>/​*
  ​* ​ Takeoff!  ​* ​ Takeoff!
  */  */
Line 533: Line 554:
     buttonState = val;            // save the new state in our variable     buttonState = val;            // save the new state in our variable
   }   }
-}</​code>/​/      // +}</​code>​ 
 + 
 +</class> 
 ==== Design challenge, part 1  ==== ==== Design challenge, part 1  ====
  
Line 564: Line 588:
  
  
- +{{bliptv>463022}}
- <​img title="​Click to play" alt="​Video thumbnail. Click to play." src="​http://​blip.tv/​file/​get/​Ladyada-bikeLight1699.flv.jpg"​ border="​0"​ title="​Click To Play" /\\ Click To Play play_blip_movie_463022();​ +
  
  
Line 576: Line 599:
 Here is one possible solution sketch: ​ \\  Here is one possible solution sketch: ​ \\ 
  
 +<class white>
 <code C>/* <code C>/*
  ​* ​ Bike light, revision 1  ​* ​ Bike light, revision 1
Line 636: Line 660:
            
  
 +</​class>​
  
 ==== Brooklyn Debounce ​ ==== ==== Brooklyn Debounce ​ ====
Line 650: Line 675:
 You play a little bit with the bike light prototype and find that, yes, this is a problem. (You should try it out, although depending on your button you may or may not see this problem occur, it is a sneaky bug) You play a little bit with the bike light prototype and find that, yes, this is a problem. (You should try it out, although depending on your button you may or may not see this problem occur, it is a sneaky bug)
  
- +{{bliptv>​463045}} 
- +
- <​img title="​Click to play" alt="​Video thumbnail. Click to play." src="​http://​blip.tv/​file/​get/​Ladyada-bouncing352.mp4.jpg"​ border="​0"​ title="​Click To Play" /> \\ Click To Play play_blip_movie_463045();​ +
  
  
Line 846: Line 870:
  
  
-  ***Does this sketch work correctly? ​ \\ **//Highlight the text below to see the answer//**// \\ //**No!  +**Does this sketch work correctly? ​** \\ //Highlight the text below to see the answer// \\  
-  ***What //does// it do?** \\ //Highlight the text below to see the answer//**////​** ​        \\ When the button is pressed to turn the light on, it only blinks once. +<class white>No!</​class> ​ 
-  ***Use println() procedure calls and your brain to try and figure out why the sketch acts this way.  \\ What happens just after the button is pressed to turn the light on?  \\ What happens the next time the loop() procedure runs? ** \\ //Highlight the text below to see the answer//**////​** ​ \\ When the button is just pressed, the **if** statement conditions are true and the light mode changes form 0 (off) to 1 (on).  \\ The LEDs are then turned on for 100ms and then off for 100ms. ​ \\ However, next time the loop() procedure runs, the **if** conditionals are false (the button state has not changed) and so the code that would blink the LED is not run again. Thus the single blink. ​+**What //does// it do?** \\ //Highlight the text below to see the answer// ​ \\  
 +<class white>When the button is pressed to turn the light on, it only blinks once.</​class>​ 
 +**Use println() procedure calls and your brain to try and figure out why the sketch acts this way.  \\ What happens just after the button is pressed to turn the light on?  \\ What happens the next time the loop() procedure runs? ** \\ //Highlight the text below to see the answer// ​ \\  
 +<class white>When the button is just pressed, the **if** statement conditions are true and the light mode changes form 0 (off) to 1 (on).  \\ The LEDs are then turned on for 100ms and then off for 100ms. ​ \\ However, next time the loop() procedure runs, the **if** conditionals are false (the button state has not changed) and so the code that would blink the LED is not run again. Thus the single blink. ​</​class>​
  
 The trick hereis that you want to //split up // your **loop()** procedure into two sections.The first section will do all the button checking and debouncing stuff. It will also determine whether the button state has changed and if so, it will change the **lightMode** variable appropriately. Once that dirty work has been done, the next section of code will examine the **light****Mode ** variable and then perform the correct actions for that mode. The trick hereis that you want to //split up // your **loop()** procedure into two sections.The first section will do all the button checking and debouncing stuff. It will also determine whether the button state has changed and if so, it will change the **lightMode** variable appropriately. Once that dirty work has been done, the next section of code will examine the **light****Mode ** variable and then perform the correct actions for that mode.
Line 860: Line 887:
 Here is one solution: Here is one solution:
  
-<​code ​C>/*+<class white> 
 +<​code>/​*
  ​* ​ Bike light, revision 3: blinky  ​* ​ Bike light, revision 3: blinky
  */  */
Line 924: Line 952:
   // If lightmode is 0, we dont have to do anything because the LEDs are already off!   // If lightmode is 0, we dont have to do anything because the LEDs are already off!
 }</​code>​ }</​code>​
 +</​class>​
    
  
Line 941: Line 969:
 The final design challenge is to take the sketch from Design Challenge 2 and upgrade it to have at least 4 modes: off, all-on, blinking LEDs, and '​wave'​. ​ The final design challenge is to take the sketch from Design Challenge 2 and upgrade it to have at least 4 modes: off, all-on, blinking LEDs, and '​wave'​. ​
  
- <​img title="​Click to play" alt="​Video thumbnail. Click to play." src="​http://​blip.tv/​file/​get/​Ladyada-bikeLiteFinal384.MP4.jpg"​ border="​0"​ title="​Click To Play" /\\ Click To Play play_blip_movie_466034();​  +{{bliptv>466034}} 
-  ***First, modify the first half of the sketch so that pressing the button cycles through all the modes, from mode 0 to mode 3. ** \\ Use **println()** procedure calls to verify that you are successfully changing between all of the modes \\  \\ **Need a Hint? \\ **//Highlight the text below to see a clue //** \\ ** With only two modes, you can use an **if-else** statement, but with more than two, you'll need to handle multiple possibilities. ​ \\ Turns out that just like you can nest **if** statements, you can also nest **if-else** statements! \\ <​code ​C>if ( condition1 ) { +  
 +**First, modify the first half of the sketch so that pressing the button cycles through all the modes, from mode 0 to mode 3. ** \\ Use **println()** procedure calls to verify that you are successfully changing between all of the modes \\  \\ **Need a Hint? **\\ 
 +//Highlight the text below to see a clue // \\   
 + 
 + 
 +<class white>With only two modes, you can use an **if-else** statement, but with more than two, you'll need to handle multiple possibilities. ​ \\ Turns out that just like you can nest **if** statements, you can also nest **if-else** statements! \\ <code >if ( condition1 ) { 
   do this;    do this; 
 } else {  } else { 
Line 952: Line 985:
      }      }
   }   }
-}</​code>​** \\ ** +}</​code>​ 
 +</​class>​ 
   ***After that is working, modify the second half of the sketch so that it performs the different effects for each mode. **   ***After that is working, modify the second half of the sketch so that it performs the different effects for each mode. **
  
 Here is an example of the finished project code  Here is an example of the finished project code 
  
-<​code ​C>/*+<class white> 
 +<​code>/​*
  ​* ​ Bike light, final version  ​* ​ Bike light, final version
  */  */
Line 1065: Line 1101:
 } }
 </​code>​ </​code>​
 +</​class>​
 **For extra credit**, come up with with some more flashy modes, and post your video to the [[http://​www.ladyada.net/​forums|forums ]] **For extra credit**, come up with with some more flashy modes, and post your video to the [[http://​www.ladyada.net/​forums|forums ]]
  
/home/ladyada/public_html/wiki/data/attic/tutorials/learn/arduino/lesson5.html.1287678523.txt.gz · Last modified: 2016/01/28 18:05 (external edit)