There is indeed a race condition in your code: if an interrupt fires
while you are reading one of the counters, you might read garbage. If it
fires between reading and resetting the counter, you loose one count.
The way to prevent the race is to block interrupts while the main thread
(everything that does not run in interrupt context) accesses the
By best attempt to provide something of an answer.
You have said:
PT_WAIT_UNTIL(pt, millis() - lastTimeBlink > 1000);
That is not literally represented in your code, so I take your question to refer
int dist2 = dist1.toInt();
PT_WAIT_UNTIL(pt, millis() - lastTimeBlink > (dist2 * 1710));
or a similar section.
When you execute this ...
The problem is that you check inside the two functions Sparkle and GreenYellow for another serial byte. Then when it's unequal, you move back to the while loop where you wait for the next serial byte, but I think you need to use the same one to decide to go to the correct function instead of waiting for a next serial byte.
Probably the easiest is to not read ...
As mentioned before, only an elevated process with PID of 0/1 can initiate a process switch. The new process
may have any PID from 0 ~ 7 after the process switch. The key for successful proactive process switching is that
Espressif Systems 620
Submit Documentation Feedback
ESP32 Technical Reference Manual V4.3