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)
# Inisialisasi Buzzer
buzzer = PWM(Pin(18))
buzzer.duty(512)
# Nada (frekuensi Hz)
notes = {
'B4': 494, 'C5': 523, 'D5': 587, 'E5': 659,
'A4': 440, 'G4': 392
}
# Liriksswoi
lines = [
"You know you're on my mind",
"And if the world don't break",
"I'll be shaking it",
"'Cause I'm a young man after all",
"And when the seasons change",
"Will you stand by me",
"'Cause I'm a young man built to fall"
]
# Melodiwoiik
melody_lines = [
['B4','A4','G4','A4','B4','A4'],
['G4','A4','B4','C5','B4','A4'],
['G4','A4','B4','C5','B4'],
['C5','D5','C5','B4','C5','D5','E5','D5'],
['D5','C5','B4','C5','D5','C5'],
['B4','C5','D5','C5','B4'],
['C5','D5','C5','B4','C5','D5','E5','D5']
]
# Ketukannwoiik
beats_lines = [
[4,4,4,4,4,2],
[4,4,4,4,4,2],
[4,4,4,4,2],
[4,4,4,4,4,4,2,2],
[4,4,4,4,4,2],
[4,4,4,4,2],
[4,4,4,4,4,4,2,2]
]
def display_full_sentence(text):
lcd.clear()
top = text[:16]
bottom = text[16:32]
lcd.move_to(0,0)
lcd.putstr(top)
lcd.move_to(0,1)
lcd.putstr(bottom)
def play_bar(line, melody, beats):
display_full_sentence(line)
for i, note in enumerate(melody):
freq = notes.get(note, 0)
dur = beats[i]
if freq > 0:
buzzer.freq(freq)
buzzer.duty(512)
else:
buzzer.duty(0)
sleep(0.4 * (4/dur))
buzzer.duty(0)
sleep(0.05)
sleep(1)
while True:
for i in range(len(lines)):
play_bar(lines[i], melody_lines[i], beats_lines[i])
lcd.clear()
lcd.putstr("The End. :3")
sleep(3)