Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. int inPin = 2;         // the number of the input pin
  2. int outPin = 13;       // the number of the output pin
  3.  
  4. int state = HIGH;      // the current state of the output pin
  5. int reading;           // the current reading from the input pin
  6. int previous = LOW;    // the previous reading from the input pin
  7.  
  8. // the follow variables are long's because the time, measured in miliseconds,
  9. // will quickly become a bigger number than can be stored in an int.
  10. long time = 0;         // the last time the output pin was toggled
  11. long debounce = 200;   // the debounce time, increase if the output flickers
  12.  
  13. void setup()
  14. {
  15.   pinMode(inPin, INPUT);
  16.   pinMode(outPin, OUTPUT);
  17. }
  18.  
  19. void loop()
  20. {
  21.   reading = digitalRead(inPin);
  22.  
  23.   // if the input just went from LOW and HIGH and we've waited long enough
  24.   // to ignore any noise on the circuit, toggle the output pin and remember
  25.   // the time
  26.   if (reading == HIGH && previous == LOW && millis() - time > debounce) {
  27.     if (state == HIGH)
  28.       state = LOW;
  29.     else
  30.       state = HIGH;
  31.  
  32.     time = millis();    
  33.   }
  34.  
  35.   digitalWrite(outPin, state);
  36.  
  37.   previous = reading;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement