Maker Pro
Maker Pro

Pushbutton controlled relays with delay

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
Found this on youtube and wanted to share the code. I had to play/pause the video to get the code. The channel is a person named Samir Tawfik. I like the project and it is easily expanded upon. I have modified his original sketch to make it easier to understand and add more relays when needed. The relays can be activated randomly and/or simultaneously. See what you think.

Code:
/*   Pushbutton controlled relays with delay. Modified by Jared Elliott.
 *   Control 4 relays via arduino using pushbuttons.
 *   There is a delay after pressing the pushbutton before it turns off.
 *   The delays can be adjusted to suit your needs.
 *   Additional relays can be added by copying the lines of code and adding the required setup code.
 * 
 *   This code was originally by Samir Tawfik on YouTube.
 */

#define RELAY_ON 0                                // The relays are active-LOW; on when LOW (0)
#define RELAY_OFF 1                               // and off when HIGH (1). These #defines just help keep track.

const int RelayPin1 = 8;                          // Pin numbers for the relays.
const int RelayPin2 = 9;                          // Constant integers keep the pin numbers from changing (read-only).
const int RelayPin3 = 10;
const int RelayPin4 = 11;
//const int RelayPin5 = *;                        // Change the * to correspond with the arduino pin
                                                  // that you wish to use for an additional relay. Uncomment to use.

const int ButtonPin1 = 2;                         // Pin numbers for the push buttons.
const int ButtonPin2 = 3;
const int ButtonPin3 = 4;
const int ButtonPin4 = 5;
//const int ButtonPin5 = *;                       // Change the * to correspond with the arduino pin
                                                  // that the additional button is connected to. Uncomment to use.

const int Duration1 = 500;                        // Number of milliseconds that the relays run after the buttons are pressed.
const int Duration2 = 500;
const int Duration3 = 500;
const int Duration4 = 500;
//const int Duration5 = *;                        // Change the * for Duration to a number of your choice. Uncomment to use.

// variables

byte State1 = RELAY_OFF;                          // Used to record whether the relays are on or off.
byte State2 = RELAY_OFF;                          // - default to HIGH/OFF -
byte State3 = RELAY_OFF;
byte State4 = RELAY_OFF;
//byte Stete5 = RELAY_OFF;                        // Uncomment to use for an additional relay.

unsigned long currentMillis = 0;                  // Stores the value of millis() in each iteration of loop().

unsigned long TimerMillis1 = 0;                   // Stores the times when the buttons were last pressed.
unsigned long TimerMillis2 = 0;
unsigned long TimerMillis3 = 0;
unsigned long TimerMillis4 = 0;
//unsigned long TimerMillis5 = 0;                 // Uncomment to use for an additional relay.

void setup()
{
// --Debug--
  Serial.begin(9600);
  Serial.println("Starting Relay Controller.ino");

  digitalWrite(RelayPin1, RELAY_OFF);             // Relays are active-LOW. Initialize relay pins
  digitalWrite(RelayPin2, RELAY_OFF);             // high so the relays are inactive at startup/reset.
  digitalWrite(RelayPin3, RELAY_OFF);
  digitalWrite(RelayPin4, RELAY_OFF);
//digitalWrite(RelayPin5, RELAY_OFF);             // Uncomment for an additional relay.

  pinMode(RelayPin1, OUTPUT);                     // Then set the pins as outputs.
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
//pinMode(RelayPin5, OUTPUT);                     // Uncomment for an additional relay.

  pinMode(ButtonPin1, INPUT_PULLUP);              // Set the float sensor pins as inputs and use the internal pullup resistors.
  pinMode(ButtonPin2, INPUT_PULLUP);
  pinMode(ButtonPin3, INPUT_PULLUP);
  pinMode(ButtonPin4, INPUT_PULLUP);
//pinMode(ButtonPin5, INPUT_PULLUP);              // Uncomment for an additional relay.

}

void loop()
{
// Get the current clock count

  currentMillis = millis() + Duration1;           // We add the countdown timer duration to
                                                  // the clock to prevent the relay from
                                                  // running at boot time, whele the clock
                                                  // counter is still below the timer value.
// Call the cunctions that do the work

  readButtonPin1();                               // Check each button and decide whether
  readButtonPin2();                               // to start the relay and countdown timer.
  readButtonPin3();
  readButtonPin4();
  triggerRelay();                                 // Actually toggles the relay pins based on
                                                  // the data from the above functions.
}

