Push Button Experiments
After completing the Push Button project, test yourself by trying these experiments.
1 | Very often we need to make connections similar to the one used for the push button where an external resistor is needed as shown on the right. This resistor is referred to as a pull-down resistor because it is connected down to ground. | |||||||||||||||||||
To simplify the connections, an internal pull-down resistor is built-in to each of the digital pins on the Arduino so that an external resistor is not needed. | ||||||||||||||||||||
To use the internal pull-down resistor, change the | ||||||||||||||||||||
You can now remove the external 22K ohm resistor and the wire that connects the resistor to GND on the breadboard as shown on the right.
Run your modified program. You should get the same result. | ||||||||||||||||||||
2 | In addition to the internal pull-down resistor, each digital pin on the Arduino also has an interal pull-up resistor, i.e. the resistor is connected up to +5V instead of to GND. Because of this, the other end of the push button must be connected to GND instead of to +5V to make it work. | |||||||||||||||||||
To use the internal pull-up resistor, change the Run your modified program. You should get the opposit result, i.e., the LED turns off when you push the button. | ||||||||||||||||||||
3 | Instead of using the push button to control the internal LED connected to pin 13, make the push button to control an external LED that is connected to pin 3.
Do not change the push button connections. Just add the connections for an external LED like the picture on the right. Modify the code to control the external LED instead of the internal LED. Hint: change pin 13 to pin 3 in the code. | |||||||||||||||||||
4 | Add a buzzer to what you have from experiment 2. Make it so that when you press the push button the external LED will turn on and the buzzer will sound one note,
and when you release the button both of them will turn off.
The otherwise the second Notice the use of the open and close braces to bracket the body of the true and false parts of the |
5
| (Difficult) Write a program so that when you press the button once it will turn on a LED. The LED stays on even after you release the button.
When you press the button a second time, the LED will turn off. It will remain off until you press the button again. This is sometimes referred to as the push-on-push-off circuit.
|
|
6
| Create an electronic dice using the push button and the 7-segment LED display. Roll the die by pressing the button. The 7-segment LED display will show a random number between 1 and 6.
| You will need to use the A sample code segment is shown on the right. Make sure you add the displayDigit function code. What happens when you reduce the delay to a much smaller number such as 10? To solve this problem, you will have to do something similar to experiment 5 above.
|
7
| (Difficult) Write a program to count how many times a push button is pressed.
| Because the computer executes the commands so fast, even the short time that you press the button down, the computer will have executed the loop many many times.
|
8
| (Difficult) Write a program to distinquish between a short button press and a long button press.
A long press is one where you press for more than 700 millisecond.
|
Answer
| 9
| (Difficult) Write a program to play the Star Wars theme song (from Project 2) when a button is pressed,
and stops playing the song as soon as the button is released.
| You might start off with the code on the right, but it is not correct because when you release the button, the song does not stop playing immediately. The reason is while playing the song there are many long blocking delay commands. So that during all this time, the code does not check the status of the button, and not be able to stop the song as soon as the button is released.
|
|