#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define BUZZER_PIN 27
// Create an SSD1306 display object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Updated "Happy Birthday" melody with correct structure
int melody[] = {
262, 262, 294, 262, 349, 330, // "Happy birthday to you"
262, 262, 294, 262, 349, 330, // "Happy birthday to you" (repeated)
262, 262, 523, 440, 349, // "Happy birthday"
466, 466, 440, 349, 392, 349, // "Happy birthday"
262, 262, 294, 262, 349, 330, // "Happy birthday to you"
262, 262, 294, 262, 349, 330 // "Happy birthday to you" (final part)
};
// Duration for each note (4 = quarter note)
int noteDurations[] = {
4, 4, 8, 8, 8, 4,
4, 4, 8, 8, 8, 4,
4, 4, 8, 8, 16,
4, 4, 8, 8, 8,
4, 4, 8, 8, 8, 8, 20,
4, 4, 8, 8, 8, 16
};
void setup() {
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Stay here if initialization fails
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.println("HBD KE 21");
display.println("SANIA");
display.display();
// Initialize buzzer
pinMode(BUZZER_PIN, OUTPUT);
delay(2000); // Show text for 2 seconds before playing melody
}
void loop() {
// Play Happy Birthday melody
for (int i = 0; i < 25; i++) {
int noteDuration = 1000 / noteDurations[i];
tone(BUZZER_PIN, melody[i], noteDuration); // Play tone
delay(noteDuration * 1.3); // Delay between notes
noTone(BUZZER_PIN); // Stop tone after duration
}
delay(5000); // Wait before repeating the melody
}