arduino / arduino/ArduinoCore-mbed

Calling tone in loop freezes the Arduino

Open
#368 0 comments 0 reactions 0 assignees View on GitHub
nano33ble nrf52
Dominant language
C
Stars
411
Forks
225
PR merge metrics
No merged PRs in 30d

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:

```c++
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:

https://github.com/arduino/ArduinoCore-mbed/blob/64bf2aa92bf7fe26ab08aae5f4340f2c2920ebaf/cores/arduino/Tone.cpp#L50-L57

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 (https://github.com/jgromes/RadioLib/discussions/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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running the provided Arduino Nano 33 BLE sketch and observing pin 6 and the serial output. Read cores/arduino/Tone.cpp around the linked allocation code, then trace the repeated tone calls and timing behavior. Done means the sketch completes its 30-second loop, prints "no beep :(")", and pin 6 continues producing the requested output without freezing.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.