// Ahora pruebo con timer.in
// No sirve, parece que no sepuede setear el timer.in desde adentro de la función llamada.
// Pero sí se puede setear llamándolo desde otra función.
// Prueba usando varios timers así, cuando quiero liquidar una task, simplemente cancelo todas las tasks de ese timer
// (que va a ser solo una, pero no conozco el handle).
// Cuando el timelapse de MyTimer es mayor que el de MyTimer2, MyTimer2 ya no llega a terminar nunca.
#include <arduino-timer.h>
// https://deepbluembedded.com/arduino-timer-library/#google_vignette
// https://github.com/contrem/arduino-timer
#define LED_TENSION_PIN 13
#define LED_BLUETOOTH_PIN 11
#define LED_LOW_BATTERY_PIN 12
long timeGap2 = 200;
Timer<2, millis> MyTimer; // 2 concurrent tasks, using millis as resolution.
Timer<1, millis> MyTimer2; // 1 concurrent task, using millis as resolution.
bool killTask2 = false;
void setup() {
// put your setup code here, to run once:
MyTimer.every(500, Task_Led_tension_Handler);
MyTimer2.in(timeGap2, Task_Led_tension_Handler2);
pinMode(LED_TENSION_PIN, OUTPUT); // set LED pin to OUTPUT
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
MyTimer.tick();
MyTimer2.tick();
}
void interesting_function()
{
timeGap2 = 2*timeGap2;
Serial.print("timeGap2: ");
Serial.println(timeGap2);
reset_Timer2();
}
void reset_Timer2()
{
// MyTimer2.cancel(); // Cancel all tasks in MyTimer2. Without this one, the timer will never change even if I try to reset it with the next line.
// I could also kill the task (not the timer) by returning false in the Task funcion.
// Hice algo mal, se queda sin funcionar el Timer2
// killTask2 = true;
// MyTimer2.every(timeGap2, Task_Led_tension_Handler2);
}
bool Task_Led_tension_Handler(void *)
{
digitalWrite(LED_TENSION_PIN, !digitalRead(LED_TENSION_PIN)); // Toggle LED1
Serial.println("First Task.");
interesting_function();
MyTimer2.in(timeGap2, Task_Led_tension_Handler2); // Acá sí funciona.
return true; // true: repeat the action, false: to stop the task.
}
bool Task_Led_tension_Handler2(void *)
{
digitalWrite(LED_TENSION_PIN, !digitalRead(LED_TENSION_PIN)); // Toggle LED1
Serial.println("Second Task.");
MyTimer2.cancel();
MyTimer2.in(timeGap2, Task_Led_tension_Handler2); // Acá NO funciona.
return true; // true: repeat the action, false: to stop the task.
}