Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* A digital push button switch
- Push button toggle switch code with debounce time for Lilypad
- Takes a push button switch as input in an LED in a circuit.
- Each press of the button toggles the LED state ON or OFF.
- Debounce code ignores mechanical noise in the switch producing a
- clean ON or OFF state.
- Written by Orwell Kowalyshyn for theMakery.space
- Portions of code from Arduino.cc
- */
- const int buttonPin = 5; //define the push button and LED pin ports as constants
- const int ledPin = 6;
- int reading; //variable for switch state - open is HIGH, closed is LOW
- int state = HIGH; //pin is turned on to start
- int debounce = 300; //200 millisecond debounce time
- long timing = 0; //set program running time to 0, 32-bit size
- void setup() {
- pinMode(buttonPin,INPUT_PULLUP); //enable input pullup resistor on pin, default to high impedence state
- pinMode(ledPin, OUTPUT); //set output for LED
- digitalWrite(ledPin,state); //turn LED on by default
- }
- void loop() {
- reading = digitalRead(buttonPin); //get state of the button, HIGH (open) or LOW (closed)
- if (reading == LOW && state == LOW && (millis()-timing > debounce)) { //button action close, and LED is off, and > debounce time
- state = HIGH; //turn on LED
- timing = millis(); //mark time when state changed
- }
- else if (reading == LOW && state == HIGH && (millis()-timing > debounce)) { //button closed and LED is on, and > debounce time
- state = LOW; //turn off LED
- timing = millis(); //mark time when state change
- }
- digitalWrite(ledPin,state); //set LED state
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement