I am witting a program that takes input from a photo resistor and turn on either a green LED or a RED led. I have a range set up between 130 and 150 that should turn on the green LED and if the level is outside of that range, a red LED is supposed to turn on.
Here is my code:
//#include <wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int photoSens = 0;
const int redLed = 2;
const int greenLed = 3;
const int blueLed = 4;
int lightOkay();
int lightBad();
void setup() {
Serial.begin(9600);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(photoSens, INPUT);
lcd.begin(16,2);
for(int i = 0; i< 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight();
}
void loop() {
int lightLevel;
lightLevel = analogRead(photoSens);
lightLevel = map(lightLevel, 0, 1023, 1, 255);
lightLevel = constrain(lightLevel, 1 , 255);
Serial.print("LightLevel");
Serial.println(lightLevel);
delay (300);
if((lightLevel >130) && (lightLevel < 150))
{
lightOkay();
}
else if((lightLevel < 129) || (lightLevel > 151))
{
lightBad();
}
}
int lightOkay()
{
Serial.println ("GreenON");
delay (500);
digitalWrite(greenLed, HIGH);
}
int lightBad()
{
Serial.println("RedON");
delay(500);
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
}
If you look at the light level in Serial Mon 1 you can see that the light level falls into the range where the green LED is on and GREENON, but REDON is being printed to the screen.
I have no clue as to why this is happening. My instructor thinks there is something wrong with the logic, but I am not so sure.
any help would be greatly appreciated.

