from machine import I2C, Pin, PWM
from time import sleep
import i2c_lcd
# --- Inisialisasi LCD I2C ---
i2c = I2C(0, sda = Pin(21), scl = Pin(22), freq = 400000)
addr = i2c.scan()[0] if i2c.scan() else 0x27
lcd = i2c_lcd.I2cLcd(i2c, addr, 2, 16)
from machine import Pin, PWM
from time import sleep_ms
# pin buzzer
buzzer = PWM(Pin(18)) # ganti dengan pin GPIO yang kamu pakai (misal 9)
# mapping nada (Do = C4)
notes = {
'C': 261,
'D': 294,
'E': 329,
'F': 349,
'G': 392,
'A': 440,
'B': 494,
'c': 523, # C tinggi
'd': 587,
'e': 659,
'f': 698,
'g': 784,
' ': 0 # spasi = diam
}
# notasi lagu Rasa Sayange (reff)
melody = "CCDCFE CCDCFE CCcAFED BBAFGF"
beats = [4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4]
tempo = 150 # semakin kecil semakin cepat
def play(note, duration):
if note == ' ':
buzzer.duty(0) # diam
sleep_ms(duration)
else:
buzzer.freq(notes[note])
buzzer.duty(512) # aktifkan buzzer (0 - 1023)
sleep_ms(duration)
buzzer.duty(0) # matikan buzzer (supaya ada jeda)
# mainkan lagu
while True:
for i in range(len(melody)) :
play(melody[i], beats[i] * tempo)
sleep_ms(tempo) # jeda antar nada