Bird
0
0
Arduinoprogramming~10 mins

Playing melodies with tone() in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Playing melodies with tone()
Start
Define notes and durations
Loop through each note
Call tone() to play note
Delay for note duration
Stop tone()
Next note or End
Repeat or Stop
The program defines notes and durations, then plays each note using tone(), waits, stops the tone, and moves to the next note.
Execution Sample
Arduino
int melody[] = {262, 294, 330};
int duration[] = {500, 500, 500};
for (int i=0; i<3; i++) {
  tone(8, melody[i]);
  delay(duration[i]);
  noTone(8);
}
This code plays three notes (C4, D4, E4) on pin 8, each for 500 milliseconds.
Execution Table
Stepimelody[i]ActionOutput/Effect
10262tone(8, 262)Pin 8 plays note C4 (262 Hz)
20262delay(500)Wait 500 ms while note plays
30262noTone(8)Stop tone on pin 8
41294tone(8, 294)Pin 8 plays note D4 (294 Hz)
51294delay(500)Wait 500 ms while note plays
61294noTone(8)Stop tone on pin 8
72330tone(8, 330)Pin 8 plays note E4 (330 Hz)
82330delay(500)Wait 500 ms while note plays
92330noTone(8)Stop tone on pin 8
103N/ALoop endsAll notes played, melody finished
💡 i reaches 3, loop condition i<3 is false, melody playback ends
Variable Tracker
VariableStartAfter 1After 2After 3Final
iundefined0123
melody[i]N/A262294330N/A
Key Moments - 3 Insights
Why do we call noTone() after delay()?
Because tone() starts playing the note and keeps playing until stopped. delay() waits while the note plays, then noTone() stops the sound. See execution_table rows 2 and 3.
What happens if we forget noTone()?
The last note will keep playing continuously because tone() never stops. The program won't move on properly. This is why noTone() is important after each note.
Why do we use a loop to play the melody?
The loop lets us play each note in order without repeating code. It goes through melody[] and duration[] arrays step by step, as shown in execution_table steps 1 to 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the frequency played at step 4?
A262 Hz
B330 Hz
C294 Hz
DNo frequency played
💡 Hint
Check the 'melody[i]' value at step 4 in the execution_table.
At which step does the program stop playing the last note?
AStep 9
BStep 8
CStep 10
DStep 7
💡 Hint
Look for noTone(8) call for the last note in execution_table.
If we increase delay duration to 1000 ms, how does execution_table change?
ATone frequencies change
BDelay steps show 1000 ms instead of 500 ms
CLoop runs fewer times
DnoTone() calls are removed
💡 Hint
Check the 'Action' and 'Output/Effect' columns for delay() steps in execution_table.
Concept Snapshot
Use tone(pin, frequency) to play a note on a pin.
Use delay(duration) to wait while note plays.
Use noTone(pin) to stop the note.
Loop through arrays of notes and durations to play melodies.
Each note plays until noTone() stops it.
This creates simple melodies on Arduino.
Full Transcript
This example shows how to play a melody on Arduino using tone(). We define arrays for notes and durations. A loop goes through each note, calls tone() to start playing, waits with delay(), then calls noTone() to stop. This repeats for all notes. The execution table shows each step: playing a note, waiting, stopping, then moving to the next. Variables i and melody[i] change as the loop runs. Key points: noTone() stops the sound, delay() controls note length, and looping plays all notes in order. The quiz checks understanding of frequencies played, when notes stop, and effects of changing delay.