How to prevent trigger multiple relays at once?

I have a 4 channel relay module and i want to prevent to trigger if there is an already triggered channel.

So i tried something but it’s not working.

void callback(char* topic, byte* payload, unsigned int length) {
  String any_relay = "";
  if (payload[0] == '1')
  {
     for (uint8_t i = 0; i < RELAY_NUM; i++)
        if(any_relay == "")   
        {
        Serial.print("Relay #");
        Serial.print(i+1);
        Serial.println(" OPENED!");
        digitalWrite(relay[i], ON);
        any_relay = topic;
        break;
        }
        else
        {
          Serial.println("Need to close the other relay channel before to open a new one! : " + any_relay);
        }
  }
}

I can see the any_relay variable is changing to the name of last opened relay channel(as a A,B,C,D) but i think there is a problem with comparison but im not sure.

  String any_relay = "";
  if (payload[0] == '1')
  {
     for (uint8_t i = 0; i < RELAY_NUM; i++)
        if(any_relay == "")   
        {

So, any_relay is unconditionally set to “” and then you test whether it is set to “”. Is that what you meant to do ?

UKHeliBob:

  String any_relay = "";

if (payload[0] == ‘1’)
  {
    for (uint8_t i = 0; i < RELAY_NUM; i++)
        if(any_relay == “”) 
        {



So, any_relay is unconditionally set to "" and then you test whether it is set to "". Is that what you meant to do ?

If it’s equal value it means there is no triggered relay already and i assign the topic value to any_relay variable ,inside of the if statement. So i expect that i couldn’t trigger other relay because any_relay variable changed(not equal to “”).

OK, I see now, but it reinforces why I always put code blocks for if, for, while in braces as it makes clear which code is executed

UKHeliBob:
OK, I see now, but it reinforces why I always put code blocks for if, for, while in braces as it makes clear which code is executed

So do you know why it's not working ?

Please post the whole program

I see an anomaly in what you have posted

On the first pass through the for loop any_relay will always equal "" so
        if(any_relay == "") will always be true.

If I interpret the code correctly then this will cause relay 1 to turn on. Have I interpreted the code correctly ?