From 6d04e0e0bdce626844b97d5d1e121124f517ac84 Mon Sep 17 00:00:00 2001 From: Peter Feerick Date: Tue, 17 Jan 2017 10:06:15 +1000 Subject: [PATCH] Prevent tone with frequency of '0' from crashing. When tone is called specifying a frequency of 0, the code will crash due to a divide by zero issue. --- cores/oak/Tone.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cores/oak/Tone.cpp b/cores/oak/Tone.cpp index dfba490..40d6264 100644 --- a/cores/oak/Tone.cpp +++ b/cores/oak/Tone.cpp @@ -66,6 +66,13 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) { // Set the pinMode as OUTPUT pinMode(_pin, OUTPUT); + // Alternate handling of zero freqency to avoid divide by zero errors + if (frequency == 0) + { + noTone(_pin); + return; + } + // Calculate the toggle count if (duration > 0) { toggle_counts[_index] = 2 * frequency * duration / 1000;