// Pin definitions
const int redPin = 8;
const int greenPin = 9;
const int bluePin = 10;
const int buzzerPin = 12;
// Define the frequency of each note for "Happy Birthday" song
const int happyBirthdayNotes[] = {262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392, 349, 262, 262, 523, 440, 349, 330, 294, 466, 466, 440, 349, 392, 349};
// Define the durations of each note (in milliseconds)
const int noteDurations[] = {400, 400, 800, 800, 800, 1600, 400, 400, 800, 800, 800, 1600, 400, 400, 400, 400, 800, 400, 400, 800, 400, 400, 400, 800, 800};
void setup() {
// Set RGB LED pins as outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Set buzzer pin as output
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Play "Happy Birthday" song and control the RGB LED
for (int i = 0; i < sizeof(happyBirthdayNotes) / sizeof(happyBirthdayNotes[0]); i++) {
// Control RGB LED
if (i % 3 == 0) {
analogWrite(redPin, 255);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
} else if (i % 3 == 1) {
digitalWrite(redPin, LOW);
analogWrite(greenPin, 255);
digitalWrite(bluePin, LOW);
} else {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
analogWrite(bluePin, 255);
}
// Play note
tone(buzzerPin, happyBirthdayNotes[i], noteDurations[i]);
delay(noteDurations[i] * 1); // Pause between notes
noTone(buzzerPin);
// Turn off all LEDs
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
delay(100); // Delay between notes
}
}