Timer not working as expected?

hello, when i make a sketch, i found on some code surround, the timer not working as expected: only execute one time, it should not happen but it does. i checked for many time but can't figure out why this happen. thank you for any suggestion.

here are my codes, i deleted the code does not relation. copy the all codes below, serial monitor, the problem will reproduce.

#include "arduino-timer.h"

auto timer = timer_create_default();
boolean verify_passed = false;
boolean led_receiving_data = false;

void setup() {
  Serial.begin(115200);
  timer.every(2000, toggle_led);
}

void loop() {
  timer.tick();
  if (verify_passed == false) {
  } else {
    led_receiving_data = true;
  }
}

void toggle_led() {
  Serial.print("a");    //<----------- only execute one time

  if (led_receiving_data == true) {
    Serial.print("b");
  }
}

If I remember correctly the callback needs to return a boolean and if it's true then the timer will repeat

try with

bool toggle_led() {
  Serial.print("a"); 
  return true;
}

@weigangjiang, your topic was moved to a more suitable location on the forum.

There's no led_receiving_data = false;

i use your suggestion and problem fixed, thank you very much.

by the way, below code is work and without return true;

#include "arduino-timer.h"

auto timer = timer_create_default();

void setup() {
  Serial.begin(115200);
  timer.every(2000, toggle_led);
}

void loop() {
  timer.tick();
}

void toggle_led() {
  Serial.print("a");
}

anyway, thank you again.

You might be lucky :wink:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.