more than one LED lighting up in sensor application

Hi, I have modified Jeremy Blum's nightlight and temp alert sketches to combine them into one sketch.
The correct LEDs will light with the temp alert (red- overtemp, blue- undertemp, green- just right) and with the nightlight, (shade the photoresistor the yellow LED lights).
The problem is that when I shade the photoresistor the yellow LED and the Red LED will BOTH light.

I have looked at the code and can't seem to see the mistake (but I am new so it could be there!)
Any advice would be greatly appreciated.

Here is the code:

/*
Exploring Arduino - Code Listing 3-3: Automatic Night Light Sketch
http://www.exploringarduino.com/content/ch3

Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com )
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
*/

//Light & Temperature sensor

const int YLED=6;        //Yellow LED on pin 6 (PWM)
const int BLED=9;          //Blue LED on Pin 9 (PWM)
const int GLED=10;         //Green LED on Pin 10 (PWM)
const int RLED=11;         //Red LED on Pin 11 (PWM)
const int LIGHT=1;       //Lght Sensor on Analog Pin A1
const int MIN_LIGHT=10; //Minimum expected light value
const int MAX_LIGHT=500; //Maximum Expected Light value
const int TEMP=0;          //Temp Sensor is on Analog pin A0
const int LOWER_BOUND=139; //Lower Temp Threshold
const int UPPER_BOUND=160; //Upper Temp Threshold


int val = 0;             //variable to hold the light sensor analog reading
int val1 = 0;            //variable to hold the temperature analog reading

void setup()
{
  pinMode (YLED, OUTPUT); //Set Yellow LED pin as output
  pinMode (BLED, OUTPUT); //Set Blue LED as Output
  pinMode (GLED, OUTPUT); //Set Green LED as Output
  pinMode (RLED, OUTPUT); //Set Red LED as Output
}

void loop()
{
  val = analogRead(LIGHT);                      //read the light sensor
  val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0); //map the light reading
  val = constrain(val, 0, 255);                 //constrain light value
  analogWrite(YLED, val);                       //control the LED

  val1 = analogRead(TEMP);
 
  if (val1 < LOWER_BOUND)
  {
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, HIGH);
  }
  else if (val1 > UPPER_BOUND)
  {
    digitalWrite(RLED, HIGH);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, LOW);
  }
  else
  {
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, HIGH);
    digitalWrite(BLED, LOW);
  }
}

Please provide schematics or a photo of your wiring.

This block of code:-

else
  {
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, HIGH);
    digitalWrite(BLED, LOW);
  }

Is done straight after this block:-

{
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, HIGH);
  }

So basically you have got your if then else structure wrong.

Grumpy_Mike:
This block of code:-

else

{
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, HIGH);
    digitalWrite(BLED, LOW);
  }



Is done straight after this block:-


{
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, HIGH);
  }




So basically you have got your if then else structure wrong.

I disagree with your findings. There is a "if {} else if {} else {}" construct which does more or less what he probably expect it to do.
Please explain why you think that the else doesn't work as an else.

It's quite simple. You use val to control your nightlight.
AT NO TIME do you consider this setting when lighting up the other LEDS. Instead they are just illuminated according to the value of val1

Doesn't matter wether it's night or day, those coloured lights are going to light.

/*
Exploring Arduino - Code Listing 3-3: Automatic Night Light Sketch

Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com )
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
*/

//Light & Temperature sensor

const int YLED=6; //Yellow LED on pin 6 (PWM)
const int BLED=9; //Blue LED on Pin 9 (PWM)
const int GLED=10; //Green LED on Pin 10 (PWM)
const int RLED=11; //Red LED on Pin 11 (PWM)
const int LIGHT=1; //Lght Sensor on Analog Pin A1
const int MIN_LIGHT=10; //Minimum expected light value
const int MAX_LIGHT=500; //Maximum Expected Light value
const int TEMP=0; //Temp Sensor is on Analog pin A0
const int LOWER_BOUND=139; //Lower Temp Threshold
const int UPPER_BOUND=160; //Upper Temp Threshold

int val = 0; //variable to hold the light sensor analog reading
int val1 = 0; //variable to hold the temperature analog reading

void setup()
{
pinMode (YLED, OUTPUT); //Set Yellow LED pin as output
pinMode (BLED, OUTPUT); //Set Blue LED as Output
pinMode (GLED, OUTPUT); //Set Green LED as Output
pinMode (RLED, OUTPUT); //Set Red LED as Output
}

void loop()
{
val = analogRead(LIGHT); //read the light sensor
val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0); //map the light reading
val = constrain(val, 0, 255); //constrain light value
analogWrite(YLED, val); //control the LED

val1 = analogRead(TEMP);

if (val1 < LOWER_BOUND)
{
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
}
else if (val1 > UPPER_BOUND)
{
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
}
else
{
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
}
}

KenF:
It's quite simple. You use val to control your nightlight.
AT NO TIME do you consider this setting when lighting up the other LEDS. Instead they are just illuminated according to the value of val1

Doesn't matter wether it's night or day, those coloured lights are going to light.

val and val1 control different things, one monitors the photoresistor voltage (A1), the other monitors the temp sensor (A0). What I can't figure out is why when I shade the photoresistor connected to A1, the red LED will light up. It is supposed to respond only to changes in val1, the temp sensor on A0

Putting in a 10 ms delay after the val and val1 analogRead statements solved it.

val = analogRead(LIGHT);    //read the light sensor
delay(10);  //delay 10 ms
val = analogRead(LIGHT);    //read the light sensor
delay(10);  //delay 10 ms

Thanks to the post by jondecker76 in the reading multiple analog inputs post.