// ---Worker functions---

void readButtonPin1()                             // Repeat comments for each 'readButtonPin' iteration.
{
  if (digitalRead(ButtonPin1) == LOW)             // If the button is tripped (pulled low)...
  {
    State1 = RELAY_ON;                            // ...then trigger the relay pin
    TimerMillis1 = currentMillis;                 // and set the relay countdown timer to the current clock time.
  }

  if (currentMillis - TimerMillis1 >= Duration1)  //If the countdown timer has expired...
  {
    State1 = RELAY_OFF;                           // ...then turn off the relay pin
    TimerMillis1 = 0;                             // and reset the timer to 0 for the next trigger.
  }
}

void readButtonPin2()
{
  if (digitalRead(ButtonPin2) == LOW)
  {
    State2 = RELAY_ON;
    TimerMillis2 = currentMillis;
  }

  if (currentMillis - TimerMillis2 >= Duration2)
  {
    State2 = RELAY_OFF;
    TimerMillis2 = 0;
  }
}

void readButtonPin3()
{
  if (digitalRead(ButtonPin3) == LOW)
  {
    State3 = RELAY_ON;
    TimerMillis3= currentMillis;
  }

  if (currentMillis - TimerMillis3 >= Duration3)
  {
    State3 = RELAY_OFF;
    TimerMillis3 = 0;
  }
}

void readButtonPin4()
{
  if (digitalRead(ButtonPin4) == LOW)
  {
    State4 = RELAY_ON;
    TimerMillis4 = currentMillis;
  }

  if (currentMillis - TimerMillis4 >= Duration4)
  {
    State4 = RELAY_OFF;
    TimerMillis4 = 0;
  }

 // Uncomment the following block of code for the additional relay
 // and change the number to correspond with your setup.
 // When uncommenting the previous lines to add an additional relay, simply change the * to 5.
 /* void readButtonPin*()
{
  if (digitalRead(ButtonPin*) == LOW)
  {
    State* = RELAY_ON;
    TimerMillis* = currentMillis;
  }

  if (currentMillis - TimerMillis* >= Duration*)
  {
    State* = RELAY_OFF;
    TimerMillis* = 0;
  }
*/
}

void triggerRelay()
{
  digitalWrite(RelayPin1, State1);                // Toggle the relay pins on and off based on
  digitalWrite(RelayPin2, State2);                // the State from the readButton functions.
  digitalWrite(RelayPin3, State3);
  digitalWrite(RelayPin4, State4);
//digitalWrite(RelayPin5, Stete5);                // Uncomment for an additional relay.
                                                  // Adjust the number as needed to correspond with the setup.
}
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
I am having trouble with this sketch now. I am attempting to add 2 more relays and buttons to control them but the method which I thought should work is not working. I have added lines and when trying to compile, I get an error stating "'readButtonPin5' was not declared in this scope". Can someone please tell me what I am doing wrong?

Thanks.

Code:
/*   Pushbutton controlled relays. Modified by Jared Elliott.
 *   Control 4 relays via arduino using pushbuttons.
 *   There is a delay after pressing the pushbutton before it turns off.
 *   The delays can be adjusted to suit your needs.
 *   Additional relays can possibly be added by copying the
 *   lines of code and adding the required setup code.
 *  
 *   This code was originally by Samir Tawfik on YouTube.
 */

#define RELAY_ON 0                                // The relays are active-LOW; on when LOW (0)
#define RELAY_OFF 1                               // and off when HIGH (1). These #defines just help keep track.

const int RelayPin1 = 8;                          // Pin numbers for the relays.
const int RelayPin2 = 9;                          // Constant integers keep the pin numbers from changing (read-only).
const int RelayPin3 = 10;
const int RelayPin4 = 11;
const int RelayPin5 = 6;
const int RelayPin6 = 7;

const int ButtonPin1 = 2;                         // Pin numbers for the push buttons.
const int ButtonPin2 = 3;
const int ButtonPin3 = 4;
const int ButtonPin4 = 5;
const int ButtonPin5 = 0;
const int ButtonPin6 = 1;

const int Duration1 = 1000;                        // Number of milliseconds that the relays
const int Duration2 = 2000;                        // run after the buttons are pressed.
const int Duration3 = 3000;
const int Duration4 = 4000;
const int Duration5 = 5000;
const int Duration6 = 6000;

