I've got a scenario where I want to rotate a servo whilst checking to see if a light value has been met.
At the moment, I'm doing this in a while look, but the Servo doesn't finish executing before it loops again
My checkForNewMessage function needs to wait for the delay as its calling an endpoint and I dont want to spam it
void spinServo() {
servoRunning = true;
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
myservo.write(0);
servoRunning = false;
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
wifiConnect();
}
// If no new message, check for new message
if(!newMessage) {
checkForNewMessage();
}
//This will keep running if a message is unread and the lid is closed
while(newMessage) {
if(!servoRunning) {
spinServo();
}
lightValue = analogRead(0);
Serial.println("New Message");
Serial.println(lightValue);
//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);
}