Skip to content

Commit

Permalink
Quick Fix of overflow for larger toggle_count values
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lookfwd committed Sep 3, 2024
1 parent 321fca0 commit bd82352
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion cores/arduino/Tone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit bd82352

Please sign in to comment.