It then compares the button's state to its state the last time through the main loop. The sketch below continually reads the button's state. That's why you need a pull-down resistor in the circuit. It will more or less randomly return either HIGH or LOW. This is because the input is "floating" - that is, not connected to either voltage or ground. If you disconnect the digital I/O pin from everything, the LED may blink erratically. (The pin is still connected to ground, but the resistor resists the flow of current, so the path of least resistance is to +5V.) When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to voltage, so that we read a HIGH. When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. The third connects to a digital I/O pin (here pin 2) which reads the button's state. The second goes from the corresponding leg of the pushbutton to the 5 volt supply. The first goes from one leg of the pushbutton through a pull-down resistor (here 10k ohm) to ground. Hardware RequiredĬonnect three wires to the board. In this tutorial we learn how to check the state change, we send a message to the Serial Monitor with the relevant information and we count four state changes to turn on and off an LED. This is called state change detection or edge detection. To do this, you need to know when the button changes state from off to on, and count how many times this change of state happens. Once you've got a pushbutton working, you often want to do some action based on how many times the button is pushed.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |