Coding for light sensor project
Posted: Sat Apr 02, 2016 11:22 am
Hi,
I have a small problem with a "while" statement - how do I end it?
The idea is that the light (an LED for now) turns on when it goes dark, it stays on for 5 seconds and I want it to stay off until it resets once it gets light again.
When the sketch gets to the while statement it stops measuring/printing in serial monitor (highligthed in red text) I must need to stop the while statement but I don't know how to and can't find the solution (or maybe can but don't understand it).
can anyone offer a solution?
many Thanks.
JF
Sketch:
I have a small problem with a "while" statement - how do I end it?
The idea is that the light (an LED for now) turns on when it goes dark, it stays on for 5 seconds and I want it to stay off until it resets once it gets light again.
When the sketch gets to the while statement it stops measuring/printing in serial monitor (highligthed in red text) I must need to stop the while statement but I don't know how to and can't find the solution (or maybe can but don't understand it).
can anyone offer a solution?
many Thanks.
JF
Sketch:
Code: Select all
// Define analog pin
int sensorPin = 0;
int lightPin = 2;
// Variables
int lightState = 0;
int lowThreshold = 10;
int highTreshold = 40;
// Setup
void setup() {
// Init serial
Serial.begin(9600);
pinMode(lightPin,OUTPUT);
}
// Main loop
void loop() {
// Get temperature
int sensorValue = analogRead(sensorPin);
// Put temperature on the serial port
Serial.println(sensorValue,DEC);
if (sensorValue < lowThreshold){
digitalWrite(lightPin, HIGH);
delay (5000);
digitalWrite(lightPin, LOW);
[color=#FF0000] while (sensorValue < lowThreshold);[/color]
// need to put in code so that until light is restored it won't go back to low state and light stays off
if (sensorValue > highTreshold)
digitalWrite(lightPin, LOW);
}
// Wait for 1 sec
delay(1000);
}