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);
}
}