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.
}