Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork213
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:
voidsetup() { 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}voidloop() {}
My initial theory was that this is caused by a failed dynamic allocation here:
ArduinoCore-mbed/cores/arduino/Tone.cpp
Lines 50 to 57 in64bf2aa
voidtone(uint8_t pin,unsignedint frequency,unsignedlong duration) { | |
if (active_tone) { | |
delete active_tone; | |
} | |
Tone* t =newTone(digitalPinToPinName(pin), frequency, duration); | |
t->start(); | |
active_tone = t; | |
}; |
However, adding a check to make suret != 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 usingPwmOut
and switching the frequency of that.