Platform: Arduino UNO WiFi Rev 2
Arduino 1.8.8
I need a timer to make sure a garage door goes completely open or closed within a specified travel time. There are two limit switches, one activated when the door is closed and one activated when the door is open. Both are Normally Open. When the door starts going up, the bottom limit switch will go open (upper switch is already open). Both are open during door travel. When the door is completely open, the bottom limit switch will be open and the upper switch will be closed.
Timer Requirements: I want to set up a timer such that, if the door doesnāt go completely open or closed within X seconds after starting, I will get an alert. If the door goes completely open or closed in less than X seconds, the timer expires and doesnāt trigger anything. The timer should be reused on the next door opening or closing. Or the timer could be destroyed and a new timer created for the next door opening or closing.
I donāt need entire code (Iāve got a Garage Door State Machine running) - just need help with the BlynkTimer functions to be able to start, expire/stop, and restart a timer.
Function setTimeout(unsigned long d, timer_callback f) looked promising, but āafter the callback f has been called, the interval is deleted, therefore the value timerId is no longer valid.ā (per the Arduino SimpleTimer Library Docs.
I tested setTimeout below and Timer2 is no longer enabled after f has been called.
Do you have any ideas how to code this using BlynkTimer? Should I delete a timer and create a new one?
Thanks for any help on this!
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
int timer2;
BlynkTimer timer;
void OnceOnlyTask() {
Serial.println("This timer only triggers once");
// Debug print lines:
Serial.print("Timer number: ");
Serial.println(timer2);
Serial.print("Timer is enabled: ");
Serial.println(timer.isEnabled(timer2));
Serial.println("Restarting timer");
timer.restartTimer(timer2);
}
void setup() {
Serial.begin(9600);
Serial.println("SimpleTimer Example");
Serial.println("Timer2 is set to trigger only once after 1 second");
Serial.println();
timer2 = timer.setTimeout(1000, OnceOnlyTask);
}
void loop() {
timer.run();
// Is timer2 enabled after f executes?
if ((millis() % 3000) == 0){
Serial.print("Timer is enabled: ");
Serial.println(timer.isEnabled(timer2));
}
}