void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define BUZZER_PIN 27
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Nada untuk lagu Happy Birthday
int melody[] = {
262, 262, 294, 262, 349, 330, // "Happy birthday to you"
262, 262, 294, 262, 392, 349, // "Happy birthday to you"
262, 262, 523, 440, 349, 330, 294, // "Happy birthday dear [name]"
466, 466, 440, 349, 392, 349 // "Happy birthday to you"
};
// Durasi setiap nada (4 = seperempat nada)
int noteDurations[] = {
4, 4, 8, 8, 8, 4,
4, 4, 8, 8, 8, 4,
4, 4, 8, 8, 8, 8, 4,
4, 4, 8, 8, 8, 4
};
void setup() {
// Inisialisasi OLED
if (!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.println("HBD KE 21");
display.println("DWI RAMADHANI");
display.display();
// Inisialisasi buzzer
pinMode(BUZZER_PIN, OUTPUT);
delay(2000); // Tampilkan teks selama 2 detik sebelum memainkan melodi
}
void loop() {
// Mainkan melodi Happy Birthday
for (int i = 0; i < 25; i++) {
int noteDuration = 1000 / noteDurations[i];
tone(BUZZER_PIN, melody[i], noteDuration);
delay(noteDuration * 1.3); // Memberikan jeda antar nada
noTone(BUZZER_PIN);
}
delay(5000); // Tunggu sebelum mengulang
}