hi im working on a project titled smart street light. using a led , arduino, relay and a light detecting sensor.
the coding is below
int sensor = A7;
int sensorValue = 0;
void setup() {
pinMode(2,OUTPUT);
Serial.begin(9600);
}
void loop(){
sensorValue = analogRead(sensor);
Serial.print(sensorValue);
Serial.print("\n");
if (sensorValue < 500)
digitalWrite(2,HIGH);
delay(5000);
digitalWrite(2, LOW);
if (sensorValue > 501)
digitalWrite(2, LOW);
delay(100);
}
in which when a light shine upon light detecting sensor, the led will immediately light up( imitating street light on) and will goes off after 5 sec when the sensor did not detect any more light.
the problem im encountering is when the sequence start again, sensor detect light, the led takes 5 sec before lighting up.
i believe my void loop is wrong but cant seem to find the mistake. hope any one can helps. thanks in advanced.
the problem im encountering is when the sequence start again, sensor detect light, the led takes 5 sec before lighting up.
The problem you are encountering is that your indenting is poor. Use Tools + Auto Format, to properly indent your code, and you'll immediately see what the problem is.
Hint: When the if statement is supposed to do multiple things, you MUST use curly braces. Even when it only does one thing, curly braces are a good idea.
PaulS:
The problem you are encountering is that your indenting is poor. Use Tools + Auto Format, to properly indent your code, and you'll immediately see what the problem is.
Hint: When the if statement is supposed to do multiple things, you MUST use curly braces. Even when it only does one thing, curly braces are a good idea.
alto777:
Don’t you want the street light to be off when it’s dark out?
Humor aside, I would expect the streetlight to be ON when it's dark out, and
I would expect the streetlight to be OFF "when a light shine upon light detecting sensor."