from machine import Pin, SPI,Timer
import max7219
import gc
from utime import sleep
spi = SPI(0, 10_000_000, sck=Pin(2), mosi=Pin(3), miso=Pin(4))
screen = max7219.Max7219(32, 8, spi, Pin(5))
btn_start=Pin(13,Pin.IN,Pin.PULL_UP)
coin_sig=Pin(14,Pin.IN,Pin.PULL_UP)
second=0
minutes=20
hour=0
counter = 0 # Initialize the button press count
debounce_timer = None
run=0
coin=0
update=True
sec_timer = Timer()
def disp_text(s):
length = len(s)
column = (length * 8) #Calculate number of columns of the message
if length<=4:
screen.fill(0)
screen.text(s,0,0,1)
screen.how()
else:
for i in range(8,-column+32,-1):
screen.fill(0)
screen.text(s,i,0,1)
screen.show()
if coin>0:
break
sleep(0.03)
def start_pressed(pin):
global update,run,counter, debounce_timer # Declare variables as global
if debounce_timer is None:
run=not run
update=False
# Start a timer for debounce period (e.g., 200 milliseconds)
debounce_timer = Timer()
debounce_timer.init(mode=Timer.ONE_SHOT, period=200, callback=debounce_callback)
def coin_pulse(pin):
global coin,counter, debounce_timer # Declare variables as global
if debounce_timer is None:
coin+=1
# Start a timer for debounce period (e.g., 200 milliseconds)
debounce_timer = Timer()
debounce_timer.init(mode=Timer.ONE_SHOT, period=200, callback=debounce_callback)
def debounce_callback(timer):
global debounce_timer
debounce_timer = None
def update_sec(timer):
global coin
if coin<=1:
sec_timer.deinit()
print("count down stop")
coin-=1
# Attach the interrupt to the button's rising edge
btn_start.irq(trigger=Pin.IRQ_FALLING, handler=start_pressed)
coin_sig.irq(trigger=Pin.IRQ_FALLING, handler=coin_pulse)
sleep(0.1)
print(machine.freq())
#screen.scroll_text("welcome to mesin beras",0.05)
while 1:
if update is False:
if run is True and coin>0 :
sec_timer.init(mode=Timer.PERIODIC, period=1000, callback=update_sec)
update=True
run=False
print("cound down start")
else:
sec_timer.deinit()
print("count down pause")
update=True
if coin>0:
screen.disp_text(str(coin)+"s")
else:
disp_text("selamat datang")
sleep(0.3)
disp_text("sila masukkan token")
sleep(0.5)
Loading
pi-pico-w
pi-pico-w