From bd823521353116c45c454a31467c32591cc738d2 Mon Sep 17 00:00:00 2001 From: Dimitrios Kouzis-Loukas Date: Mon, 2 Sep 2024 21:54:02 -0400 Subject: [PATCH] Quick Fix of overflow for larger toggle_count values Quick Fix of overflow for larger `toggle_count` values. Because of the 16-bit `int` value in many platforms including Uno, the calculation of `toggle_count` overflows easily. For example with a frequency of 39kHz and a duration of 9ms, you get `toggle_count == 112 `, which is 1.5ms. --- cores/arduino/Tone.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp index 1bfb3e379..2ad0fc4b9 100644 --- a/cores/arduino/Tone.cpp +++ b/cores/arduino/Tone.cpp @@ -349,7 +349,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) // Calculate the toggle count if (duration > 0) { - toggle_count = 2 * frequency * duration / 1000; + toggle_count = 2 * (unsigned long)frequency * duration / 1000; } else {