Hi, I’m hoping I could get help with enabling and disabling a timeout timer. I’ve tried all sorts of code combinations with no luck (I’m a novice).
I’ve stripped everything back to show the problem code below. In the Blynk App I have a switch (V3) to turn the ESP8266 onboard LED on/off, a switch (V18) to enable/disable the timeout timer and a slider (V19) to set the duration of the timer. I’ve tried following the guidelines in Using BlynkTimer or SimpleTimer but not getting anywhere.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "xxxxx";
char ssid[] = "xxxxx";
char pass[] = "xxxxx";
BlynkTimer timer; //Declare name of timer
int onBoardLED = D4; //Onboard LED
int SV1Timer; //ID of timer
int OffTimerTime = 1000; //Timer defaults to 1000ms on startup
int OffTimerRaw; //Value of slider in Blynk App (seconds)
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(onBoardLED, OUTPUT); //Define onBoard LED as output
Blynk.virtualWrite(V3, 0); // (Inverted in app) switch to control LED - set display as on
Blynk.virtualWrite(V18, 0); //Blynk App switch to stop/start timer set dispay as off
Blynk.virtualWrite(V19, 1); //Blynk App slider for timer duration - set to 1
SV1Timer = timer.setTimeout(OffTimerTime, OffCommand); //Create timer as "SV1Timer"
timer.disable(SV1Timer); //disable timer until activated by app
}
void loop()
{
Blynk.run();
timer.run();
}
BLYNK_WRITE(V3) { //Switch to turn onBoardLED on/off
int pinValue = param.asInt();
if (pinValue == 1) {
digitalWrite(onBoardLED, HIGH); //(Inverted)Turn onBoardLED off
Serial.println("LED Off");
} else {
digitalWrite(onBoardLED, LOW); //(Inverted)Turn onBoardLED on
Serial.println("LED On");
}
}
BLYNK_WRITE(V18) { //Switch button to start/stop timer
int pinValueV18 = param.asInt();
if (pinValueV18 == 1)
{
Serial.println("Enable timer");
timer.restartTimer(SV1Timer);
timer.enable(SV1Timer);
}
else
Serial.println("Disable timer");
timer.disable(SV1Timer);
}
BLYNK_WRITE(V19) { //Slider for timer duration (displayed in seconds)
OffTimerRaw = param.asInt(); //Slider value (seconds)
OffTimerTime = OffTimerRaw * 1000; //Converts slider value to milliseconds
Serial.print("Countdown set to:");
Serial.println(OffTimerTime);
}
void OffCommand() {
Serial.println("OffCommand");
digitalWrite(onBoardLED, HIGH); //(INVERTED) Turn LED off
Blynk.virtualWrite(V18, 0); //Set Blynk App switch to off
Blynk.virtualWrite(V3, 1); // Inverted to show off
}