Hi there,
I am attempting to read my photocell once and set a value that doesn't change to int "ambient" and use that as the baseline for my evaluation somewhere at the beginning of the program, maybe 3 seconds after power up. Right now I have to look at my serial monitor and set the value (in the case of the current program "32"). I would want to replace the "32" value with "ambient" so that the program functions based on the light level of the environment it powers in. I believe one problem is that I am using the delay to set the point at which the value is measured and assigned, but I'm not sure. The program seems to set the value to zero, so the program reads Normal at 0, Warning slightly above and Danger above that. Program functions fine when I manually set the level. I think this should be fairly simple but I haven't been able to figure it out. Thanks in advance for any advice!
const int sensorPin = 0;
const int ledPin0 = 8;
const int ledPin1 = 9;
int lightLevel, high = 0, low = 1023;
int ambient;
#include <Wire.h> // include libraries for LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
pinMode (sensorPin, INPUT); // set A0 to input
pinMode (ledPin0, OUTPUT); // set pins to control output to LED
pinMode (ledPin1, OUTPUT);
Serial.begin(9600);
lcd.begin(20,4);
}
void loop() {
lightLevel = analogRead(sensorPin); // name reading from light sensor lightLevel
Serial.print("Light Level "); // identifies light level in serial
Serial.println(lightLevel); // prints light level to serial
lightProtocol(); // call subroutines
outRange();
}
void lightProtocol() {
lightLevel = map(lightLevel, 605, 1020, 0, 64); // reduce range of light levels for stability
lightLevel = constrain(lightLevel, 0, 64);
Serial.print("Constrained " ); // name constrained result in serial
Serial.println(lightLevel); // output constrained light level
delay (100);
/* if ((delay < 100) && (delay > 98)){ // attempt to set to room light, code not working
ambient = analogRead(lightLevel);
} */
}
void outRange(){
digitalWrite(ledPin0, HIGH); // default ledPins to high for common anode LED
digitalWrite(ledPin1, HIGH);
if ((lightLevel <= 32 + 3) && (lightLevel >= 32 - 7)){ // sets light range parameter for proper light level
digitalWrite(ledPin0, HIGH); // output green only
digitalWrite(ledPin1, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Light Input Status:"); // lcd output normal
lcd.setCursor(2,1);
lcd.print("Normal");
delay(1000);
}
else if (((lightLevel > 32 + 3) && (lightLevel <= 32 + 7)) || ((lightLevel < 32 - 7) && (lightLevel >= 32 - 27))){ // sets light range parameter for warning light level
digitalWrite(ledPin0, LOW); // ouput red and green to result in yellow
digitalWrite(ledPin1, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Light Input Status:");
lcd.setCursor(2,1);
lcd.print("Warning:"); // lcd output warning, within safe range but approaching danger level
if (lightLevel > 32 + 3){
lcd.setCursor(2,2);
lcd.print("Light above");
lcd.setCursor(2,3);
lcd.print("optimal level");
}
else if (lightLevel < 32 - 7){
lcd.setCursor(2,2);
lcd.print("Light below");
lcd.setCursor(2,3);
lcd.print("optimal level");
}
delay(1000);
}
else if ((lightLevel > 32 + 7) || (lightLevel < 32 - 27)){ // set light range for danger level
digitalWrite(ledPin0, LOW); // red light only active for danger status
digitalWrite(ledPin1, HIGH);
delay(200); // `` delay adjust with below for strobe rate
digitalWrite(ledPin0, HIGH); // lights off for blinking adjusted delay above and below for strobe rate annotated ``
digitalWrite(ledPin1, HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Light Input Status:"); // output danger status to LCD
lcd.setCursor(2,1);
lcd.print("Danger:");
if (lightLevel > 32 + 7){
lcd.setCursor(2,2);
lcd.print("Light above");
lcd.setCursor(2,3);
lcd.print("danger level");
}
else if (lightLevel < 32 - 27){
lcd.setCursor(2,2);
lcd.print("Light below");
lcd.setCursor(2,3);
lcd.print("danger level");
}
delay(25); // `` delay adjust with above for strobe rate
}
}