// variables

byte State1 = RELAY_OFF;                          // Used to record whether the relays are on or off.
byte State2 = RELAY_OFF;                          // - default to HIGH/OFF -
byte State3 = RELAY_OFF;
byte State4 = RELAY_OFF;
byte Stete5 = RELAY_OFF;                        // Uncomment to use for an additional relay.
byte Stete6 = RELAY_OFF;

unsigned long currentMillis = 0;                  // Stores the value of millis() in each iteration of loop().

unsigned long TimerMillis1 = 0;                   // Stores the times when the buttons were last pressed.
unsigned long TimerMillis2 = 0;
unsigned long TimerMillis3 = 0;
unsigned long TimerMillis4 = 0;
unsigned long TimerMillis5 = 0;                 // Uncomment to use for an additional relay.
unsigned long TimerMillis6 = 0;

void setup()
{
// --Debug--
  Serial.begin(9600);
  Serial.println("Starting Relay Controller.ino");

  digitalWrite(RelayPin1, RELAY_OFF);             // Relays are active-LOW. Initialize relay pins
  digitalWrite(RelayPin2, RELAY_OFF);             // high so the relays are inactive at startup/reset.
  digitalWrite(RelayPin3, RELAY_OFF);
  digitalWrite(RelayPin4, RELAY_OFF);
  digitalWrite(RelayPin5, RELAY_OFF);             // Uncomment for an additional relay.
  digitalWrite(RelayPin6, RELAY_OFF);

  pinMode(RelayPin1, OUTPUT);                     // Then set the pins as outputs.
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(RelayPin5, OUTPUT);                     // Uncomment for an additional relay.
  pinMode(RelayPin6, OUTPUT);

  pinMode(ButtonPin1, INPUT_PULLUP);              // Set the float sensor pins as inputs
  pinMode(ButtonPin2, INPUT_PULLUP);              // and use the internal pullup resistors.
  pinMode(ButtonPin3, INPUT_PULLUP);
  pinMode(ButtonPin4, INPUT_PULLUP);
  pinMode(ButtonPin5, INPUT_PULLUP);
  pinMode(ButtonPin6, INPUT_PULLUP);

}

void loop()
{
// Get the current clock count

  currentMillis = millis() + Duration1;           // We add the countdown timer duration to
                                                  // the clock to prevent the relay from
                                                  // running at boot time, whele the clock
                                                  // counter is still below the timer value.
// Call the functions that do the work

  readButtonPin1();                               // Check each button and decide whether
  readButtonPin2();                               // to start the relay and countdown timer.
  readButtonPin3();
  readButtonPin4();
  readButtonPin5();
  readButtonPin6();
  triggerRelay();                                 // Actually toggles the relay pins based on
                                                  // the data from the above functions.
}

// ---Worker functions---

void readButtonPin1()                             // Repeat comments for each 'readButtonPin' iteration.
{
  if (digitalRead(ButtonPin1) == LOW)             // If the button is tripped (pulled low)...
  {
    State1 = RELAY_ON;                            // ...then trigger the relay pin
    TimerMillis1 = currentMillis;                 // and set the relay countdown timer to the current clock time.
  }

  if (currentMillis - TimerMillis1 >= Duration1)  //If the countdown timer has expired...
  {
    State1 = RELAY_OFF;                           // ...then turn off the relay pin
    TimerMillis1 = 0;                             // and reset the timer to 0 for the next trigger.
  }
}

void readButtonPin2()
{
  if (digitalRead(ButtonPin2) == LOW)
  {
    State2 = RELAY_ON;
    TimerMillis2 = currentMillis;
  }

  if (currentMillis - TimerMillis2 >= Duration2)
  {
    State2 = RELAY_OFF;
    TimerMillis2 = 0;
  }
}

void readButtonPin3()
{
  if (digitalRead(ButtonPin3) == LOW)
  {
    State3 = RELAY_ON;
    TimerMillis3= currentMillis;
  }

  if (currentMillis - TimerMillis3 >= Duration3)
  {
    State3 = RELAY_OFF;
    TimerMillis3 = 0;
  }
}

