-
-
Notifications
You must be signed in to change notification settings - Fork 214
Open
Description
Hi,
I have ran into a bit of an issue that calling tone in a loop will crash/hang/freeze Arduino Nano 33 BLE. Here's a simple sketch to replicate:
void setup() {
Serial.begin(9600);
while(!Serial);
pinMode(6, OUTPUT);
Serial.println("beep :)");
int start = millis();
while(millis() - start < 30000) {
tone(6, 2200);
delayMicroseconds(833);
tone(6, 1200);
delayMicroseconds(833);
} // after a couple of seconds, pin 6 stops outputting anything (checked with a scope)
noTone(6);
Serial.println("no beep :("); // never gets printed
}
void loop() {
}
My initial theory was that this is caused by a failed dynamic allocation here:
ArduinoCore-mbed/cores/arduino/Tone.cpp
Lines 50 to 57 in 64bf2aa
void tone(uint8_t pin, unsigned int frequency, unsigned long duration) { | |
if (active_tone) { | |
delete active_tone; | |
} | |
Tone* t = new Tone(digitalPinToPinName(pin), frequency, duration); | |
t->start(); | |
active_tone = t; | |
}; |
However, adding a check to make sure t != NULL
didn't fix this, so now I'm no longer so sure. I don't know the platform well enough to attempt to fix this.
Background: this popped up in one of my projects (jgromes/RadioLib#407), when generating 1200/2200 Hz tones for AX.25 transmission. I was able to work around the issue by using PwmOut
and switching the frequency of that.