contrem / arduino-timer

Non-blocking library for delaying function calls
BSD 3-Clause "New" or "Revised" License
339 stars 50 forks source link

how do i close a timer? #9

Open allegfede opened 4 years ago

allegfede commented 4 years ago

i made a timer with: auto delay_timer = timer_create_default();

and cannot terminate it using: delay_timer.cancel();

timer is started with: delay_timer.every(1000, bomb_armed);

seems he need the "task", but cannot find a way to define this task ...

contrem commented 4 years ago

The in / at / every functions return the Task. From the README:

To cancel a Task

auto task = timer.in(delay, function_to_call);
timer.cancel(task);

so:

auto task = delay_timer.every(1000, bomb_armed);
delay_timer.cancel(task);

There's also a cancel example in examples/full/full.ino

allegfede commented 4 years ago

arduino ide replies that

task is not defined in this scope ... ore something like that.

I bypassed the problem defining Timer<1, millis> delay_timer

and running the timer with: auto task_delay = defuse_delay_timer.every(1000, function_to_call);

results in compiles ok.

On Fri, 13 Dec 2019 at 08:45, Michael Contreras notifications@github.com wrote:

The in / at / every functions return the Task. From the README:

To cancel a Task

auto task = timer.in(delay, function_to_call); timer.cancel(task);

so:

auto task = delay_timer.every(1000, bomb_armed); delay_timer.cancel(task);

There's also a cancel example in examples/full/full.ino https://github.com/contrem/arduino-timer/blob/master/examples/full/full.ino#L66

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/contrem/arduino-timer/issues/9?email_source=notifications&email_token=AAK4JWUPSM7WGMHBKDZIPEDQYM4TPA5CNFSM4J2CQNQKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGZF6WA#issuecomment-565337944, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAK4JWX4WKPU2PKA7GZX4JTQYM4TPANCNFSM4J2CQNQA .

-- Sourceforge: https://sourceforge.net/u/allegfede/

YouTube Channel: https://www.youtube.com/c/v1p3rslab

VIMEO HD videos: http://www.vimeo.com/user1912745/videos

contrem commented 4 years ago

Without seeing your code I am unable to diagnose your potential issue. Does examples/full/full.ino compile with your setup?

Escap3RoomsInc commented 4 years ago

He is right though. There is definitely an issue with cancel. If I call cancel inside a function, I get errors. I literally have to setup the timer again then call the cancel immediately to cancel both instances. This has to be issue or your example is not very clear. Using this in a real world scenario, you're not going to immediately call for a cancel after setting the timer like in your example. Needs to be more clear.

contrem commented 4 years ago

Please provide an example of the errors, and example code that isn't working properly, to help identify the issue you're experiencing.

johnmastri commented 4 years ago

Running into the same issue - is there a way to define the task variables on the root scope without instantiating them? Otherwise, there is no way to cancel them from within another callback function call (unless I'm missing something)

//-- TASK VARIABLES DEFINED OUTSIDE OF SCOPE BUT NOT INSTANTIATED CAUSES AN ERROR WHEN ATTEMPTING TO INSTANTIATE WITHIN A FUNCTION
//error: 'startRoutineEvent' was not declared in this scope
 t.cancel(startRoutineEvent);

auto ledEvent;
auto countdownEvent;
auto startRoutineEvent;

void setup() {

}

void selectMode() {

  t.cancel(ledEvent);
  t.cancel(countdownEvent);
  t.cancel(startRoutineEvent);

//I WOULD LIKE TO INSTANTIATE THEM HERE
 countdownEvent = t.in(3000, showCountdown);
 startRoutineEvent = t.in(6000, startRoutine);

}

void cancelTimers() {

//AND BE ABLE TO CANCEL THEM HERE IF NEED BE
t.cancel(countdownEvent);
t.cancel(startRoutineEvent);

}
contrem commented 4 years ago

is there a way to define the task variables on the root scope without instantiating them?

Yes. In your example, replace:

auto ledEvent;
auto countdownEvent;
auto startRoutineEvent;

With

Timer<>::Task ledEvent;
Timer<>::Task countdownEvent;
Timer<>::Task startRoutineEvent;

You get an error because you can't use auto without an initializer.

Note that the Timer<>::Task constructor must match the Timer template if it wasn't created as the default timer.

Let me know if this helps, and thank you for providing example code and error messages.