void readButtonPin4()
{
  if (digitalRead(ButtonPin4) == LOW)
  {
    State4 = RELAY_ON;
    TimerMillis4 = currentMillis;
  }

  if (currentMillis - TimerMillis4 >= Duration4)
  {
    State4 = RELAY_OFF;
    TimerMillis4 = 0;
  }

void readButtonPin5()
{
  if (digitalRead(ButtonPin5) == LOW)
  {
    State5 = RELAY_ON;
    TimerMillis5 = currentMillis;
  }

  if (currentMillis - TimerMillis5 >= Duration5)
  {
    State5 = RELAY_OFF;
    TimerMillis5 = 0;
  }

void readButtonPin6()
{
  if (digitalRead(ButtonPin6) == LOW)
  {
    State6 = RELAY_ON;
    TimerMillis6 = currentMillis;
  }

  if (currentMillis - TimerMillis6 >= Duration6)
  {
    State6 = RELAY_OFF;
    TimerMillis6 = 0;
  }
}

void triggerRelay()
{
  digitalWrite(RelayPin1, State1);                // Toggle the relay pins on and off based on
  digitalWrite(RelayPin2, State2);                // the State from the readButton functions.
  digitalWrite(RelayPin3, State3);
  digitalWrite(RelayPin4, State4);
  digitalWrite(RelayPin5, Stete5);
  digitalWrite(RelayPin5, Stete6);
}
 
Last edited:

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
I found my mistake. I misspelled two lines...need to get better glasses.
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
I thought the mistake that I found was going to be the solution. It is not. I have corrected the spelling of the two lines and I still get an error.

Any ideas?
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
I just wanted to reply to this thread with an update on this. It seems that there were some spelling errors and an issue with brackets missing. The problem is corrected and this is the final code:
*edit* I modified the code to make the ButtonPin numbers match the pins they were in. In doing so, I had to comment out the Serial.begin and Serial.println. With these two lines, pins 0 and 1 do not work as inputs. I noted this in the code.
Code:
/*   Pushbutton controlled relays. Modified by Jared Elliott.
 *   Control 6 relays via arduino using pushbuttons.
 *   There is a delay after pressing the pushbutton before it turns off.
 *   The delays can be adjusted to suit your needs.
 *
 *   This code was originally by Samir Tawfik on YouTube.
 */

#define RELAY_ON 0                                // The relays are active-LOW; on when LOW (0)
#define RELAY_OFF 1                               // and off when HIGH (1). These #defines just help keep track.

const int RelayPin1 = 6;                          // Pin numbers for the relays.
const int RelayPin2 = 7;                          // Constant integers keep the pin numbers from changing (read-only).
const int RelayPin3 = 8;
const int RelayPin4 = 9;
const int RelayPin5 = 10;
const int RelayPin6 = 11;

const int ButtonPin1 = 0;                         // Pin numbers for the push buttons.
const int ButtonPin2 = 1;
const int ButtonPin3 = 2;
const int ButtonPin4 = 3;
const int ButtonPin5 = 4;
const int ButtonPin6 = 5;

const int Duration1 = 1000;                        // Number of milliseconds that the relays
const int Duration2 = 1000;                        // run after the buttons are pressed.
const int Duration3 = 1000;
const int Duration4 = 1000;
const int Duration5 = 1000;
const int Duration6 = 1000;

// variables

byte State1 = RELAY_OFF;                          // Used to record whether the relays are on or off.
byte State2 = RELAY_OFF;                          // - default to HIGH/OFF -
byte State3 = RELAY_OFF;
byte State4 = RELAY_OFF;
byte State5 = RELAY_OFF;
byte State6 = RELAY_OFF;

unsigned long currentMillis = 0;                  // Stores the value of millis() in each iteration of loop().

unsigned long TimerMillis1 = 0;                   // Stores the times when the buttons were last pressed.
unsigned long TimerMillis2 = 0;
unsigned long TimerMillis3 = 0;
unsigned long TimerMillis4 = 0;
unsigned long TimerMillis5 = 0;
unsigned long TimerMillis6 = 0;

void setup()
{
// --Debug--
// Note: When using the following two lines, you must use different pins other than 0 and 1 for button input.
  //Serial.begin(9600);
  //Serial.println("Starting Relay Controller.ino");

  digitalWrite(RelayPin1, RELAY_OFF);             // Relays are active-LOW. Initialize relay pins
  digitalWrite(RelayPin2, RELAY_OFF);             // high so the relays are inactive at startup/reset.
  digitalWrite(RelayPin3, RELAY_OFF);
  digitalWrite(RelayPin4, RELAY_OFF);
  digitalWrite(RelayPin5, RELAY_OFF);
  digitalWrite(RelayPin6, RELAY_OFF);

  pinMode(RelayPin1, OUTPUT);                     // Then set the pins as outputs.
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(RelayPin5, OUTPUT);
  pinMode(RelayPin6, OUTPUT);

  pinMode(ButtonPin1, INPUT_PULLUP);              // Set the float sensor pins as inputs
  pinMode(ButtonPin2, INPUT_PULLUP);              // and use the internal pullup resistors.
  pinMode(ButtonPin3, INPUT_PULLUP);
  pinMode(ButtonPin4, INPUT_PULLUP);
  pinMode(ButtonPin5, INPUT_PULLUP);
  pinMode(ButtonPin6, INPUT_PULLUP);
}

void loop()
{
// Get the current clock count

  currentMillis = millis() + Duration1;           // We add the countdown timer duration to
                                                  // the clock to prevent the relay from
                                                  // running at boot time, whele the clock
                                                  // counter is still below the timer value.
// Call the functions that do the work

  readButtonPin1();                               // Check each button and decide whether
  readButtonPin2();                               // to start the relay and countdown timer.
  readButtonPin3();
  readButtonPin4();
  readButtonPin5();
  readButtonPin6();
  triggerRelay();                                 // Actually toggles the relay pins based on
                                                  // the data from the above functions.
}

// ---Worker functions---

void readButtonPin1()                             // Repeat comments for each 'readButtonPin' iteration.
{
  if (digitalRead(ButtonPin1) == LOW)             // If the button is tripped (pulled low)...
  {
    State1 = RELAY_ON;                            // ...then trigger the relay pin
    TimerMillis1 = currentMillis;                 // and set the relay countdown timer to the current clock time.
  }

  if (currentMillis - TimerMillis1 >= Duration1)  //If the countdown timer has expired...
  {
    State1 = RELAY_OFF;                           // ...then turn off the relay pin
    TimerMillis1 = 0;                             // and reset the timer to 0 for the next trigger.
  }
}

void readButtonPin2()
{
  if (digitalRead(ButtonPin2) == LOW)
  {
    State2 = RELAY_ON;
    TimerMillis2 = currentMillis;
  }

  if (currentMillis - TimerMillis2 >= Duration2)
  {
    State2 = RELAY_OFF;
    TimerMillis2 = 0;
  }
}

void readButtonPin3()
{
  if (digitalRead(ButtonPin3) == LOW)
  {
    State3 = RELAY_ON;
    TimerMillis3= currentMillis;
  }

  if (currentMillis - TimerMillis3 >= Duration3)
  {
    State3 = RELAY_OFF;
    TimerMillis3 = 0;
  }
}

void readButtonPin4()
{
  if (digitalRead(ButtonPin4) == LOW)
  {
    State4 = RELAY_ON;
    TimerMillis4 = currentMillis;
  }

  if (currentMillis - TimerMillis4 >= Duration4)
  {
    State4 = RELAY_OFF;
    TimerMillis4 = 0;
  }
}

void readButtonPin5()
{
  if (digitalRead(ButtonPin5) == LOW)
  {
    State5 = RELAY_ON;
    TimerMillis5 = currentMillis;
  }

  if (currentMillis - TimerMillis5 >= Duration5)
  {
    State5 = RELAY_OFF;
    TimerMillis5 = 0;
  }
}

void readButtonPin6()
{
  if (digitalRead(ButtonPin6) == LOW)
  {
    State6 = RELAY_ON;
    TimerMillis6 = currentMillis;
  }

  if (currentMillis - TimerMillis6 >= Duration6)
  {
    State6 = RELAY_OFF;
    TimerMillis6 = 0;
  }
}

void triggerRelay()
{
  digitalWrite(RelayPin1, State1);                // Toggle the relay pins on and off based on
  digitalWrite(RelayPin2, State2);                // the State from the readButton functions.
  digitalWrite(RelayPin3, State3);
  digitalWrite(RelayPin4, State4);
  digitalWrite(RelayPin5, State5);
  digitalWrite(RelayPin6, State6);
}
 
Last edited:
Top