Many items you use every day have pushbuttons. Cell phones, microwave ovens, TV remotes, and computer keyboards might all have pushbuttons.
Here we introduce a common pushbutton circuit and a program for monitoring it with your microcontroller. It also shows how to use a pushbutton's state to control LED light circuits. Keep in mind that lights are just one example of a device you can turn on and off with a microcontroller. Your invention might instead use pushbuttons to control circuits for motors, heating elements, or other devices.
This circuit uses the same P26 and P27 LEDs on the Propeller Activity Board or FLiP from Blink a Light [1], along with two pushbutton circuits you will build onto your breadboard.
(2) pushbuttons (#400-00002)
(2) 100 ohm resistors (brown-black-brown) or (2) 220 ohm resistors (red-red-brown)
(2) 10 k-ohm resistors (brown-black-orange)
If you don't have 100 ohm resistors, switch them out for 220 ohm resistors (red-red-brown) when setting up your circuit. The Activity Board diagram shows 100 ohm resistors, the FLiP breadboard shows 220 ohm resistors.
Note: Use the schematics with any Propeller board that has a prototyping area. This activity will not work with the resistive touch-buttons on the Propeller QuickStart.
This example will display the state of the button connected to P3 in the SimpleIDE Terminal. It displays 1 if the button is pressed, or 0 if it is not pressed.
The pushbutton circuit: When the pushbutton is not pressed, the circuit applies GND (0 V) to the I/O pin. If the pushbutton is pressed, the circuit applies 3.3 V to the I/O pin, and a small amount of current passes from the 3.3 V connection, through the 10 kΩ resistor to ground. Setting P3 and P4 to input lets the I/O pins monitor the voltages on the pushbutton circuits while remaining invisible to them.
The code: The simpletools library’s input function returns 1 if the button is pressed, or 0 if it is not pressed. int button = input(3) copies the result to a variable named button. Then, pause(100) delays for 1/10 of a second before repeating the while(1) loop.
/* Button Display.c Displays the state of the P3 button in the SimpleIDE Terminal. 1 -> button pressed, 0 -> button not pressed. */ #include "simpletools.h" // Include simpletools int main() // main function { while(1) // Endless loop { int button = input(3); // P3 input -> button variable print("button = %d\n", button); // Display button state pause(100); // Wait 0.1 second before repeat } }
Remember if...else if... statements from Make Several Decisions [3]? You can add one of those statements to your program to make the light blink if the button is pressed, or stay off and just wait for 1/10 of a second before repeating if the button is not pressed.
Can you modify your program to make the LED connected to P27 blink when the button connected to P4 is pressed?
Links
[1] http://learn.parallax.com/propeller-c-simple-circuits/blink-light
[2] http://learn.parallax.com/reference/breadboard-basics
[3] https://learn.parallax.com/propeller-c-start-simple/make-several-decisions