Advertisement
DragNfLy

Debounced Scmitt Trigger Switch

Jul 12th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. /* Debounced Switch Input
  2. */
  3.  
  4. const int buttonPin = 2;          // Button pin
  5. const int ledPin =  7;            // LED pin
  6.  
  7. int ledState = LOW;
  8.  
  9. // variables will change:
  10. volatile int buttonState = 0;     // Pushbutton Status Variable
  11.  
  12. unsigned long previousMillis = 0; // Store last time LED was updated
  13.  
  14. const long interval = 1000;
  15.  
  16. void setup() {
  17.   // initialize the LED pin as an output:
  18.   pinMode(ledPin, OUTPUT);
  19.   // initialize the pushbutton pin as an input:
  20.   pinMode(buttonPin, INPUT);
  21.   // Attach an interrupt to the ISR vector
  22.   attachInterrupt(0, pin_ISR, CHANGE);
  23. }
  24.  
  25. void loop() {
  26.   // Nothing here!
  27. }
  28.  
  29. void pin_ISR() {
  30.   unsigned long currentMillis = millis();
  31.  
  32.   if (currentMillis - previousMillis >= interval) {
  33.     // save the last time you blinked the LED
  34.     previousMillis = currentMillis;
  35.  
  36.     // if the LED is off turn it on and vice-versa:
  37.     if (ledState == LOW) {
  38.       ledState = HIGH;
  39.     } else {
  40.       ledState = LOW;
  41.     }
  42.  
  43.     // Set LED with the ledState of the variable
  44.     digitalWrite(ledPin, ledState);
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement