Help enabling/disabling timeout timers

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
    }

Well, I wrote that guide and I can’t make any sense of your sketch.

I’ve never declared a timeout timer in the void setup before, I only do that for interval timers, or when a sacrificial timer is being created.

Also, all of your stuff in void setup almost certainly doesn’t work the way you think it does, and even if it did, the slider wouldn’t change the timeout period because void setup only runs once at startup.

You’d be far better using a lambda timeout timer in your BLYNK_WRITE(V18) callback function as I’ve described in my guide.

Pete.

I’ve never declared a timeout timer in the void setup before, I only do that for interval timers, or when a sacrificial timer is being created.

I think what confused me is the guide does show a timeout timer in void setup and didn’t realise this only applies to a sacrificial timer:

int control_heater_timer_ID;

void setup()
{
  timer.setTimeout(3600000L, [] () {} ); // dummy/sacrificial Function
  timer.setInterval(5000L, take_sensor_reading);
  control_heater_timer_ID = timer.setInterval(30000L, control heater);
}

I thought once declared here I could enable and disable it in other parts of my code, but I guess that’s not the case.
I have used what I think is the lambda timeout timer in BLYNK_WRITE(V18). This seems to work, so I can now set the timer duration with the sider BLYNK_WRITE(V19), the timer is started when pressing BLYNK_WRITE(V18) and unchecking the button disables the timer.

There’s probably a tidier and more efficient way of doing this, but finally it’s a working solution I’ve been looking for.

The updated working code is:

#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)

int pinValueV18;

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
}

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
  pinValueV18 = param.asInt();
  if (pinValueV18 == 1)
  {
    Serial.println("Enable timer");

    SV1Timer = timer.setTimeout(OffTimerTime, []()
    {
      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 offOffCommand);
    });
  }
  if (pinValueV18 == 0) {
    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);
}

Thanks for pointing me in the right direction.
Will

1 Like