gm310509

Turn On 2 Relays with delay and blinking LED

Nov 29th, 2021 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. /*
  2.  * Example of how to turn on two relays with a delay and blink an LED while turning on.
  3.  *
  4.  * Inspired by a reddit post:
  5.  * https://www.reddit.com/r/arduino/comments/r4phym/how_to_turn_on_two_relays_one_instantly_one/
  6.  *
  7.  * By gm310509
  8.  *    November-2021
  9.  *
  10.  */
  11. const int LED = 3;     // an actual LED
  12. const int RELAY0 = 5;   // first relay 1
  13. const int RELAY1 = 6;   // aka relay 2
  14.  
  15. const int BUTTON = 2;
  16.  
  17. const int ON_DELAY_MS = 5000;     // Delay between turning relay 1 and 2 in ms (i.e. 5000 = 5 seconds).
  18. const int BLINK_INTERVAL = 250;
  19.  
  20. void setup() {
  21.   Serial.begin(115200);
  22.   while (!Serial) {
  23.   }
  24.  
  25.   pinMode(LED, OUTPUT);
  26.   pinMode(RELAY0, OUTPUT);
  27.   pinMode(RELAY1, OUTPUT);
  28.  
  29.   pinMode(BUTTON, INPUT);
  30. }
  31.  
  32.  
  33. void loop() {
  34. static boolean relaysOn = false;        // relay status on or off - initially off.
  35. static boolean turningOn = false;       // true if we are in the process of turning the relays on.
  36. static unsigned long nextStepTime = 0;  // when to take the next step.
  37. static unsigned long nextBlinkTime = 0;  // when to blink the LED.
  38. static int prevBtnState = HIGH;
  39.  
  40.   unsigned long now = millis();         // Get current "time" ready for the rest of this block of code..
  41.  
  42.   int btnState = digitalRead(BUTTON);   // Read the button state
  43.   if (btnState != prevBtnState) {       // Has it changed state? (i.e. pressed or released)
  44.     prevBtnState = btnState;
  45.     delay(10);                          // This a simplistic way to debounce the button.
  46.    
  47.     if (btnState == LOW) {              // Button has been pressed.
  48.       if (relaysOn) {                   // Are they on?
  49.         digitalWrite (RELAY0, LOW);     // Yes, turn everything off.
  50.         digitalWrite (RELAY1, LOW);
  51.         digitalWrite (LED, LOW);
  52.         relaysOn = false;               // indicate that everything is off.
  53.         delay
  54.       } else if (!turningOn) {          // Are we in the middle of turning the relays ON
  55.                                         // No we are not in the middle of turning them on,
  56.                                         // so initiate the turning on process.
  57.         turningOn = true;
  58.         digitalWrite (RELAY0, HIGH);    // turn the first two things on.
  59.         digitalWrite (LED, HIGH);
  60.         nextStepTime = now + ON_DELAY_MS;    // Work out when to turn the next one on.
  61.         nextBlinkTime = now + BLINK_INTERVAL;   // Same for the blinking of the LED.
  62.        
  63.       } else {                          // We are in the middle of turning the relays on, so what to do?
  64.                                         // I do not know, so right now, do nothing.
  65.       }
  66.     }
  67.   }
  68.  
  69.  
  70.   if (turningOn) {                      // Are we turning stuff on?
  71.                                         // Yes, so check if it is time to perform the next step
  72.     if (now >= nextBlinkTime) {         // Blink the LED?
  73.       nextBlinkTime = now + BLINK_INTERVAL;   // Calculate the next time to change the LED
  74.       digitalWrite(LED, ! digitalRead(LED));  // invert the LED.
  75.     }
  76.  
  77.     if (turningOn && now >= nextStepTime) {
  78.       digitalWrite(RELAY1, HIGH);         // turn on the second relay
  79.       relaysOn = true;                    // The relays are no all on
  80.       turningOn = false;                  // And we are done with the turning on process.
  81.     }
  82.   }
  83. }
  84.  
Add Comment
Please, Sign In to add comment