ScienceGeyser

DebounceRelayControl

May 26th, 2021 (edited)
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Debounce
  3.  
  4.   Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  5.   press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
  6.   minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
  7.  
  8.   The circuit:
  9.   - LED attached from pin 13 to ground
  10.   - pushbutton attached from pin 2 to +5V
  11.   - 10 kilohm resistor attached from pin 3 to ground
  12.  
  13.   - Note: On most Arduino boards, there is already an LED on the board connected
  14.     to pin 13, so you don't need any extra components for this example.
  15.  
  16.   created 21 Nov 2006
  17.   by David A. Mellis
  18.   modified 30 Aug 2011
  19.   by Limor Fried
  20.   modified 28 Dec 2012
  21.   by Mike Walters
  22.   modified 30 Aug 2016
  23.   by Arturo Guadalupi
  24.  
  25.   This example code is in the public domain.
  26.  
  27.   http://www.arduino.cc/en/Tutorial/Debounce
  28. */
  29.  
  30. // constants won't change. They're used here to set pin numbers:
  31. const int buttonPin = 3;    // the number of the pushbutton pin
  32. const int ledPin = 13;      // the number of the LED pin
  33. const int relayPin = 4;
  34.  
  35. // Variables will change:
  36. int ledState = LOW;         // the current state of the output pin
  37. int buttonState;             // the current reading from the input pin
  38. int lastButtonState = HIGH;   // the previous reading from the input pin
  39.  
  40. // the following variables are unsigned longs because the time, measured in
  41. // milliseconds, will quickly become a bigger number than can be stored in an int.
  42. unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  43. unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
  44.  
  45. void setup() {
  46.   pinMode(buttonPin, INPUT);
  47.   pinMode(ledPin, OUTPUT);
  48.   pinMode(relayPin, OUTPUT);
  49.  
  50.  
  51.   // set initial LED state
  52.   digitalWrite(ledPin, ledState);
  53.   digitalWrite(relayPin, ledState);
  54. }
  55.  
  56. void loop() {
  57.   // read the state of the switch into a local variable:
  58.   int reading = digitalRead(buttonPin);
  59.  
  60.   // check to see if you just pressed the button
  61.   // (i.e. the input went from LOW to HIGH), and you've waited long enough
  62.   // since the last press to ignore any noise:
  63.  
  64.   // If the switch changed, due to noise or pressing:
  65.   if (reading != lastButtonState) {
  66.     // reset the debouncing timer
  67.     lastDebounceTime = millis();
  68.   }
  69.  
  70.   if ((millis() - lastDebounceTime) > debounceDelay) {
  71.     // whatever the reading is at, it's been there for longer than the debounce
  72.     // delay, so take it as the actual current state:
  73.  
  74.     // if the button state has changed:
  75.     if (reading != buttonState) {
  76.       buttonState = reading;
  77.  
  78.       // only toggle the LED if the new button state is HIGH
  79.       if (buttonState == LOW) {
  80.         ledState = !ledState;
  81.       }
  82.     }
  83.   }
  84.  
  85.   // set the LED:
  86.   digitalWrite(ledPin, ledState);
  87.   digitalWrite(relayPin, ledState);
  88.  
  89.   // save the reading. Next time through the loop, it'll be the lastButtonState:
  90.   lastButtonState = reading;
  91. }
Add Comment
Please, Sign In to add comment