SimpleTimer doesn't work without timer.run() in loop()

I’m using the SimpleTimer like so: timer.setTimer(1000, toggleLed, 3); but nothing happens, I tried to add a print but it doesn’t print it.

“toggleLed” has a signature of void foo(); and I included #include <SimpleTimer.h>.

@Yamashiro if you are using Blynk library 0.4.7 or later, which you really should be, then BlynkTimer is already in the library i.e. you don’t need

#include <SimpleTimer.h>

Where in the Arduino guide https://playground.arduino.cc/Code/SimpleTimer did you see the syntax you are using?

If you look closely you will see the timer has to be configured as an integer so it can automatically be deleted after it has run x times.

@Yamashiro actually it doesn’t need to be defined as an integer which means you have a bug elsewhere in your code. Paste a formatted sketch if you need further assitance.

I have Blynk 0.4.8.

I just replaced the SimpleTimer with BlynkTimer timer; and both timer.setTimeout(1000, turnControlLedOff); and timer.setTimer(100, ledToggle, 3); don’t work.

It’s from:

What can cause it not to work?

The function works for me as per the Arduino docs.

What do you have in ledToggle function?

Here is a minimal example where the timeout doesn’t work:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266_SSL.h>
BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
// control led
BLYNK_WRITE(V0)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  if(pinValue == 1){
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println("got value");
    timer.setTimeout(1000, foo);
  }
}

void foo(){
  Serial.println("foo");
}

void setup()
{

  pinMode(BUILTIN_LED, OUTPUT);  // set onboard LED as output
  digitalWrite(BUILTIN_LED, HIGH);
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
}

void loop()
{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

I only see “got value” and not “foo” in the serial monitor.

@Yamashiro check the new title for your fix.

4 Likes

Capture

4 Likes