From b4f6d8e31444f8c234567864b5957d2469062829 Mon Sep 17 00:00:00 2001 From: Brian Schimke Date: Sun, 20 Sep 2020 19:43:03 -0400 Subject: [PATCH] Prevent Stuck Loop Micros overflows every ~71.5 minutes. This can cause the newTime value to get stuck at a very high value while Micros rolls back to 0. When this happens the while condition in the sample loop is not met for a very long time. Changing the while condition to check for the difference between the two unsigned longs prevents this possibility (a negative outcome is not possible as there is no sign). --- ESP32_FFT_VU/ESP32_FFT_VU.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ESP32_FFT_VU/ESP32_FFT_VU.ino b/ESP32_FFT_VU/ESP32_FFT_VU.ino index cfc3db5..58ec6e7 100644 --- a/ESP32_FFT_VU/ESP32_FFT_VU.ino +++ b/ESP32_FFT_VU/ESP32_FFT_VU.ino @@ -116,7 +116,7 @@ void loop() { newTime = micros(); vReal[i] = analogRead(AUDIO_IN_PIN); // A conversion takes about 9.7uS on an ESP32 vImag[i] = 0; - while (micros() < (newTime + sampling_period_us)) { /* chill */ } + while ((micros() - newTime) < sampling_period_us) { /* chill */ } } // Compute FFT