Relays are trilling, how button take overruled command

Hello,

I have a question that I can't quite figure out, at least I know what probably causes it, but I don't see a solution.
What do we have: 3 buttons and 3 relays
When I turn switch 1 on, relay 1 turns on properly, when I turn the switch off again, relay 1 also turns off again, the same with switch 2, when we press it, relay 2 turns on and turns off again when we turn switch 2 off again.

When I turn switch 1 and switch 2 both on, both relays 1 and 2 are also on, the intention is that when I press push button 3, relay 1 and relay 2 should go out and relay 3 should be on as long as I press this push button, at the moment I release push button 3, relay 1 and relay 2 should turn on again, because it was still on.

With the sketch I made it works but the relays vibrate and that is probably because in the loop the command is given, which is given earlier in the sketch, that if the button is on the relays must also be on.

How do I get Push button 3 to have priority to turn off relays 1 and 2 as long as it is active. This is my sketch, hopefully someone can help me, it must be something small but I can't see it right now

void loop() {
  buttonState1 = digitalRead(tlButton);
  if (buttonState1 == HIGH) {
    digitalWrite(ledPin1, LOW); //turn relais 1 off
  } else {
    digitalWrite(ledPin1, HIGH); //turn relais 1 on
  }

  buttonState2 = digitalRead(mdButton);
  if (buttonState2 == HIGH) {
    digitalWrite(ledPin2, LOW); //turn relais 2 off
  } else {
    digitalWrite(ledPin2, HIGH); //turn relais 2 on
  }

  buttonState3 = digitalRead(zhButton);
  if (buttonState3 == HIGH) {
    digitalWrite(ledPin3, LOW); //turn relais 3 on
    digitalWrite(ledPin2, LOW); //turn relais 2 off
    digitalWrite(ledPin1, LOW); //turn relais 1 off
  } else {
    digitalWrite(ledPin3, HIGH); //turn relais 1 off
    digitalWrite(ledPin2, HIGH); //turn relais 2 on
    digitalWrite(ledPin1, HIGH); //turn relais 1 on
  }
}

With snippets of code it is hard to tell what the program does. Do all the switch pins have pull up resistors or do they have pull down resistors? Do they have resistors connected any way?

Do your switches ever have bouncing contacts? Are you powering the relays from a separate power supply?

All switches are connected correctly according to the Arduino method, with the correct resistors, the switches also work fine, without any interrupting intervention, they work perfectly because of the resistors. The problem is also not with the hardware in my opinion. The problem is in the sketch, if the push button, momentary presh is pressed, the LOOP is still repeated, there comes the moment that it is told that the switches are on, so the relays respond to the button being open while later in the loop it is told that it can be closed, so it starts to bounce, I will add the entire code in a moment

What powers your relays? Show a schematic, please.

int tlButton = 4;
int buttonState4 = 0;
int mdButton = 5;
int buttonState5 = 0;
int zhButton = 6;
int buttonState6 = 0;
int ledPin1 = 1;
int ledPin2 = 2;
int ledPin3 = 3;

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(tlButton, INPUT);
  pinMode(mdButton, INPUT);
  pinMode(zhButton, INPUT);
}


void loop() {
  buttonState1 = digitalRead(tlButton);
  if (buttonState1 == HIGH) {
    digitalWrite(ledPin1, LOW);  //turn relais 1 off
  } else {
    digitalWrite(ledPin1, HIGH);  //turn relais 1 on
  }

  buttonState2 = digitalRead(mdButton);
  if (buttonState2 == HIGH) {
    digitalWrite(ledPin2, LOW);  //turn relais 2 off
  } else {
    digitalWrite(ledPin2, HIGH);  //turn relais 2 on
  }

  buttonState3 = digitalRead(zhButton);
  if (buttonState3 == HIGH) {
    digitalWrite(ledPin3, LOW);  //turn relais 3 on
    digitalWrite(ledPin2, LOW);  //turn relais 2 off
    digitalWrite(ledPin1, LOW);  //turn relais 1 off
  } else {
    digitalWrite(ledPin3, HIGH);  //turn relais 1 off
    digitalWrite(ledPin2, HIGH);  //turn relais 2 on
    digitalWrite(ledPin1, HIGH);  //turn relais 1 on
  }
}

I have to make a schematic but if you see the sketch, you can already make it out, but I want to draw it. It is an arduino uno, 4 relay card, 2 switches and 1 push momentary button. relays and switches work fine and also connected as it should be but I will make a schematic, keep in touch

If you want switch 3 to have priority over switch 1 and 2, you will have to keep track of the states and add some more logic other than if switch 1 then ON else OFF. You should study the state change detection example in the IDE to detect when a switch changes state, not if a switch is in a certain state. It will help with the logic.

something along these lines...

if ( switch3_override == true ) {
    // ignore switch 1 and 2
}
else {
{
  // check switch 1 and 2 like normal
 ...
}

// logic to detect switch 3 state change and toggle switch3_override true/false

It's nice when posted code can be compiled. Please try again.

Fixed what you posted. Try this - untested. Suggest you get away from using pin 1 - it's Serial, and you'll need Serial Monitor to be available for debugging in future.

int tlButton = 4;
int buttonState1 = 0;
int mdButton = 5;
int buttonState2 = 0;
int zhButton = 6;
int buttonState3 = 0;
int ledPin1 = 1;
int ledPin2 = 2;
int ledPin3 = 3;

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(tlButton, INPUT);
  pinMode(mdButton, INPUT);
  pinMode(zhButton, INPUT);
}

const int RLYON = HIGH;
const int RLYOFF = LOW;
void loop() {
  buttonState1 = digitalRead(tlButton);
  buttonState2 = digitalRead(mdButton);
  buttonState3 = digitalRead(zhButton);

  if (buttonState3 == HIGH) {
    digitalWrite(ledPin3, RLYON);  //turn relais 3 on
    digitalWrite(ledPin2, RLYOFF);  //turn relais 2 off
    digitalWrite(ledPin1, RLYOFF);  //turn relais 1 off
  }
  else {
    digitalWrite(ledPin3, RLYOFF);  //turn relais 3 off
    if (buttonState1 == HIGH) {
      digitalWrite(ledPin1, RLYON);  //turn relais 1 on
    } else {
      digitalWrite(ledPin1, RLYOFF);  //turn relais 1 on
    }
    if (buttonState2 == HIGH) {
      digitalWrite(ledPin2, RLYON);  //turn relais 2 on
    } else
    {
      digitalWrite(ledPin2, RLYOFF);  //turn relais 2 off
    }
  }
}

Still no answer about wiring, or power source. so I'm done. Good luck!

@camsysca has posted a fixed version of your code, so maybe your problem is fixed and you no longer care what was wrong with your original code.

If you do care, that's a good thing, because we learn more from our mistakes than we do from blindly copying someone else's fix.

If you want an explanation of the problem with your original code, just ask.

sorry i was working today. in the meantime the problem has been solved by camsysca solution, this is exactly what i meant, and now that i read the code i understand it too. Sometimes the code needs to be seen first to understand it and that is good. thank you for your cooperation

Maybe next time you will be more interested. Good luck.