Published:2013/8/14 21:13:00 Author:lynne | Keyword: Read the State of a Button with Arduino , Tutorial #3 | From:SeekIC
On the first line we set a variable pinButton with an integer value of 8 (the pin number on Arduino Board where we connect the button or switch). Then in the void() function we set the pin 8 as INPUT and initialize the serial port.
In the loop() we declare variable stateButton with the value obtained using the digitalRead() function that reads the state of pin 8. Then it displays its state in the Serial Monitor window as 0 or 1. There is also the delay() that ensures that we can actually read the value. A problem with the delay might be that if you press the button exactly when the delay happens (20ms in our case) then it will display the action in the windows, that why is better to use a lower value or else you might lose some clicks.
Reprinted Url Of This Article:
http://www.seekic.com/circuit_diagram/Basic_Circuit/Read_the_State_of_a_Button_with_Arduino_–_Tutorial_3.html
Print this Page | Comments | Reading(3)
Code: