Why all 3 LEDs light up together?

I have 3 sensors that output 0-1024 values and they are connected to an Arduino, which i want to light up 3 LEDs, each LED when a corresponding sensor sends a value over a certain threshold.

My problem is, that every time 1 of the sensors goes over the threshold value, all the 3 LEDs light up, instead of only one.

What could be the problem?

Here is the code:

int RedLED = 5;
int GreenLED = 6;
int BlueLED = 7;

int RedLEDinput = A0;
int GreenLEDinput = A1;
int BlueLEDinput = A2;

#define Threshold 500

void setup() {

   pinMode(RedLED, OUTPUT); 
   pinMode(GreenLED, OUTPUT); 
   pinMode(BlueLED, OUTPUT); 
   pinMode(RedLEDinput,INPUT);
   pinMode(GreenLEDinput,INPUT);
   pinMode(BlueLEDinput,INPUT);

}

void loop() {
  
  if (analogRead(RedLEDinput)>=Threshold){
  digitalWrite(RedLED, HIGH);
  } 
  
  else if (analogRead(RedLEDinput)<Threshold){
    digitalWrite(RedLED, LOW);    
  }

   if (analogRead(GreenLEDinput)>=Threshold){
   digitalWrite(GreenLED, HIGH);   
  } 
 
  else if (analogRead(GreenLEDinput)<Threshold){
   digitalWrite(GreenLED, LOW);    
  }
 
  if (analogRead(BlueLEDinput)>=Threshold){
    digitalWrite(BlueLED, HIGH); 
  }
   
  else if (analogRead(BlueLEDinput)<Threshold){ 
    digitalWrite(BlueLED, LOW);   
   }

  
 }

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

What type of sensors are you using ?

Please post a photo of a hand drawn wiring diagram, with pins and connections clearly labeled.

I have 3 sensors that output 0-1024 values

For input to analogRead(), the sensors need to output a voltage between 0 and the analog reference voltage. Post a link to the sensors.

I suggest to use the serial monitor to display the values and see what is wrong.
In setup add:

Serial.begin(9600);

In loop add:

Serial.print("RedLEDinput=");
Serial.println(analogRead(RedLEDinput));
...

Recompile & run with the serial monitor on.

Your sketch is good. You have a wiring issue. Here is a simulation.

No.

I appreciate the effort to create a wowki-simulation

But it is improvable .
the wires should not cross more than nescessary

best regards Stefan

1 Like

Hello

Try, check and study these mods to you sketch.

int RedLED = 5;
int GreenLED = 6;
int BlueLED = 7;

int RedLEDinput = A0;
int GreenLEDinput = A1;
int BlueLEDinput = A2;
#define Threshold 500
void setup() {
  pinMode(RedLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(BlueLED, OUTPUT);
  pinMode(RedLEDinput, INPUT);
  pinMode(GreenLEDinput, INPUT);
  pinMode(BlueLEDinput, INPUT);
}
void loop() {
  digitalWrite(RedLED, (analogRead(RedLEDinput) > Threshold));
  digitalWrite(GreenLED, (analogRead(GreenLEDinput) > Threshold));
  digitalWrite(BlueLED, (analogRead(BlueLEDinput) > Threshold));
}

Have a nice day and enjoy coding in C++.

That was correct, i used wrong wiring on the LEDs circuit.

Thanks for all the people who tried to help.

Art is never finished. : ) I did not clean-up when I moved the Nano, and clarity is imperative.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.