Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how do i close a timer? #9

Open
allegfede opened this issue Dec 12, 2019 · 7 comments
Open

how do i close a timer? #9

allegfede opened this issue Dec 12, 2019 · 7 comments
Labels
FAQ Frequently Asked Question support Help using the library

Comments

@allegfede
Copy link

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
Copy link
Owner

contrem commented Dec 13, 2019

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
Copy link
Author

allegfede commented Dec 13, 2019 via email

@contrem
Copy link
Owner

contrem commented Dec 13, 2019

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

@contrem contrem added the support Help using the library label Dec 13, 2019
@contrem contrem added the FAQ Frequently Asked Question label Dec 27, 2019
@Escap3RoomsInc
Copy link

Escap3RoomsInc commented Jan 23, 2020

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
Copy link
Owner

contrem commented Jan 24, 2020

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

@johnmastri
Copy link

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
Copy link
Owner

contrem commented Feb 6, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FAQ Frequently Asked Question support Help using the library
Projects
None yet
Development

No branches or pull requests

4 participants