Skip to content

Commit bd82352

Browse files
authored
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.
1 parent 321fca0 commit bd82352

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Diff for: cores/arduino/Tone.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
349349
// Calculate the toggle count
350350
if (duration > 0)
351351
{
352-
toggle_count = 2 * frequency * duration / 1000;
352+
toggle_count = 2 * (unsigned long)frequency * duration / 1000;
353353
}
354354
else
355355
{

0 commit comments

Comments
 (0)