#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BUZZER_PIN 8
#define LED_PIN 13 // Example pin for the LED
// Melody of "Happy Birthday"
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
};
// Note durations: 4 = quarter note, 8 = eighth note, etc.
int noteDurations[] = {
5, 5, 5, 5, 5, 3,
5, 5, 5, 5, 5, 3,
5, 5, 5, 5, 5, 5, 3,
5, 5, 5, 5, 5, 3
};
void setup() {
// Initialize the display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Clear the buffer
display.clearDisplay();
// Set up the buzzer pin
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Display "Happy Birthday" followed by a heart on the SSD1306 display
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.clearDisplay();
display.setCursor(22, 10); // Center the text
display.println("Happy Birthday");
display.setTextSize(2);
display.setCursor(45,40); // Center the heart
display.println("<3"); // Heart symbol
display.display();
// Play the "Happy Birthday" melody
for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) {
int noteDuration = 1000 / noteDurations[i];
tone(BUZZER_PIN, melody[i], noteDuration);
// Blink the LED
digitalWrite(LED_PIN, HIGH); // Turn on the LED
delay(noteDuration * 0.9); // Adjust the blinking duration if needed
digitalWrite(LED_PIN, LOW); // Turn off the LED
int pauseBetweenNotes = noteDuration * 1.30; // Adjust the tempo
delay(pauseBetweenNotes);
noTone(BUZZER_PIN);
delay(50); // Add a little delay before the next note
}
// Clear the display after playing the melody
display.clearDisplay();
display.display();
// Wait before playing again
delay(0000); // 5 seconds delay before playing again
}