arkhipenko / TaskScheduler

Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers
http://playground.arduino.cc/Code/TaskScheduler
BSD 3-Clause "New" or "Revised" License
1.33k stars 243 forks source link

Pause and resume the clock #127

Closed yangjianlin-c closed 3 years ago

yangjianlin-c commented 3 years ago

Hi Sir, I'd like to design a pomodoro clock with this library. The main function is as below:

I have build a very simple code to run the clock. But I'm not sure how to response or monitor the button status to control the schedule. Could you provide any suggestion please? Thanks.

And the online link of this code: https://wokwi.com/arduino/projects/307877482987520578

#define _TASK_SLEEP_ON_IDLE_RUN
#include <TaskScheduler.h>

//Pause Button
int pushButton = 4;
int LED=9;

Scheduler ts;
void t1Callback();
bool workStart();
void workEnd();
bool restStart();
void restEnd();

Task t1(0, TASK_ONCE, t1Callback, &ts, true);
Task work(5 * TASK_SECOND, TASK_ONCE, NULL, &ts, false, &workStart, &workEnd); //adding task to the chain on creation
Task rest(2 * TASK_SECOND, TASK_ONCE, NULL, &ts, false, &restStart, &restEnd);      //adding task to the chain on creation

void t1Callback()
{
    work.restartDelayed();
}
bool workStart(){    
    Serial.println("Work satus.");
    digitalWrite(LED, HIGH);
    return true;
}
void workEnd()
{
    rest.restartDelayed();
}
bool restStart(){    
    Serial.println("rest satus.");
    digitalWrite(LED, LOW);
    return true;
}
void restEnd()
{
    work.restartDelayed();
}

void setup(void)
{
    Serial.begin(115200);
    pinMode(pushButton, INPUT);
}

void loop(void)
{
    ts.execute();
}
arkhipenko commented 3 years ago

Use OneButton library to query the button.

https://www.arduino.cc/reference/en/libraries/onebutton/

You can use a Task to "tick" buttons periodically, and put activation in the callback for a click or DoubleClick event.

yangjianlin-c commented 3 years ago

OK. thanks for your suggestion. Then how to stop running task? with disableAll() or something else? Seems I can't stop the running task from another task with disableAll function.

yangjianlin-c commented 3 years ago

Seems I find the reason. I first abort() the running task and then disableAll(). As in the workEnd() and restEnd() I request it start a new task. So not able stop the loop, right?

arkhipenko commented 3 years ago

You disable a running task by calling Task.disable(). Abort() is a special case and should be only used as such. You can also just let the task run it's course and stop by itself

arkhipenko commented 3 years ago

Please refer to TaskScheduler wiki and examples for help. Unfortunately I cannot provide educational support. Only issue resolution.