Hey all, I know this has probably been answered in once way or another but I can't seem to find the answer I am after (or i'm incapable of my puny brain comprehending it) - Bare in mind my experience with coding is little to none.
I have a project where it will tweet whether the lights are on or off in a room measured by a photo resistor (analog pin 0). I have it working except that the loop will constantly print the line. I only want the code "lightMonitorOn" and "lightMonitorOff" to run only when the condition is met in my loop.
int ledPin = 9;
int lightSensor = 0;
int threshold = 300;
void loop() {
while (analogRead(lightSensor) > threshold) {
lightMonitorOn();
}
while (analogRead(lightSensor) < threshold) {
lightMonitorOff();
}
}
void lightMonitorOn() {
// update the LED pin
digitalWrite(ledPin, LOW);
// send the string lights! back to the computer followed by a newline Serial.println("lights are On!");
delay (3000);
}
void lightMonitorOff() {
// update the LED pin
digitalWrite(ledPin, HIGH);
// send the string lights! back to the computer followed by a newline Serial.println("lights are off!");
delay (3000);
}
I hope this makes sense and if this has been explained once or 1000 times, feel free to give me crap and point me in the right direction where I can find the answer.
Reading the sensor twice is unnecessary. The value read is either above the threshold, or it isn't. The second if() should be an else.
Ahhh this makes even more sense to me. I prefer to understand it as opposed to punching in the code in a brainless fashion. For this I thank you kind sir.