Need hlep making code work

I am trying to make 5 relays work from 5 switches it works but the problem i’m having is all 5 outputs activate at the same time with any one of the 5 inputs i need each one to work separately. Would appreciate any help with what i’m doing wrong.

Arduino Nano
8 channel relay board

int R1 = 12; //Relay1
int S1 = 6; //Switch1
int R2 = 11; //Relay2
int S2 = 5; //Switch2
int R3 = 10; //Relay3
int S3 = 4; //Switch3
int R4 = 9; //Relay4
int S4 = 3; //Switch4
int R5 = 8; //Relay5
int S5 = 2; //Switch5

int state = HIGH;
int reading;
int previous = LOW;

long time = 0;
long debounce = 200;

void setup() {

pinMode(R1, OUTPUT);
pinMode(S1, INPUT_PULLUP);
pinMode(R2, OUTPUT);
pinMode(S2, INPUT_PULLUP);
pinMode(R3, OUTPUT);
pinMode(S3, INPUT_PULLUP);
pinMode(R4, OUTPUT);
pinMode(S4, INPUT_PULLUP);
pinMode(R5, OUTPUT);
pinMode(S5, INPUT_PULLUP);
}

void loop() {
reading = digitalRead(S1);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}
digitalWrite(R1, state);
previous = reading;

reading = digitalRead(S2);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}
digitalWrite(R2, state);
previous = reading;

reading = digitalRead(S3);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}
digitalWrite(R3, state);
previous = reading;

reading = digitalRead(S4);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}
digitalWrite(R4, state);
previous = reading;

{ reading = digitalRead(S5);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}
digitalWrite(R5, state);
previous = reading;
}
}

Latching1.txt (2 KB)

To start with, each switch needs its own copy of previous and time.

Also an easy way to toggle the state is to say 'state = !state'.

Thanks for the replay can you give me a example of each switch needing its own copy

Hello Rbarn1974,
Welcome to the forum. You will find the folk here more friendly and more helpful if you follow the forum rules. Please read again (I'm sure you have already read them once) 'how to use this forum - please read'. The go back and modify your initial post according to the instructions in part 7.

Thank you.

Rbarn1974:
I am trying to make 5 relays work from 5 switches it works but the problem i’m having is all 5 outputs activate at the same time with any one of the 5 inputs i need each one to work separately. Would appreciate any help with what i’m doing wrong.

When you have five of something you should almost certainly use arrays instead of declaring five sets of variables and writing the same code five times.

const byte ChannelCount = 5;
const byte InputPins[ChannelCount] = {6, 5, 4, 3, 2};
const byte OutputPins[ChannelCount] = {12, 11, 10, 9, 8};


boolean OutputState[ChannelCount] = {false};
boolean PreviousButtonState[ChannelCount] = {false};


const unsigned long debounce = 200;
unsigned long previousStateChangeTime = 0;




void setup()
{
  for (int i = 0; i < ChannelCount; i++)
  {
    pinMode(OutputPins[i], OUTPUT);
    digitalWrite(OutputPins[i], HIGH);  // Relay Off
    pinMode(InputPins[i], INPUT_PULLUP);
  }
}


void loop()
{
  for (int i = 0; i < ChannelCount; i++)
  {
    boolean state = digitalRead(InputPins[i]);


    if (state != PreviousButtonState[i] && millis() - previousStateChangeTime > debounce)
    {
      previousStateChangeTime = millis();
      PreviousButtonState[i] = state;


      if (state)  // Just went 'active'
      {
        OutputState[i] = !OutputState[i];
        digitalWrite(OutputPins[i], OutputState[i] ? LOW : HIGH);
      }
    }
  }
}

Thank you for the for the help i'm trying to learn this coding but sometimes you just need a little help and I apologize for not posting correctly will make sure to get it right next time Thanks again.

Rbarn1974:
Thank you for the for the help i'm trying to learn this coding but sometimes you just need a little help and I apologize for not posting correctly will make sure to get it right next time Thanks again.

No problem. We were all beginners once.

Your digitalWrite() statements are outside of the if statements, so they are all being executed every time the code executes loop(), regardless of which switch set the state variable.