/* 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 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 = A0;
const int RelayPin6 = A1;
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 = 6;
const int ButtonPin6 = 7;
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);
}