Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int button = 12;
- const int led = 5;
- boolean lastbutton = false;
- boolean currentbutton = false;
- boolean ledOn = false;
- void setup()
- {
- pinMode (button, INPUT);
- pinMode (led, OUTPUT);
- digitalWrite (led, HIGH);
- delay (5);
- Serial.begin (9600);
- }
- boolean debounce (boolean lastbutton)
- {
- boolean currentbutton = digitalRead (button);
- if (lastbutton != currentbutton)//if lastbutton is different than currentbutton then delay for 5 milliseconds and read the currentbutton
- {
- delay (5);
- currentbutton = digitalRead (button);
- }
- return currentbutton;
- }
- void loop()
- {
- currentbutton = debounce (lastbutton);//this makes the button act like a switch, so you don't have to keep your finger on the push button to turn on the LED
- if (currentbutton == true && lastbutton ==false)//if they are opposite
- {
- ledOn =! ledOn;//do the opposite of the LED therefore turning it on or off
- }
- digitalWrite (led, ledOn);
- lastbutton = currentbutton;//changed the lastbutton to match the currentbutton so if the currentbutton was false lastbutton will now turn to false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement