[SOLVED] Arduino TOGGLE BUTTON with multiple relays or relay cluster

Hello,

I am having a problem with my relay switches, I have a 4 relay switch 5v for arduino and I am trying to make it so that when I push button 1 relay goes on, then I press again and relay goes off.

This works with the code although the problem is that it works only for relay 1 and only for 1 relay, if say I change the code and make multiple variable it will not work so here is the code for 1.

FIY I am using a UNO R3 ATmega 328

Keep in mind that this first code does work for only 1 relay, and it does work when I press it turn it on and then press again turns off

const int rl1 = 7;
const int rl2 = 12;
const int rl3 = 2;
const int rl4 = 8;
const int button1 = 11;
const int button2 = 10;
const int button3 = 3;
const int button4 = 4;

int rl1State = LOW;
int rl2State = LOW;
int rl3State = LOW;
int rl4State = LOW;
int buttonState = LOW;
int lastButtonState = HIGH;
int reading;

long lastDebounceTime=0;
long debounceDelay = 50;


void setup() {

  Serial.begin(9600);
  pinMode(rl1, OUTPUT);
  pinMode(rl2, OUTPUT);
  pinMode(rl3, OUTPUT);
  pinMode(rl3, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  
}

void loop() {
  
  reading = digitalRead(button1);
  

  if(reading != lastButtonState){
    lastDebounceTime = millis();
    lastButtonState = reading;
  }
  
  if((millis() - lastDebounceTime) > debounceDelay){
    if(buttonState != lastButtonState){
      buttonState = lastButtonState;
      if(buttonState == HIGH){
        rl1State = !rl1State;
        digitalWrite(rl1, rl1State);
        
        
      }
    }
  }
  
 
     
}

I tried this for multiple ones:

const int rl1 = 7;
const int rl2 = 12;
const int rl3 = 2;
const int rl4 = 8;
const int button1 = 11;
const int button2 = 10;
const int button3 = 3;
const int button4 = 4;

int rl1State = LOW;
int rl2State = LOW;
int rl3State = LOW;
int rl4State = LOW;


//States
int buttonState1 = LOW;
int lastButtonState1 = HIGH;
int buttonState2 = LOW;
int lastButtonState2 = HIGH;
int buttonState3 = LOW;
int lastButtonState3 = HIGH;
int buttonState4 = LOW;
int lastButtonState4 = HIGH;

//Read State

int reading1;
int reading2;
int reading3;
int reading4;

long lastDebounceTime1=0;
long debounceDelay1 = 50;

long lastDebounceTime2=0;
long debounceDelay2 = 50;

long lastDebounceTime3=0;
long debounceDelay3= 50;

long lastDebounceTime4=0;
long debounceDelay4 = 50;


void setup() {

  Serial.begin(9600);
  pinMode(rl1, OUTPUT);
  pinMode(rl2, OUTPUT);
  pinMode(rl3, OUTPUT);
  pinMode(rl3, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  
}

void loop() {
  
  reading1 = digitalRead(button1);
  reading2 = digitalRead(button2);
  reading3 = digitalRead(button3);
  reading4 = digitalRead(button4);

  if(reading1 != lastButtonState1){
    lastDebounceTime1 = millis();
    lastButtonState1 = reading1;
  }
 //Relay 1 
  if((millis() - lastDebounceTime1) > debounceDelay1){
    if(buttonState1 != lastButtonState1){
      buttonState1 = lastButtonState1;
      if(buttonState1 == HIGH){
        rl1State = !rl1State;
        digitalWrite(rl1, rl1State);
        
        
      }
    }
  }
  
  
  //Relay2
  if(reading2 != lastButtonState2){
    lastDebounceTime2 = millis();
    lastButtonState2 = reading2;
  }
  
  if((millis() - lastDebounceTime2) > debounceDelay2){
    if(buttonState2 != lastButtonState2){
      buttonState2 = lastButtonState2;
      if(buttonState2 == HIGH){
        rl2State = !rl2State;
        digitalWrite(rl2, rl2State);
        
        
      }
    }
  }
  
 
     
}

I also tried to remake all the variables for each button and relay but still does not work

As well one of my relays does not work when put to any pin (when all the pins are connected) but it works only when one of the pins are disconnected, its really weird, I tested the relay it's fine I changed the arduino but same.

Thank you!

Yes I did post this in another thread, but I think it's the wrong one and don't know how to erase it.

So what is the schematic of your wiring?

The debounce function doesn't look right anyway. Why do you need the:-

if(buttonState != lastButtonState){

Do you have pull-up or pull-down resistors on your buttons? I notice that you are not using INPUT_PULLUP, so you will need external resistors to prevent your Arduino inputs from "floating" and giving unpredictable readings.

Solved, here is the solution I know a lot of people are looking for it:

Resistor 10kOhm or 100kOhm

Schematics:

Code:

//Buttons

int button1 = 7;
int button2 = 6;
int button3 = 4;
int button4 = 2;


//Relays
int rl1 = 13;
int rl2 = 12;
int rl3 = 11;
int rl4 = 8;


//States for Relay and Button (1)

int state1 = HIGH;      // the current state of the output pin
int reading1;           // the current reading from the input pin
int previous1 = LOW;    // the previous reading from the input pin

//States for Relay and Button (2)

int state2 = HIGH;      // the current state of the output pin
int reading2;           // the current reading from the input pin
int previous2 = LOW;    // the previous reading from the input pin

//States for Relay and Button (3)

int state3 = HIGH;      // the current state of the output pin
int reading3;           // the current reading from the input pin
int previous3 = LOW;    // the previous reading from the input pin

//States for Relay and Button (4)

int state4 = HIGH;      // the current state of the output pin
int reading4;           // the current reading from the input pin
int previous4 = LOW;    // the previous reading from the input pin




// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time1 = 0;          // the last time the output pin was toggled
long time2 = 0;
long time3 = 0;
long time4 = 0;

long debounce1 = 200;   // the debounce time, increase if the output flickers
long debounce2 = 200;
long debounce3 = 200;
long debounce4 = 200;


void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);

  pinMode(rl1, OUTPUT);
  pinMode(rl2, OUTPUT);
  pinMode(rl3, OUTPUT);
  pinMode(rl4, OUTPUT);

}

void loop() {

  reading1 = digitalRead(button1);
  reading2 = digitalRead(button2);
  reading3 = digitalRead(button3);
  reading4 = digitalRead(button4);


  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  //Condition Relay 1
  if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
    if (state1 == HIGH)
      state1 = LOW;
    else
      state1 = HIGH;

    time1 = millis();    
  }

  //Condition Relay 2
    if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce2) {
    if (state2 == HIGH)
      state2 = LOW;
    else
      state2 = HIGH;

    time2 = millis();    
  }

  //Condition Relay 3
    if (reading3 == HIGH && previous3 == LOW && millis() - time3 > debounce3) {
    if (state3 == HIGH)
      state3 = LOW;
    else
      state3 = HIGH;

    time3 = millis();    
  }

  //Condition Relay 4
    if (reading4 == HIGH && previous4 == LOW && millis() - time4 > debounce4) {
    if (state4 == HIGH)
      state4 = LOW;
    else
      state4 = HIGH;

    time4 = millis();    
  }




  digitalWrite(rl1, state1);
  digitalWrite(rl2, state2);
  digitalWrite(rl3, state3);
  digitalWrite(rl4, state4);


  previous1 = reading1;
  previous2 = reading2;
  previous3 = reading3;
  previous4 = reading4;
}

"Solved, here is the solution I know a lot of people are looking for it:"

If you'd asked PaulRB "what do you mean about 'INPUT_PULLUP', PaulRB?" then you'd have found there's a way to get by without adding those 130ohm resistors.

Every time you activate a switch you run about 40mA down the drain.

I tried INPUT_PULLUP and it does not work for a toggle, probably it's just my board, I am working with a clone

With INPUT_PULLUP your activated ("button-pushed") condition is LOW (not HIGH).

Then I will try again and see if it works, the simpler the better!