Multiple Inputs with If Statements Doesn't Output Correctly

Ok, this should be an easy one.

The Goal:
I have 4 reed switches, each controlling a different color (R,B,G and Purple) LED strand via a relay. The strands are separate, I'm not sharing LEDs.
Anytime all switches are 'off', the R, B and G strands should be lit (Purple should be off)
When Switch 1 is toggled on, the red strand should light, and the others should be off.
Similarly:
Switch 2 turns on only Blue
Switch 3 turns on only Green
Switch 4 turns on only Purple

I can get this to work with just Red and Blue, but as soon as I add either of the other colors, the behavior gets bizarre.

Here is the code - can you tell me what I'm doing wrong?
I've commented out the additional lights in this example.

const int SWITCH1 = 13; //Set Switch 1 Pin
const int SWITCH2 = 12; //Set Switch 2 Pin
//const int SWITCH3 = 11; //Set Switch 3 Pin
//const int SWITCH4 = 10; //Set Switch 4 Pin
const int LED1 = A5; //Set RED LED Relay Pin
const int LED2 = A4; //Set GREEN LED Relay Pin
//const int LED3 = A3; //Set BLUE LED Relay Pin
//const int LED4 = A4; //Set Purple LED Relay Pin

int SWITCHstate1 = 0;
int SWITCHstate2 = 0;
//int SWITCHstate3 = 0;
//int SWITCHstate4 = 0;

void setup()
{
  pinMode(SWITCH1, INPUT);
  pinMode(SWITCH2, INPUT);
//  pinMode(SWITCH3, INPUT);
//  pinMode(SWITCH4, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
//  pinMode(LED3, OUTPUT);
//  pinMode(LED4, OUTPUT);
}

void loop()
{
  SWITCHstate1 = digitalRead(SWITCH1);
  if (SWITCHstate1 == HIGH)
  {
    digitalWrite(LED1, HIGH); //Turn RED LED off
    digitalWrite(LED2, LOW); //Turn GREEN LED on
//    digitalWrite(LED3, HIGH); //Turn BLUE LED off
//    digitalWrite(LED4, HIGH); //Turn PURPLE LED off
  } 
  else{
    digitalWrite(LED1, LOW);
  }
  SWITCHstate2 = digitalRead(SWITCH2);
  if (SWITCHstate2 == HIGH)
  {
    digitalWrite(LED1, LOW); //Turn RED LED on
    digitalWrite(LED2, HIGH); //Turn GREEN LED off
 //   digitalWrite(LED3, HIGH); //Turn BLUE LED off
 //   digitalWrite(LED4, HIGH); //Turn PURPLE LED off
  } 
  else{
    digitalWrite(LED2, LOW);
  }
/*  SWITCHstate3 = digitalRead(SWITCH3);
  if (SWITCHstate3 == HIGH)
  {
    digitalWrite(LED1, HIGH); //Turn RED LED off
    digitalWrite(LED2, HIGH); //Turn GREEN LED off
    digitalWrite(LED3, LOW); //Turn BLUE LED on
    digitalWrite(LED4, HIGH); //Turn PURPLE LED off
  } 
  else{
    digitalWrite(LED3, LOW);
  }
  SWITCHstate4 = digitalRead(SWITCH4);
  if (SWITCHstate4 == HIGH)
  {
    digitalWrite(LED1, HIGH); //Turn RED LED off
    digitalWrite(LED2, HIGH); //Turn GREEN LED off
    digitalWrite(LED3, HIGH); //Turn BLUE LED off
    digitalWrite(LED4, LOW); //Turn PURPLE LED on
  } 
  else{
    digitalWrite(LED4, HIGH);
  }
  */
}

With read switches, your inputs will be in undefined state if the switch is open and connected to 5V.
Use your read switches to connect the input to GND and use the internal pullup resistor in the AVR to tie the input to VCC if the switch is open.
You do this by replacing pinMode(INPUT) by pinMode(INPUT_PULLUP).

Hello
Your description is calling for the ussage of CPP, with an object for the button and related led.

Hi, @rygard
Welcome to the forum.

Can you please post a circuit diagram of your project?
Not a Fritzy image.

Showing your power supply, component labels and pin names.

Sound like the ideal use for switch.. case..

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

You need to read all 4 switches first, so that you can identify the case where all are off.

