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:14]
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 202: Line 214:
 [[http://​www.ladyada.net/​images/​arduino/​testswitchpullup.jpg|{{ ​ http://​www.ladyada.net/​images/​arduino/​testswitchpullup_t.jpg?​nolink&​500x318 ​ |}}]] [[http://​www.ladyada.net/​images/​arduino/​testswitchpullup.jpg|{{ ​ http://​www.ladyada.net/​images/​arduino/​testswitchpullup_t.jpg?​nolink&​500x318 ​ |}}]]
  
 +<class note>
 **Must a pullup/down resistor be 10KΩ?** **Must a pullup/down resistor be 10KΩ?**
  
-You'll notice that both the Arduino schematic, and the examples here use a 10KΩ resistor as the pullup or pulldown. Is there something special about 10KΩ? Nope! While, it is pretty much univerally used as the '​default'​ value, most of the time you can use as high as 100KΩ or as low as 4.7KΩ. Going much lower will waste more power, going higher may give you unstable results (due to microcontroller-internals that are not that important right now).  \\ I suggest going with 10KΩ because they are common resistor values and people reading your schematic will be more likely to understand that its 'just a pullup resistor'​.**Quick Quiz!**+You'll notice that both the Arduino schematic, and the examples here use a 10KΩ resistor as the pullup or pulldown. Is there something special about 10KΩ? Nope! While, it is pretty much univerally used as the '​default'​ value, most of the time you can use as high as 100KΩ or as low as 4.7KΩ. Going much lower will waste more power, going higher may give you unstable results (due to microcontroller-internals that are not that important right now).  \\ I suggest going with 10KΩ because they are common resistor values and people reading your schematic will be more likely to understand that its 'just a pullup resistor'​. 
 +</​class>​
  
 +**Quick Quiz!**
  
-  ***With the pull-down resistor configuration,​ what is the value read by digitalRead() when the button is pressed?** \\ //Highlight the text below to see the answer \\ //The returned value is 1 (HIGH)  +**With the pull-down resistor configuration,​ what is the value read by digitalRead() when the button is pressed?** \\ //Highlight the text below to see the answer \\ // 
-  ***With the pull-down resistor configuration,​ what is the value read by digitalRead() when the button is released?** \\ //Highlight the text below to see the answer ​ \\ //The returned value is 0 (LOW)             +<class white>The returned value is 1 (HIGH) ​</​class>​ 
-  ***With the pull-up resistor configuration,​ what is the value read by digitalRead() when the button is pressed?** \\ //Highlight the text below to see the answer ​ \\ //The returned value is 0 (LOW)             +**With the pull-down resistor configuration,​ what is the value read by digitalRead() when the button is released?** \\ //Highlight the text below to see the answer ​ \\ // 
-  ***With the pull-up resistor configuration,​ what is the value read by digitalRead() when the button is released?** \\ //Highlight the text below to see the answer ​ \\ //The returned value is 1 (HIGH)  +<class white>The returned value is 0 (LOW)   </​class> ​         
-  ***Lets say you wanted to design a switch so that when its pressed, the value read from the pin is 1, and when it's released the value is 0. Would you use a pull-up or pull-down resistor configuration?​** \\ //Highlight the text below to see the answer //         \\ You would want to use a pull-down resistor configuration.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)]]</li>[[http://​www.arduino.cc/​en/​uploads/​Main/​Arduino-Diecimila-schematic.pdf|{{ ​ http://​www.ladyada.net/​images/​arduino/​arduinoresetschem.jpg?​nolink&​242x285 ​ |}}]] \\ //Fig 5.7// +**With the pull-up resistor configuration,​ what is the value read by digitalRead() when the button is pressed?** \\ //Highlight the text below to see the answer ​ \\ // 
 +<class white>The returned value is 0 (LOW)    </​class> ​       ​ 
 +**With the pull-up resistor configuration,​ what is the value read by digitalRead() when the button is released?** \\ //Highlight the text below to see the answer ​ \\ // 
 +<class white>The returned value is 1 (HIGH) ​</​class>​ 
 +**Lets say you wanted to design a switch so that when its pressed, the value read from the pin is 1, and when it's released the value is 0. Would you use a pull-up or pull-down resistor configuration?​** \\ //Highlight the text below to see the answer //         ​\\ ​ 
 +<class white>You would want to use a pull-down resistor configuration.</​class>​ 
 + 
 + 
 +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) ​
  
  
-  ***Is this switch connected up with a pull-up or pull-down resistor? What value is the resistor? ** \\ //Highlight the text below to see the answer \\ //The resistor is a 10KΩ pull-up +**Is this switch connected up with a pull-up or pull-down resistor? What value is the resistor? ** \\ //Highlight the text below to see the answer \\ // 
-  *** The switch is called S1, look on your Arduino (you may have to remove the shield to see it) to identify S1. What is S1 used for?** \\ //Highlight the text below to see the answer// ​          \\ S1 is the button you press to reset the Arduino +<class white>The resistor is a 10KΩ pull-up</​class>​ 
-  *** Based on what S1 does and what you've learned about pullup/​pulldown resistors, describe what you think this circuitry does, and how the RESET pin works** \\ //Highlight the text below to see the answer \\ //Normally the RESET pin is pulled up to 5V. When the button is pressed, the pin is connected to ground. The Arduino microntroller resets itself when the RESET pin is connected to ground.  ​+** The switch is called S1, look on your Arduino (you may have to remove the shield to see it) to identify S1. What is S1 used for?** \\ //Highlight the text below to see the answer// ​          ​\\ ​ 
 +<class white>S1 is the button you press to reset the Arduino</​class>​ 
 +** Based on what S1 does and what you've learned about pullup/​pulldown resistors, describe what you think this circuitry does, and how the RESET pin works** \\ //Highlight the text below to see the answer \\ // 
 +<class white>Normally the RESET pin is pulled up to 5V. When the button is pressed, the pin is connected to ground. The Arduino microntroller resets itself when the RESET pin is connected to ground.  ​</​class>​ 
 ==== Iffy statements ​ ==== ==== Iffy statements ​ ====
  
Line 228: Line 256:
 Which we can rephrase more specifically as Which we can rephrase more specifically as
  
-**__If__the ​button is pressed, turn on the LED. \\ ****__If__the ​button is not pressed, turn off the LED.**+**__If__ the button is pressed, turn on the LED. \\ ****__If__ the button is not pressed, turn off the LED.**
  
 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 278: Line 307:
  
  
-^Some Conditional Tests...^+^Some Conditional Tests...^||
 ^Symbol^Definition^Usage Example^ ^Symbol^Definition^Usage Example^
-|**==**|Equality test^| +|**==**|Equality test| if (foo == 5) {  \\ Serial.print("​Foo is equal to 5"); \\  }\\Make sure you don't confuse this for the **assignment operator = **!| 
- +|!=|Inequality test |if (digitalRead(buttonPin) != LOW) {  \\ Serial.print("​The button pin is not LOW "); \\ }      ​|
-if (foo == 5) {  \\ Serial.print("​Foo is equal to 5"); \\  } +
- +
- +
- +
-Make sure you don't confuse this for the **assignment operator = **! +
- +
- +
-|!=      |Inequality test |if (digitalRead(buttonPin) != LOW) {  \\ Serial.print("​The button pin is not LOW "); \\ }      ​+
 |>​|Greater-than test |if ( var2 > 10 ) {  \\ Serial.print("​Variable #2 is larger than 10"); \\ }| |>​|Greater-than test |if ( var2 > 10 ) {  \\ Serial.print("​Variable #2 is larger than 10"); \\ }|
 |<​|Smaller-than test |if ( chickenstock < 10 ) {  \\ Serial.print("​We have less than 10 chickens in stock"​);​ \\ } | |<​|Smaller-than test |if ( chickenstock < 10 ) {  \\ Serial.print("​We have less than 10 chickens in stock"​);​ \\ } |
 |<​=|Smaller-than-or-equal-to test |if ( 20 <= yearstolive ) { \\ Serial.print("​Good news, you  have at least 20 years left!"​);​ \\ }| |<​=|Smaller-than-or-equal-to test |if ( 20 <= yearstolive ) { \\ Serial.print("​Good news, you  have at least 20 years left!"​);​ \\ }|
 |>​=|Greater-than-or-equal-to test |if ( kitten() >= 6 ) { \\ Serial.print("​The kitten() procedure returned a number larger than or equal to 6"); \\ }| |>​=|Greater-than-or-equal-to test |if ( kitten() >= 6 ) { \\ Serial.print("​The kitten() procedure returned a number larger than or equal to 6"); \\ }|
 +
 +
 **Quick Quiz!** **Quick Quiz!**
  
  
-  ***Modify the sketch so that it does the opposite, when the button is pressed the LED turns off and when it is released it turns on. Remember to change the sketch only, use the same circuitry! ​\\ **//​Highlight the text below to see the answer// ​ \\ Swap the lines digitalWrite(ledPin,​ HIGH); and digitalWrite(ledPin,​ LOW);  +**Modify the sketch so that it does the opposite, when the button is pressed the LED turns off and when it is released it turns on. Remember to change the sketch only, use the same circuitry! ** \\ //Highlight the text below to see the answer// ​ \\  
-  ​* ​**Modify the sketch so that the LED blinks 5 times a second (100ms on and 100ms off) when the button is pressed and is completely off when the button is released.** \\ //Highlight the text below to see the answer// ​ \\ <​code ​C>int ledPin = 12;                // LED is connected to pin 12+<class white>Swap the lines digitalWrite(ledPin,​ HIGH); and digitalWrite(ledPin,​ LOW); </​class>​ 
 +**Modify the sketch so that the LED blinks 5 times a second (100ms on and 100ms off) when the button is pressed and is completely off when the button is released.** \\ //Highlight the text below to see the answer// ​ \\  
 +<class white> 
 +<code >int ledPin = 12;                // LED is connected to pin 12
 int switchPin = 2;              // switch is connected to pin 2 int switchPin = 2;              // switch is connected to pin 2
 int val;                    // variable for reading the pin status int val;                    // variable for reading the pin status
Line 315: Line 341:
     delay(100);     delay(100);
   }   }
