Dim LEDs in IF/ELSE but not in IF ***SOLVED***

Coming back from a while away from Arduino so I seem to have gotten rusty.

I'm using these IR sensors to turn on an an LED. 1 sensor to 1 LED. When using this first set of code, the LED comes on at full brightness, obviously it stays on as there's no code to turn off.

 int rSensor = A0;
int gSensor = A1;
int wSensor = A2;
int rLed = 9;
int gLed = 8;
int wLed = 10;


void setup() {
Serial.begin (9600);  
pinMode (rLed, OUTPUT);
pinMode (gLed, OUTPUT);
pinMode (wLed, OUTPUT);
pinMode (rSensor, INPUT);
pinMode (gSensor, INPUT);
pinMode (wSensor, INPUT);
}

void loop() {

int rSensor = analogRead(A0);
int gSensor = analogRead(A1);
int wSensor = analogRead(A2);

Serial.println(rSensor);
if (rSensor < 200){
  digitalWrite(rLed, HIGH);
 }

if (gSensor < 200){
  digitalWrite(gLed, HIGH);
 }

 if (wSensor < 200){
  digitalWrite(wLed, HIGH);
 }
 
}

But when i use an ELSE function to turn the light back off when the sensor stops sending a signal, the LEDs only come on very faintly.

int rSensor = A0;
int gSensor = A1;
int wSensor = A2;
int rLed = 9;
int gLed = 8;
int wLed = 10;


void setup() {
Serial.begin (9600);  
pinMode (rLed, OUTPUT);
pinMode (gLed, OUTPUT);
pinMode (wLed, OUTPUT);
pinMode (rSensor, INPUT);
pinMode (gSensor, INPUT);
pinMode (wSensor, INPUT);
}

void loop() {

int rSensor = analogRead(A0);
int gSensor = analogRead(A1);
int wSensor = analogRead(A2);

Serial.println(rSensor);
if (rSensor < 200){
  digitalWrite(rLed, HIGH);
 }
 
else (rSensor > 300);
 {
  digitalWrite(rLed, LOW);
 }
if (gSensor < 200){
  digitalWrite(gLed, HIGH);
 }
 
else (gSensor > 300);
 {
  digitalWrite(gLed, LOW);
 }
 if (wSensor < 200){
  digitalWrite(wLed, HIGH);
 }
 
else (wSensor > 300);
 {
  digitalWrite(wLed, LOW);
 }
}

The LEDs are each wired 5v in, out to a 330 ohm resistor, and then to GND. I'm just struggling to see why the use of the ELSE function makes them only light up very dimly.

Thanks

One thing that I would do is not to have a global variable named rSensor which is a pin number with a value of A0 (ie 14) and a local variable of the same name that is the value read from that pin.

When you write

 if (rSensor < 200)

which of the two variables with the same name is it testing ?

As to your problem

  if (rSensor < 200)
  {
    digitalWrite(rLed, HIGH);
  }
  else (rSensor > 300);  //shouldn't this be else if ?
  {
    digitalWrite(rLed, LOW);
  }

See my comment in the code above

You forgot to use Auto Format on your code. What the compiler sees is:

  if (rSensor < 200)
  {
    digitalWrite(rLed, HIGH);
  }
  else 
    (rSensor > 300);  // This line does nothing but is a legal expression statement.

  // This block is NOT inside the 'else' clause so it is unconditional
  {
    digitalWrite(rLed, LOW);
  }

Your LED is always getting turned off, even is it was just turned on. That is why it is dim.

UKHeliBob:
One thing that I would do is not to have a global variable named rSensor which is a pin number with a value of A0 (ie 14) and a local variable of the same name that is the value read from that pin.

When you write

 if (rSensor < 200)

which of the two variables with the same name is it testing ?

Yes I've altered the code to have separate variables, thank you. It's taking me a while to get back into the swing of things!

johnwasser:
You forgot to use Auto Format on your code.
.
.
.
.
Your LED is always getting turned off, even is it was just turned on. That is why it is dim.

Oh thank you! I assumed it was someting in my code turning it on and off but I couldn't see where i went wrong.

Now onto trying to figure out how to fade an LED from inside an if function. So many things to learn!!!