from machine import Pin, PWM
from time import sleep
from dht import DHT22
# Konfigurasi LCD parallel (16x2)
rs = Pin(16, Pin.OUT)
en = Pin(17, Pin.OUT)
d4 = Pin(18, Pin.OUT)
d5 = Pin(19, Pin.OUT)
d6 = Pin(20, Pin.OUT)
d7 = Pin(21, Pin.OUT)
# Konfigurasi Keypad 4x4
rows = [Pin(2, Pin.OUT), Pin(3, Pin.OUT), Pin(4, Pin.OUT), Pin(5, Pin.OUT)]
cols = [Pin(6, Pin.IN, Pin.PULL_DOWN), Pin(7, Pin.IN, Pin.PULL_DOWN), Pin(8, Pin.IN, Pin.PULL_DOWN), Pin(9, Pin.IN, Pin.PULL_DOWN)]
key_map = [['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']]
# Konfigurasi DHT22
dht_sensor = DHT22(Pin(22))
# Tombol Push untuk Memasak Air
cook_button = Pin(10, Pin.IN, Pin.PULL_DOWN)
# Tombol Reset
reset_button = Pin(11, Pin.IN, Pin.PULL_UP)
# Buzzer
buzzer = PWM(Pin(15, Pin.OUT))
# Fungsi LCD
def lcd_send(data, rs_mode):
rs.value(rs_mode) # 0 untuk perintah, 1 untuk data
d4.value((data >> 4) & 1)
d5.value((data >> 5) & 1)
d6.value((data >> 6) & 1)
d7.value((data >> 7) & 1)
en.value(1)
sleep(0.0005)
en.value(0)
d4.value(data & 1)
d5.value((data >> 1) & 1)
d6.value((data >> 2) & 1)
d7.value((data >> 3) & 1)
en.value(1)
sleep(0.0005)
en.value(0)
def lcd_init():
lcd_send(0x33, 0) # Initialize
lcd_send(0x32, 0) # Set to 4-bit mode
lcd_send(0x28, 0) # 2 lines, 5x7 matrix
lcd_send(0x0C, 0) # Display on, cursor off
lcd_send(0x06, 0) # Entry mode
lcd_send(0x01, 0) # Clear display
sleep(0.1)
def lcd_putstr(data):
for char in data:
lcd_send(ord(char), 1)
def lcd_set_cursor(line, pos):
if line == 0:
address = 0x80 + pos
elif line == 1:
address = 0xC0 + pos
lcd_send(address, 0)
def lcd_clear():
lcd_send(0x01, 0)
sleep(0.1)
# Fungsi untuk membaca keypad
def read_keypad():
for row_num, row_pin in enumerate(rows):
row_pin.value(1)
for col_num, col_pin in enumerate(cols):
if col_pin.value() == 1:
row_pin.value(0)
return key_map[row_num][col_num]
row_pin.value(0)
return None
# Fungsi untuk mendapatkan input jumlah dari keypad
def get_input():
jumlah = ""
lcd_set_cursor(1, 0)
lcd_putstr("Jumlah: ")
while True:
key = read_keypad()
if key is not None:
if key == '#':
break
elif key.isdigit():
jumlah += key
lcd_putstr(key)
sleep(0.3) # Hindari input ganda akibat debounce
return int(jumlah) if jumlah.isdigit() else 0
# Fungsi untuk mendapatkan input jumlah dari keypad dengan reset
def get_input():
jumlah = ""
lcd_set_cursor(1, 0)
lcd_putstr("Jumlah: ")
while True:
key = read_keypad()
if key is not None:
if key == '#':
break
elif key == '*': # Reset input
jumlah = ""
lcd_set_cursor(1, 8) # Hapus angka yang ditampilkan
lcd_putstr(" ") # Menimpa angka sebelumnya dengan spasi
lcd_set_cursor(1, 8)
continue # kembali ke awal loop while
elif key.isdigit():
jumlah += key
lcd_putstr(key)
sleep(0.3) # Hindari input ganda akibat debounce
return int(jumlah) if jumlah.isdigit() else 0
# Fungsi Buzzer
def beep(freq, duration):
buzzer.freq(freq)
buzzer.duty_u16(32768)
sleep(duration)
buzzer.duty_u16(0)
# Fungsi Memasak Air
def cook_water():
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr("Memasak Air")
sleep(1)
while True:
dht_sensor.measure()
temp = dht_sensor.temperature()
lcd_set_cursor(1, 0)
lcd_putstr(f"Suhu: {temp:.1f}C ")
if temp >= 70:
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr("Air Siap!")
sleep(2)
lcd_clear()
beep(500, 0.5)
break
sleep(1)
# Fungsi Menu Pilih Kopi
def pilih_kopi():
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr("Pilih Kopi:")
lcd_set_cursor(1, 0)
lcd_putstr("A:Arab B:Am C:Luwak")
while True:
key = read_keypad()
if key in ['A', 'B', 'C']:
lcd_clear()
lcd_set_cursor(0, 0)
if key == 'A':
lcd_putstr("Arabica Dipilih")
elif key == 'B':
lcd_putstr("Americano Dipilih")
elif key == 'C':
lcd_putstr("Luwak Dipilih")
sleep(2)
lcd_clear()
beep(500, 0.5)
break
# Inisialisasi LCD
lcd_init()
while True: # Loop utama untuk mengulang keseluruhan proses
# Tampilkan pesan inisialisasi awal
lcd_set_cursor(0, 0)
lcd_putstr("Mesin Pembuat")
lcd_set_cursor(1, 0)
lcd_putstr("Kopi")
sleep(3)
lcd_clear()
# Memasak Air (Tahap awal yang akan diulang oleh reset)
lcd_set_cursor(0, 0)
lcd_putstr("Tekan Tombol")
lcd_set_cursor(1, 0)
lcd_putstr("Masak Air")
while True: # Loop untuk menunggu tombol masak air atau reset
if not reset_button.value(): # Cek tombol reset
sleep(0.5)
if not reset_button.value():
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr("Proses Direset")
sleep(2)
break # Keluar dari loop while dan kembali ke awal loop utama (menunggu masak air)
elif cook_button.value(): # cek jika tombol masak air di tekan
break #keluar dari loop while dan lanjut ke proses berikutnya
sleep(0.1) # delay untuk mengurangi beban prosesor
if not cook_button.value(): # jika tombol masak air tidak di tekan maka akan kembali ke awal loop while true
continue
cook_water()
# Menu Pilih Kopi
pilih_kopi()
# Proses pembuatan kopi
steps = ["Masukkan kopi", "Masukkan gula", "Masukkan air"]
for step in steps:
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr(step)
jumlah = get_input()
if jumlah < 0:
jumlah = 0
if jumlah > 0:
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr(f"{step}:")
i = jumlah
while i >= 0:
lcd_set_cursor(1, 8)
lcd_putstr(" ")
lcd_set_cursor(1, 8)
lcd_putstr(str(i))
sleep(1)
i -= 10
else:
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr("Tidak ada proses")
sleep(2)
lcd_clear()
beep(500, 0.5)
# Selesai
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr("Pembuatan Kopi")
lcd_set_cursor(1, 0)
lcd_putstr("Selesai!")
sleep(2)
if not reset_button.value(): # Cek tombol reset setelah selesai
sleep(0.5)
if not reset_button.value():
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr("Proses Direset")
sleep(2)
continue
# Tampilkan pesan inisialisasi awal
lcd_set_cursor(0, 0)
lcd_putstr("Mesin Pembuat")
lcd_set_cursor(1, 0)
lcd_putstr("Kopi")
sleep(3)
lcd_clear()
# Memasak Air
lcd_set_cursor(0, 0)
lcd_putstr("Tekan Tombol")
lcd_set_cursor(1, 0)
lcd_putstr("Masak Air")
while not cook_button.value():
sleep(0.1) # Tunggu tombol ditekan
cook_water()
# Menu Pilih Kopi
pilih_kopi()
# Proses pembuatan kopi (DIREVISI)
steps = ["Masukkan kopi", "Masukkan gula", "Masukkan air"]
for step in steps:
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr(step)
jumlah = get_input()
if jumlah < 0:
jumlah = 0
if jumlah > 0:
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr(f"{step}:")
i = jumlah
while i >= 0:
lcd_set_cursor(1, 8)
lcd_putstr(" ")
lcd_set_cursor(1, 8)
lcd_putstr(str(i))
sleep(1)
i -= 10
else:
lcd_clear()
lcd_set_cursor(0,0)
lcd_putstr("Tidak ada proses")
sleep(2)
lcd_clear()
beep(500, 0.5)
# Selesai
lcd_clear()
lcd_set_cursor(0, 0)
lcd_putstr("Pembuatan Kopi")
lcd_set_cursor(1, 0)
lcd_putstr("Selesai!")