Skip to content

Commit 46f6cad

Browse files
authored
Fix crash when calling tone() with frequency 0 (#520)
Avoid division by 0 when a frequency of 0 is passed to the tone() function. If a 0 is passed, noTone() is called instead.
1 parent 4d12c30 commit 46f6cad

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

cores/arduino/Tone.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ void toneAccurateClock (uint32_t accurateSystemCoreClockFrequency)
5757

5858
void tone (uint32_t outputPin, uint32_t frequency, uint32_t duration)
5959
{
60+
61+
// Avoid divide by zero error by calling 'noTone' instead
62+
if (frequency == 0)
63+
{
64+
noTone(outputPin);
65+
return;
66+
}
67+
6068
// Configure interrupt request
6169
NVIC_DisableIRQ(TONE_TC_IRQn);
6270
NVIC_ClearPendingIRQ(TONE_TC_IRQn);

0 commit comments

Comments
 (0)