Something like:

  // collect the state of all the switches
  bool sw1_on = digitalRead (SWITCH1) ;
  bool sw2_on = digitalRead (SWITCH2) ;
  bool sw3_on = digitalRead (SWITCH3) ;
  bool sw4_on = digitalRead (SWITCH4) ;
  // figure out if they are all off
  bool all_off = !sw1_on && !sw2_on && !sw3_on && !sw4_on ;
  digitalWrite (LED1, !(all_off || sw2_on)) ; // red
  digitalWrite (LED2, !(all_off || sw1_on)) ; // green
  digitalWrite (LED3, !(all_off || sw3_on)) ; // blue
  digitalWrite (LED4, !sw4_on) ; // purple

Note the use of Boolean (truth) valued variables and the negation operator !
From your original code I determined the switch pins are active HIGH and the LED pins active low.

const int SWITCH1 = 13; //Set Switch 1 Pin
const int SWITCH2 = 12; //Set Switch 2 Pin
const int SWITCH3 = 11; //Set Switch 3 Pin
const int SWITCH4 = 10; //Set Switch 4 Pin
const int RED_LED = A5; //Set RED LED Relay Pin
const int GRN_LED = A4; //Set GREEN LED Relay Pin
const int BLU_LED = A3; //Set BLUE LED Relay Pin
const int PUR_LED = A2; //Set Purple LED Relay Pin

