Channel select function

I think I just haven't gotten to this part of the tutorial... I got the code to do most of what I want it to do, but how do I get only one to be on at a time now?

//channel one
reading1 = digitalRead(button1);
if (reading1 == HIGH && previous1 == LOW && millis() - time > debounce) {
if (state1 == HIGH)
state1 = LOW;
else
state1 = HIGH;

time = millis();
}
digitalWrite(relay1, state1);
previous1 = reading1;

//channel two
reading2 = digitalRead(button2);
if (reading2 == HIGH && previous2 == LOW && millis() - time > debounce) {
if (state2 == HIGH)
state2 = LOW;
else
state2 = HIGH;

time = millis();
}
digitalWrite(relay2, state2);
previous2 = reading2;

//channel three
reading3 = digitalRead(button3);
if (reading3 == HIGH && previous3 == LOW && millis() - time > debounce) {
if (state3 == HIGH)
state3 = LOW;
else
state3 = HIGH;

time = millis();
}
digitalWrite(relay3, state3);
previous3 = reading3;

//channel four
reading4 = digitalRead(button4);
if (reading4 == HIGH && previous4 == LOW && millis() - time > debounce) {
if (state4 == HIGH)
state4 = LOW;
else
state4 = HIGH;

time = millis();
}
digitalWrite(relay4, state4);
previous4 = reading4;
}

after you code

else
      state1 = HIGH;
      state2 = LOW;
      state3 = LOW;
      state4 = LOW;

force the others low after turning the one on you want to have on

but how do I get only one to be on at a time now?

Start with some CLEAR requirements. That's a whine, not a requirement.

If 3 switches are pressed, which ONE pin do you want on?

I moved some things around to help with the visibility and made a simple change

I missed the { and } to cause all to change with the on state1 state in my earlier example

consider using code tage </> button top left when posting code you will get a better response form this community :slight_smile:

else
  {
      state1 = HIGH;
      state2 = LOW;
      state3 = LOW;
      state4 = LOW;
  }
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

  //channel one
  reading1 = digitalRead(button1);
  if (reading1 == HIGH && previous1 == LOW && millis() - time > debounce) {
    if (state1 == HIGH)
      state1 = LOW;
    else {
      state1 = HIGH;
      state2 = LOW;
      state3 = LOW;
      state4 = LOW;
    }
    time = millis();
  }

  previous1 = reading1;

  //channel two
  reading2 = digitalRead(button2);
  if (reading2 == HIGH && previous2 == LOW && millis() - time > debounce) {
    if (state2 == HIGH)
      state2 = LOW;
    else {
      state1 = LOW;
      state2 = HIGH;
      state3 = LOW;
      state4 = LOW;
    }
    time = millis();
  }

  previous2 = reading2;

  //channel three
  reading3 = digitalRead(button3);
  if (reading3 == HIGH && previous3 == LOW && millis() - time > debounce) {
    if (state3 == HIGH)
      state3 = LOW;
    else {
      state1 = LOW;
      state2 = LOW;
      state3 = HIGH;
      state4 = LOW;
    }

    time = millis();
  }
  previous3 = reading3;

  //channel four
  reading4 = digitalRead(button4);
  if (reading4 == HIGH && previous4 == LOW && millis() - time > debounce) {
    if (state4 == HIGH)
      state4 = LOW;
    else
    {
      state1 = LOW;
      state2 = LOW;
      state3 = LOW;
      state4 = HIGH;
    }
    time = millis();
  }
  previous4 = reading4;


  digitalWrite(relay1, state1);
  digitalWrite(relay2, state2);
  digitalWrite(relay3, state3);
  digitalWrite(relay4, state4);
}

This should work
Z

I think I would simplify that by reading all the buttons and using a single debounce interval for all of them.

Then set all the outputs LOW before setting whichever ones need to be HIGH depending on the buttons (or vice versa if the default state is HIGH).

...R

Thank you everybody for the help! I am sorry for not using the proper “format” for this forum earlier. I am new. I did run the code and it worked perfectly!