TaskScheduler help

I'm sure I'm missing something obvious here, but I've been going up and down this code, and comparing it to examples online that definitely work, and I'm drawing a blank.

I can get to the "countdown" function, but I can't get to the "countdownCallback" function. It should get there after 1 second, right? What am I missing?

#include <avr/io.h>
#include <avr/interrupt.h>
#include <TaskScheduler.h>

#define RESET_PIN 9           // reset button pin

void countdownCallback();

// set up task scheduler for countdown 
Scheduler ts;
Task t1(1000, 50, &countdownCallback, &ts); // create task of runtime timerRuntimeS (starts at 50s)

void setup() {
  Serial.begin(115200);
  ts.init();
  ts.addTask(t1);
  pinMode(RESET_PIN, INPUT_PULLUP);
  Serial.println("Setup");
}

void countdown(int runtime) {
  Serial.println("Countdown");
  t1.setIterations(runtime); 
  t1.enable();
}

void countdownCallback() {
  Serial.println("Callback");
  if (t1.isFirstIteration()) {
    Serial.println("Timer started");
  }
  
  if (t1.isLastIteration()) {
    Serial.println("Timer finished");
    t1.disable();
  }
}

long debounce = 300;
long myTime = millis();
void loop() { 
  ts.execute(); 
 // Reset button debounce and action
  if (digitalRead(RESET_PIN) == LOW && (millis() - myTime) > debounce) {
    countdown(50);                     // Call the countdown function, start countdown timer
    Serial.println("Reset");
    myTime = millis();
  } 
}


Your topic has been moved. Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Did the examples you followed use Serial.print() in the callback function?
I'm not sure what the current state of the art is but it is common practice to set a (volatile) flag in the callback function and do the printing in the loop().

Alright, I removed the Serial.print from the callback functions, but it's still not getting to the callback function. I'm not well-versed in C, so I'm sure there's another mistake I'm making without realizing.

#include <avr/io.h>
#include <avr/interrupt.h>
#include <TaskScheduler.h>

#define RESET_PIN 9           // reset button pin

bool countdownStarted = false;

void countdownCallback();

// set up task scheduler for countdown 
Scheduler ts;
Task countdownTask(1000, 50, &countdownCallback, &ts, true); // create task of runtime timerRuntimeS (starts at 50s)

void setup() {
  Serial.begin(115200);
  ts.init();
  ts.addTask(countdownTask);
  pinMode(RESET_PIN, INPUT_PULLUP);
  Serial.println("Setup");
}

void countdown(int runtime) {
  Serial.println("Countdown");
  countdownTask.setIterations(runtime); 
  countdownTask.enable();
}

void countdownCallback() {
  if (countdownTask.isFirstIteration()) {
    countdownStarted = true;
  }
  
  if (countdownTask.isLastIteration()) {
    countdownStarted = false;
    countdownTask.disable();
  }
}

long debounce = 300;
long period = 1000;
long myTime = millis();
long myTime2 = millis();
void loop() { 
  ts.execute(); 
 // Reset button debounce and action
  if (digitalRead(RESET_PIN) == LOW && (millis() - myTime) > debounce) {
    Serial.println("Reset");
    countdown(5);                     // Call the countdown function, start countdown timer
    myTime = millis();
  } 

  if ((millis() - myTime2) > period) {
    if (countdownStarted) {
      Serial.println("Timer active");
    }
    else {
      Serial.println("Timer not active");
    }
    myTime2 = millis();
  } 
}

#include <TaskScheduler.h>

void task1Callback();
void task2Callback();

Scheduler ts;

Task t1(1000, TASK_FOREVER, &task1Callback);
Task t2(500, TASK_FOREVER, &task2Callback);

void setup() {
  Serial.begin(115200);
  ts.init();
  ts.addTask(t1);
  ts.addTask(t2);
  t1.enable();
  t2.enable();
}

void loop() {
  ts.execute(); // Run scheduled tasks
}

void task1Callback() {
  Serial.println("Task 1 executed");
}

void task2Callback() {
  Serial.println("Task 2 executed");
}

Here's the example that works.

I do not know anything about this stuff !! Maybe you should enable the task. Look at the difference in the two setup() functions; the working one has lines like t1.enable().

Okay, for whatever reason, by setting the initial iterations value of the task to "TASK_FOREVER" instead of a set number, I got it to work. I was pulling my hair out yesterday, and I come in and figure it out first thing today. Go figure.

#include <TaskScheduler.h>

#define RESET_PIN 9           // reset button pin

void countdownCallback();

// set up task scheduler for countdown 
Scheduler ts;
Task countdownTask(1000, TASK_FOREVER, &countdownCallback); // create task of runtime timerRuntimeS (starts at 50s)

void setup() {
  Serial.begin(115200);
  ts.init();
  ts.addTask(countdownTask);
  pinMode(RESET_PIN, INPUT_PULLUP);
  Serial.println("Setup");
}

void countdown(int runtime) {
  Serial.println("Countdown");
  countdownTask.setIterations(runtime); 
  countdownTask.enable();
}

void countdownCallback() {
  Serial.println("Callback");
  if (countdownTask.isFirstIteration()) {
    Serial.println("Timer Started");
  }
  
  if (countdownTask.isLastIteration()) {
    countdownTask.disable();
    Serial.println("Timer Ended");
  }
}

int debounce = 300;
long myTime = millis();
void loop() { 
  ts.execute(); 
 // Reset button debounce and action
  if (digitalRead(RESET_PIN) == LOW && (millis() - myTime) > debounce) {
    Serial.println("Reset");
    countdown(5);                     // Call the countdown function, start countdown timer
    myTime = millis();
  } 
}

I am enabling it in the countdown function. I don't want to start the timer until the button is pressed. The countdown function is largely unnecessary here, but it was a part of a larger set of code that I wanted to partially include in my dummy program. Thanks for your help!

As I said, I do not know anything about this stuff :wink: So I learned something today.

Not at all unusual.

I've told ppl even simple things take two days, but what I should have said is a sleep cycle is necessary.

What does the expression "sleep on it" mean?

chatGPT said:

The expression "sleep on it" means to take some time to think about a decision or problem, usually overnight. The idea is that by giving yourself some time and a fresh perspective after a good night's sleep, you might come to a clearer or better conclusion. It's often used when someone is unsure about a choice and wants to avoid making a hasty decision.

Why do solutions to vexing problems occur during mundane activities like brushing one's teeth or showering?

chatGPT said:

Solutions to vexing problems often come during mundane activities because these tasks allow your mind to relax and wander. When you're engaged in a routine activity, your conscious mind is less focused, which can enable your subconscious to work on problems in the background. This mental downtime can foster creativity and make connections that might not occur during more focused thinking. Additionally, being in a relaxed state can reduce stress, allowing for clearer thinking and new insights. It's a great example of how sometimes stepping back can lead to breakthroughs!

a7

Yeah, a lot of the time, I'll sleep on something and as soon as I look at it, it'll come to me what's wrong right away. Today, the revelation was more in how to approach the troubleshooting. I just took the working code and incrementally added my own until it wasn't working anymore. Sometimes the obvious thing you're missing is the ability to meticulously troubleshoot lol.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.