void setup()
{
  pinMode(SWITCH1, INPUT);
  pinMode(SWITCH2, INPUT);
  pinMode(SWITCH3, INPUT);
  pinMode(SWITCH4, INPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(GRN_LED, OUTPUT);
  pinMode(BLU_LED, OUTPUT);
  pinMode(PUR_LED, OUTPUT);
}

void loop()
{
  if (digitalRead(SWITCH1) == HIGH)
  {
    digitalWrite(RED_LED, HIGH); //Turn RED LED off
    digitalWrite(GRN_LED, LOW); //Turn GREEN LED on
    digitalWrite(BLU_LED, HIGH); //Turn BLUE LED off
    digitalWrite(PUR_LED, HIGH); //Turn PURPLE LED off
  } 
  else if (digitalRead(SWITCH2) == HIGH)
  {
    digitalWrite(RED_LED, LOW); //Turn RED LED on
    digitalWrite(GRN_LED, HIGH); //Turn GREEN LED off
    digitalWrite(BLU_LED, HIGH); //Turn BLUE LED off
    digitalWrite(PUR_LED, HIGH); //Turn PURPLE LED off
  } 
  else if (digitalRead(SWITCH3) == HIGH)
  {
    digitalWrite(RED_LED, HIGH); //Turn RED LED off
    digitalWrite(GRN_LED, HIGH); //Turn GREEN LED off
    digitalWrite(BLU_LED, LOW); //Turn BLUE LED on
    digitalWrite(PUR_LED, HIGH); //Turn PURPLE LED off
  } 
  else if (digitalRead(SWITCH4) == HIGH)
  {
    digitalWrite(RED_LED, HIGH); //Turn RED LED off
    digitalWrite(GRN_LED, HIGH); //Turn GREEN LED off
    digitalWrite(BLU_LED, HIGH); //Turn BLUE LED off
    digitalWrite(PUR_LED, LOW); //Turn PURPLE LED on
  } 
  else
  {
    digitalWrite(RED_LED, LOW); //Turn RED LED on
    digitalWrite(GRN_LED, LOW); //Turn GREEN LED on
    digitalWrite(BLU_LED, LOW); //Turn BLUE LED on
    digitalWrite(PUR_LED, HIGH); //Turn PURPLE LED off
  }
}

Simples!

1 Like

Ok, I think I've drawn this correctly. I'm using the 5v relay modules, so the center line is the trigger from the arduino.

I gave this a try, it only functions on the pin 13 switch. :frowning:

Hi,
Write some code that JUST switches the relays ON and OFF in a cycle to prove you can control them.

Then write some code that turns just one relay ON when you press ANY button, to prove you have proper button input.

In other words write simple code to test your hardware before attacking software.

Why are you using RESET?
Use 5V on your pullup resistors, UNO is 5V logic.
Use 10K or 4k7 resistors, NOT 100R, they will draw too much current from your supply rail.

Tom.... :smiley: :+1: :coffee: :australia:

Well, I did do that. The system works with the initial code when I'm just using 2 switch/relay pairs. When I try to run 3 or 4 it fails.

what did you do exactly?
If you changed your circuit - update the schematic and upload it so we can se what you have done.
If it still doesn't work provide real pictures of your setup where we can see each single wire / connection.

This code works perfectly on a single switch/relay/LED setup

const int SWITCH1 = 13; //Set Switch 1 Pin
const int LED1 = A5; //Set RED LED Relay Pin
int SWITCHstate1 = 0;

void setup() {
  pinMode(SWITCH1, INPUT);
  pinMode(LED1, OUTPUT);
}

void loop() {
  SWITCHstate1 = digitalRead(SWITCH1);
  if (SWITCHstate1 == HIGH)
  {
    digitalWrite(LED1, HIGH); //Turn RED LED on
}
  else{
    digitalWrite(LED1, LOW); //Turn RED LED off
  }
}

Hi,

Good , now expand it to do each button, lets check ALL that your buttons are being read.

Tom... :smiley: :+1: :coffee: :australia:

Ok, so this code successfully turns on and off each relay with the use of the proper switch. So now what I need to do is make it so that when no switch is active, LED1, LED2, and LED3 are on and LED4 are off.

Then everything need to turn off and the active switch should turn on its color by itself, and this is where I'm running into issues.

const int SWITCH1 = 13; //Set Switch 1 Pin
const int SWITCH2 = 12; //Set Switch 2 Pin
const int SWITCH3 = 11; //Set Switch 3 Pin
const int SWITCH4 = 10; //Set Switch 4 Pin
const int LED1 = A5; //Set RED LED Relay Pin
const int LED2 = A4; //Set GREEN LED Relay Pin
const int LED3 = A3; //Set BLUE LED Relay Pin
const int LED4 = A2; //Set PURPLE LED Relay Pin
int SWITCHstate1 = 0;
int SWITCHstate2 = 0;
int SWITCHstate3 = 0;
int SWITCHstate4 = 0;

void setup() {
  pinMode(SWITCH1, INPUT);
  pinMode(SWITCH2, INPUT);
  pinMode(SWITCH3, INPUT);
  pinMode(SWITCH4, INPUT);  
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
}

void loop() {
  SWITCHstate1 = digitalRead(SWITCH1);
  if (SWITCHstate1 == HIGH)
  {
    digitalWrite(LED1, HIGH); //Turn RED LED on
}
  else{
    digitalWrite(LED1, LOW); //Turn Red LED off
  }
  
  SWITCHstate2 = digitalRead(SWITCH2);
  if (SWITCHstate2 == HIGH)
  {
    digitalWrite(LED2, HIGH); //Turn GREEN LED on
}
  else{
    digitalWrite(LED2, LOW); //Turn GREEN LED off
  }
    
  SWITCHstate3 = digitalRead(SWITCH3);
  if (SWITCHstate3 == HIGH)
  {
    digitalWrite(LED3, HIGH); //Turn BLUE LED on
}
  else{
    digitalWrite(LED3, LOW); //Turn BLUE LED off
  }
      
  SWITCHstate4 = digitalRead(SWITCH4);
  if (SWITCHstate4 == HIGH)
  {
    digitalWrite(LED4, HIGH); //Turn PURPLE LED on
}
  else{
    digitalWrite(LED4, LOW); //Turn PURPLE LED off
  }
}

Use the code in reply #6. It is very simple code and should do exactly what you specified. If you still feel it's not working, describe in more detail what happens and what you expected to happen. And post an updated/corrected schematic and some bright, well focussed pics showing clearly how every wire and component is connected.

@PaulRB I tried that code, and it was unsuccessful. It turns on LED2 (Green) when all switches are low, and only SWITCH1 works, it turns off LED2 and turns on LED1. Switch2, 3 and 4 do nothing, and LED 3/4 do nothing.
The schematic I uploaded is correct and complete. I can try to get some pics of the hardware (its in a solderless board, so its pretty jumbled looking), but as I mention above, each switch and relay work correctly when individually triggered.

I can say with some confidence that the wiring is correct to the schematic, but if you really think its necessary, I can tidy it up for some pics.

I very seriously doubt there is a problem with the code I posted, it is so simple, there is little to go wrong.

Your schematic may or may not reflect your actual circuit, but as has been pointed out already, indicates some potential problems: use of the reset pin, strange "3.3V" markings, pull-up resistors which are uneccessarily low, unusual use of the "NC" relay connections. It is likely your problems are hardware if the code from post #6 does not seem to be working. Hopefully an updated schematic and pics will reveal the real problem here.

Ok, I fixed that schematic, I had made a couple 'typos (e.g the reset and NC)', and reposted above. The resistors are 100ohm, what do you suggest I use instead?
nevermind, I see @TomGeorge 's post above regarding the resistors, I'll upgrade those to something more substantial.

Can I suggest you do not do that again. Anyone trying to follow this topic can no longer see your previous attempt, so the comments made by other forum members about the problems with that earlier version no longer make sense. Always post updated schematics/code in a new post at the bottom of the topic.

Pay attention to @TomGeorge 's other comments, as well as the suggested resistor values.

Will do, thanks.