I've got some code that checks for messages, and marks them as read if there is light on a light sensor.
My loop looks like this:
void spinServo() {
if(!servoRunning) {
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
myservo.write(0);
}
}
void loop() {
// If no new message, check for new message
if(!newMessage) {
checkForNewMessage();
lightValue = analogRead(0);
Serial.println(lightValue);
}
//This will keep running if a message is unread and the lid is closed
while(newMessage) {
spinServo();
lightValue = analogRead(0);
Serial.println("New Message");
//If the lid is opened, the message is read
if(lightValue > lightValueThreshold) {
Serial.printf("Analog read value (LDR) %d above threshold of %d -> consider message read.\n", lightValue, lightValueThreshold);
markAsRead();
servoRunning = false;
}
}
// Wait X seconds
delay(fetchIntervalMillis);
}
What I'm finding though is that the loop always waits for the spinServo function. Is there a way I can continuously check for the light values, without it waiting for the servo?
What im finding is that if I change my servo function to last longer, and the light sensor has light on it, it wont detect the light immediately so the markAsRead function gets missed if the light on the sensor is removed.