Dual relay project with Arduino

Share

Working on a project for arduino which will use two switches to control two relays separately. There will be timers to control the on time of the relays. Here is the sketch code.

// constnts that won’t change. They’re used here to set pin numbers

const int button1 = 2;      // the number of the pushbutton pin

const int button2 = 3;      // the number of the pushbutton pin

const int relay1 = 11;     // the number of the relay1 pin

const int relay2 = 12;     // the number of the relay2 pin

// variables that will change

int buttonState1 = 1;       // variable for reading the pushbutton1 status

int buttonState2 = 1;       // variable for reading the pushbutton2 status

boolean button1_pressed = false; // This is a state flag for button1.

int relay1_onTime = 1000; // This is the time in milliseconds that relay1 will turn on.

unsigned long button1_previousTime = 0; // This is for saving the millis() time that the button was pressed.

unsigned long button1_currentTime = 0; // This is for saving a later time to compare to the above time.

boolean button2_pressed = false; // This is a state flag for button2.

int relay2_onTime = 5000; // This is the time in milliseconds that relay2 will turn on.

unsigned long button2_previousTime = 0;// This is for saving the millis() time that the button was pressed.

unsigned long button2_currentTime = 0;// This is for saving a later time to compare to the above time.

void setup()

{

 // initialize the relay pin as an output

 pinMode(relay1, OUTPUT);

 pinMode(relay2, OUTPUT);

 digitalWrite(relay1, HIGH);

 digitalWrite(relay2, HIGH);

 // initialize the pushbutton pin as an input

 pinMode(button1, INPUT_PULLUP);   // PULLUP for add 5v to pin without hardware prevent pin from floating between HIGH and LOW

 pinMode(button2, INPUT_PULLUP);   // PULLUP for add 5v to pin without hardware prevent pin from floating between HIGH and LOW

}

void loop()

{

 if (button1_pressed == false) {

   // This check is here to de-bounce the button input.

   // Also it only tests for the button press if the relay is turned [off].

   // read the state of the pushbutton value

   buttonState1 = digitalRead(button1);

   // check if the pushbutton is pressed. If it is, the buttonState is LOW.

   if (buttonState1 == LOW)

   {

     digitalWrite(relay1, LOW);      // pin 2 is pressed and connected to GND so it will be LOW

     //delay (1000);                   // remove 5v from pin 11 so relay in1 will be 0v and this will make relay on

     // wait 1 sec.

     button1_pressed = true;

     button1_previousTime = millis();

   }

   //else

   //{

   //digitalWrite(relay1, HIGH);     // add 5v to arduino pin 11 so relay in1 will be 5v and this will make the relay turn off.

   // turning off the relay is done further below

   //}

 }

 if (button2_pressed == false) {

   // This check is here to de-bounce the button input.

   // Also it only tests for the button press if the relay is turned [off].

   // read the state of the pushbutton value

   buttonState2 = digitalRead(button2);

   // check if the pushbutton is pressed

   // if it is, the buttonState is LOW

   if (buttonState2 == LOW)          // pin 3 is pressed and connected to GND so it will be LOW

   { // remove 5v from pin 12 so relay in2 will be 0v and this will make the relay turn on

     digitalWrite(relay2, LOW);      // and wait 5 seconds.

     //delay (5000);

     button2_pressed = true;

     button2_previousTime = millis();

   }

   //else

   //{

   //digitalWrite(relay2, HIGH);     // add 5v to arduino pin 12 so relay in2 will be 5v and this will make the relay turn off.

   // turning off the relay is done further below

   //}

 }

 if (button1_pressed == true) {

   button1_currentTime = millis();

   if (button1_currentTime >= button1_previousTime) {

     if (button1_currentTime >= (button1_previousTime + relay1_onTime)) {

       // This is now where the relay gets turned off, but only if it is on,

       // and has been on for at least (relay1_onTime) milliseconds.

       button1_pressed = false; // This turns off the state flag so you can press the button again.

       digitalWrite(relay1, HIGH); // This turns off the relay.

     }

   }

   else {

     button1_previousTime = millis();

     // this is millis() rollover protection, not likely to happen but a good habit.

   }

 }

 if (button2_pressed == true) {

   button2_currentTime = millis();

   if (button2_currentTime >= button2_previousTime) {

     if (button2_currentTime >= (button2_previousTime + relay2_onTime)) {

       // This is now where the relay gets turned off, but only if it is on,

       // and has been on for at least (relay2_onTime) milliseconds.

       button2_pressed = false;// This turns off the state flag so you can press the button again.

       digitalWrite(relay2, HIGH); // This turns off the relay.

     }

   }

   else {

     button2_previousTime = millis();

     // this is millis() rollover protection, not likely to happen but a good habit.

   }

 }

}

Leave a Reply