Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int buttonpin = 12;
- const int motorpin = 9;
- boolean lastbutton = false;
- boolean currentbutton = false;
- boolean motorOn = false;
- void setup()
- {
- pinMode (lastbutton, INPUT);
- pinMode (motorpin, OUTPUT);
- Serial.begin (9600);
- }
- boolean debounce (boolean lastbutton)
- {
- boolean currentbutton = digitalRead (buttonpin);
- if (lastbutton != currentbutton)//if lastbutton is different than currentbutton then delay for 5 milliseconds and read the currentbutton
- {
- delay (5);
- currentbutton = digitalRead (buttonpin);
- }
- return currentbutton;
- }
- void loop()
- {
- currentbutton = debounce (lastbutton);
- if (lastbutton == LOW && currentbutton == HIGH)
- {
- motorOn =! motorOn;
- }
- digitalWrite (motorpin, motorOn);
- lastbutton = currentbutton;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement