Why does my simple sleep test program give a stack overflow error (13 red flashes)? I’m trying to debug my electron not waking from sleeping.
#include "Particle.h"
#define AC_MONITOR_PIN D2
long sleepTime = 2;
Timer c(5000, runTimer);
int i = 0;
void setup() {
Serial.begin(9600);
c.start();
pinMode(AC_MONITOR_PIN, INPUT_PULLDOWN);
Serial.printf("starting, %d\n", millis());
runTimer();
}
void loop() {
}
void runTimer() {
Serial.printf("Tic %d %d\n", i, millis());
i++;
if (i > 2) {
System.sleep(AC_MONITOR_PIN, RISING, sleepTime);
Serial.println("wakeup");
}
}
Software Timers run on their own thread which has only got a limited stack quota.
Just as an ISR needs to be kept small and fast, Software Timer callback should too.
A viable way out of this is to let the timer only set a flag and leave the “heavy lifting” to loop()
which regularly checks the flag.
2 Likes
It doesn’t look like this is a stack quota issue. Instead the sleep process takes about 6 seconds to complete. During that time, the timer function continues to run. So when I put the “sleep” command in the timer function it gets called twice and looks like that may be causing the issue.
I fixed this by moving the sleep function out of the timer and setting a “sleepOnlyOnce” flag.
1 Like