arduino-timer icon indicating copy to clipboard operation
arduino-timer copied to clipboard

how do i close a timer?

Open allegfede opened this issue 4 years ago • 7 comments

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 ...

allegfede avatar Dec 12 '19 18:12 allegfede

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

contrem avatar Dec 13 '19 07:12 contrem

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 [email protected] 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

allegfede avatar Dec 13 '19 11:12 allegfede

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

contrem avatar Dec 13 '19 19:12 contrem

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.

Escap3RoomsInc avatar Jan 23 '20 22:01 Escap3RoomsInc

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

contrem avatar Jan 24 '20 02:01 contrem

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

}

johnmastri avatar Feb 05 '20 23:02 johnmastri

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.

contrem avatar Feb 06 '20 03:02 contrem