Switching relays so only one is active

Hi,

I have been using the below code with success for switching relays so only one is active at any time, so pressing the switch for a relay with turn it on and turn off the previous active one.

I would like to change the code so if I press the swicth for the active relay it will turn that relay off so no relays are on, would that be possible to do?

#include <Button.h>

Button ButtonPin0 = Button(6);   // the input pin for the switch 1
Button ButtonPin1 = Button(7);   // the input pin for the switch 2
Button ButtonPin2 = Button(8);   // the input pin for the switch 3

byte RELAY1 = 1;      // the output pin for relay 1
byte RELAY2 = 2;      // the output pin for relay 2
byte RELAY3 = 3;      // the output pin for relay 3

void setup() {

  pinMode(RELAY1, OUTPUT);    // sets the RELAY1 pin as an output
  pinMode(RELAY2, OUTPUT);    // sets the RELAY2 pin as an output
  pinMode(RELAY3, OUTPUT);    // sets the RELAY2 pin as an output
  {
    digitalWrite(RELAY1, HIGH);  // set RELAY1 to HIGH
    digitalWrite(RELAY2, LOW);   // set RELAY1 to LOW
    digitalWrite(RELAY3, LOW);   // set RELAY1 to LOW
  }
}

void loop() 
{
   if(ButtonPin0.isPressed()){
      {
        digitalWrite(RELAY1, HIGH);  // set RELAY1 to HIGH
        digitalWrite(RELAY2, LOW);   // set RELAY2 to LOW
        digitalWrite(RELAY3, LOW);   // set RELAY3 to LOW
      }
    }
    
    if(ButtonPin1.isPressed()){
      {
        digitalWrite(RELAY1, LOW);  // set RELAY1 to HIGH
        digitalWrite(RELAY2, HIGH);   // set RELAY2 to LOW
        digitalWrite(RELAY3, LOW);   // set RELAY3 to LOW
      }
    }
    
    if(ButtonPin2.isPressed()){
      {
        digitalWrite(RELAY1, LOW);  // set RELAY1 to HIGH
        digitalWrite(RELAY2, LOW);   // set RELAY2 to LOW
        digitalWrite(RELAY3, HIGH);   // set RELAY3 to LOW
      }
    }
 }

would that be possible to do?

Yes.

  {
    digitalWrite(RELAY1, HIGH);  // set RELAY1 to HIGH
    digitalWrite(RELAY2, LOW);   // set RELAY1 to LOW
    digitalWrite(RELAY3, LOW);   // set RELAY1 to LOW
  }

{What} {are} {those} {curly} {braces} {for} {?}

You should keep track of which relay is on, in a global byte variable, with an initial value of 0 (meaning no relay is on). When a switch is pressed, compare the switch number to the relay that is on. If they are the same, simply turn the relay that is on off and set the value to 0.

If the value is not the same as the switch, turn the relay that is on off, turn the proper one on, and set the value to the relay that you just turned on.

Thank you Paul, good point about the brackets, I have removed them now.

Are there any examples of code for the solution you suggested that I can look into? My level of knowledge with coding is very low so I am not sure how to implement your suggestion.

My level of knowledge with coding is very low so I am not sure how to implement your suggestion.

How about one step at a time?

Step 1:

byte currentRelay = 0;

Step 2:

void loop()
{
   if(ButtonPin0.isPressed())
   {
      // If the active relay isn't this one, turn it off
      if(currentRelay != 1)
      {
         // Code here to turn off current relay
         // Code here to turn relay 1 on
         currentRelay = 1;
      }
      else
      {
         // Current relay is 1. Turn it off
         currentRelay = 0;
      }
   }

   // Similar code for other switches
}

You just need to replace the comments with real code.

Paul that is awesome, thanks.

I will give it a bash this weekend :slight_smile:

Were you able to make this work?

ACS680: You may never get an answer to your question.

If you have a problem you're trying to solve, please start your own thread, it is bad form to hijack another member's message thread to solve your problem. Even if the problems are similar, it it best to start your own. You'll usually get much better results using your own words, code and schematics.

Please review:

How to use this forum

Sadly due to work I have not had the chance to delve into this yet, I will though! :slight_smile: