Skip to content

Ard Tone Blocking

DodaTech 1 min read

In this tutorial, you'll learn about Arduino tone() Blocks Program Execution. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

The program freezes while tone() is playing.

Quick Fix

Wrong

tone(9, 440, 1000);
delay(1000);  // Wastes CPU time
No other code can run during the 1-second delay.
unsigned long toneEnd = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (millis() >= toneEnd) {
    noTone(9);
  } else {
    tone(9, 440);
  }
  Serial.println("Running...");  // Continuous
  delay(100);
}

void startTone() {
  tone(9, 440);
  toneEnd = millis() + 500;
}
"Running..." prints continuously while tone plays in background.

Prevention

tone() with duration parameter runs in the background (hardware timer). Do not use delay() while a tone is playing — the tone continues in hardware, but your program pauses. Use millis() timing to check when the tone should stop.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### Does tone() block like delay()?

No. tone() starts the waveform and returns immediately (unless you use the blocking pattern with delay). The tone plays in hardware.

Can I check if a tone is still playing?

There is no built-in isTonePlaying() function. Track the end time manually using millis().

What happens if I start tone() while one is playing?

The previous tone is replaced. The new frequency starts immediately on the same timer.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro