Playing melodies with tone()
📖 Scenario: You have a small buzzer connected to your Arduino. You want to play a simple melody by making the buzzer beep at different notes.
🎯 Goal: Build a program that plays a short melody using the tone() function on a buzzer connected to pin 8.
📋 What You'll Learn
Create an array called
notes with the frequencies of the melody notesCreate an array called
durations with the length of each note in millisecondsUse a
for loop with variable i to play each note using tone()Use
noTone() to stop the buzzer between notesPrint
"Melody finished!" after playing all notes💡 Why This Matters
🌍 Real World
Playing melodies with a buzzer is common in alarms, toys, and simple music projects.
💼 Career
Understanding how to control hardware like buzzers with code is useful for embedded systems and electronics jobs.
Progress0 / 4 steps