-}</​code>​**Notethat you don't need to do anything if the switchPin is HIGH because at the end of the "val == LOW" statements the LED has been turned off! **+}</​code>​ 
 +</​class>​ 
 +**Notethat you don't need to do anything if the switchPin is HIGH because at the end of the "val == LOW" statements the LED has been turned off! ** 
  
 **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 325: Line 355:
 Here is one possible solution sketch: Here is one possible solution sketch:
  
-<​code ​C>+<class white> 
 +<​code>​
 /* /*
  ​* ​ Switch and 2 LED test program  ​* ​ Switch and 2 LED test program
Line 354: Line 385:
   }   }
 }</​code>  ​ }</​code>  ​
 +</​class>​
  
 ==== Do it...or else!  ==== ==== Do it...or else!  ====
Line 443: Line 474:
 </​code>​ </​code>​
  
-Finally, we make sure that we've updated the button state variable with the current state. ​   **Quick Quiz!    ** +Finally, we make sure that we've updated the button state variable with the current state. ​   ​ 
-  ***Remove (or comment out) the line that says "​buttonState = val;" from the sketch and re-upload it to the Arduino. ​ \\ What happens now?  \\ **//Highlight the text below to see the answer// \\ When the button is held down, the Arduino prints out "​Button just pressed"​ over and over again. When its released, nothing is printed + 
-  ​* ​**Why does this happen? Go through the sketch, keeping track of what ****buttonState and val are storing at each line.** \\ //Highlight the text below to see the answer \\ //When the Arduino starts up, it sets buttonState to LOW (assuming the button isn't pressed as it is reset). Whenever the button pin is read as HIGH the (val != buttonState) test is true and it prints out a message. The buttonState is never set to HIGH so it never prints "​Button is released"​ and it always passes the (val != buttonState) test  ​+**Quick Quiz!    ** 
 + 
 +**Remove (or comment out) the line that says "​buttonState = val;" from the sketch and re-upload it to the Arduino. ​ \\ What happens now? ** \\ //Highlight the text below to see the answer// \\  
 +<class white>When the button is held down, the Arduino prints out "​Button just pressed"​ over and over again. When its released, nothing is printed</​class>​ 
 +**Why does this happen? Go through the sketch, keeping track of what buttonState and val are storing at each line.** \\ //Highlight the text below to see the answer \\ // 
 +<class white>When the Arduino starts up, it sets buttonState to LOW (assuming the button isn't pressed as it is reset). Whenever the button pin is read as HIGH the (val != buttonState) test is true and it prints out a message. The buttonState is never set to HIGH so it never prints "​Button is released"​ and it always passes the (val != buttonState) test  ​</​class>​ 
 ==== Counting presses ​ ==== ==== Counting presses ​ ====
  
Line 476: 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 511: 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 542: 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 554: 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 614: Line 660:
            
  
 +</​class>​
  
 ==== Brooklyn Debounce ​ ==== ==== Brooklyn Debounce ​ ====
Line 628: 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 824: 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 838: 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 902: 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 919: 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 930: 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 1043: 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.1287677690.txt.gz · Last modified: 2016/01/28 18:05 (external edit)