from machine import Pin, I2C, PWM
import ssd1306
import time
# Setup I2C OLED (GPIO 22=SCL, GPIO 21=SDA)
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# Inisialisasi OLED 128x64
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Setup buzzer di GPIO 13
buzzer = PWM(Pin(13), freq=1000, duty=0)
# Setup tombol di GPIO 26 dengan pull-up internal
button = Pin(26, Pin.IN, Pin.PULL_UP)
# Nada untuk lagu Happy Birthday (frekuensi dalam Hz)
notes = {
'C4': 262,
'D4': 294,
'E4': 330,
'F4': 349,
'G4': 392,
'A4': 440,
'B4': 494,
'C5': 523
}
# Urutan nada Happy Birthday (note, durasi ms)
happy_birthday_melody = [
('C4', 400), ('C4', 400), ('D4', 800), ('C4', 800), ('F4', 800), ('E4', 1600),
('C4', 400), ('C4', 400), ('D4', 800), ('C4', 800), ('G4', 800), ('F4', 1600),
('C4', 400), ('C4', 400), ('C5', 800), ('E4', 800), ('F4', 800), ('E4', 800), ('D4', 1600),
('A4', 400), ('A4', 400), ('F4', 800), ('D4', 800), ('G4', 800), ('F4', 1600)
]
def play_tone(freq, duration_ms):
buzzer.freq(freq)
buzzer.duty(512) # 50% duty cycle
time.sleep_ms(duration_ms)
buzzer.duty(0)
def play_happy_birthday():
for note, duration in happy_birthday_melody:
play_tone(notes[note], duration)
time.sleep_ms(50) # jeda antar nada
while True:
oled.fill(0)
if button.value() == 0: # tombol ditekan (aktif LOW)
oled.text("Happy Birthday!", 0, 0)