const int piezoPin = 9; // Connect the piezo buzzer to pin 9
void setup() {
pinMode(piezoPin, OUTPUT);
}
void loop() {
// Play a simple tone
tone(piezoPin, 1000); // 1000 Hz
delay(500); // Play for 500 milliseconds
noTone(piezoPin); // Stop the tone
delay(500); // Pause for 500 milliseconds
// Play a different tone
tone(piezoPin, 2000); // 2000 Hz
delay(500);
noTone(piezoPin);
delay(500);
// Play a different tone
tone(piezoPin, 5000); // 5000 Hz
delay(500);
noTone(piezoPin);
delay(500);
// Play a melody
int melody[] = {262, 294, 330, 349, 392, 440, 494, 523}; // Notes of C major scale
int noteDuration = 200; // Duration of each note in milliseconds
for (int i = 0; i < 8; i++) {
tone(piezoPin, melody[i], noteDuration);
delay(noteDuration);
noTone(piezoPin);
delay(50); // Short pause between notes
}
delay(1000); // Longer pause between melodies
}