// First Row LED Pins
const int row1[] = {2, 4, 5, 18, 19, 0}; // Added dummy pin to match row2 length
// Second Row LED Pins
const int row2[] = {21, 22, 23, 25, 26, 33}; // Added GPIO 36 as the last LED
const int numLEDs = 6; // Increased to 6 LEDs
const int delayTime = 500; // LED delay time in milliseconds
const int buzzerPin = 15; // Buzzer pin
// Happy Birthday melody and note durations
int melody[] = {
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
};
int durations[] = {
4, 8, 4, 4, 4, 2,
4, 8, 4, 4, 4, 2,
4, 8, 4, 4, 4, 4, 2,
4, 8, 4, 4, 4, 2
};
unsigned long previousLedMillis = 0;
unsigned long previousToneMillis = 0;
int currentLed = 0;
int currentNote = 0;
void setup() {
for (int i = 0; i < numLEDs; i++) {
pinMode(row1[i], OUTPUT);
pinMode(row2[i], OUTPUT);
}
pinMode(buzzerPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
// LED Animation (non-blocking)
if (currentMillis - previousLedMillis >= delayTime) {
previousLedMillis = currentMillis;
turnOffAll();
digitalWrite(row1[currentLed], HIGH);
digitalWrite(row2[currentLed], HIGH);
currentLed = (currentLed + 1) % numLEDs;
}
// Buzzer Melody (non-blocking)
static bool notePlaying = false;
int noteDuration = 1000 / durations[currentNote];
if (!notePlaying && currentMillis - previousToneMillis >= noteDuration * 1.3) {
tone(buzzerPin, melody[currentNote], noteDuration);
previousToneMillis = currentMillis;
notePlaying = true;
}
if (notePlaying && currentMillis - previousToneMillis >= noteDuration) {
noTone(buzzerPin);
notePlaying = false;
currentNote = (currentNote + 1) % (sizeof(melody) / sizeof(melody[0]));
}
}
void turnOffAll() {
for (int i = 0; i < numLEDs; i++) {
digitalWrite(row1[i], LOW);
digitalWrite(row2[i], LOW);
}
}