import uasyncio as asyncio
from machine import Pin
from utime import sleep
BUTTON_PIN = 5
LED_PIN = 2
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
led = Pin(LED_PIN, Pin.OUT)
MAX = 20
bottle_count = 0
box_count = 0
counting = True
emergency = False
last_count = 0
async def count_bottles():
global bottle_count, box_count, last_count
while True:
await asyncio.sleep(1)
if counting:
bottle_count += 1
print("Bottle count:", bottle_count)
if bottle_count >= MAX:
box_count += 1
bottle_count = 0
last_count = 0
print("Box filled with 20 bottles. Total boxes:", box_count)
async def emergency_blink():
while True:
if emergency:
led.on()
await asyncio.sleep(0.5)
led.off()
await asyncio.sleep(0.5)
else:
await asyncio.sleep(1)
def button_pressed(p):
global counting, emergency, last_count, bottle_count, box_count
counting = not counting
if not counting:
emergency = True
last_count = bottle_count
else:
emergency = False
bottle_count = last_count
button.irq(trigger=Pin.IRQ_FALLING, handler=button_pressed)
loop = asyncio.get_event_loop()
loop.create_task(count_bottles())
loop.create_task(emergency_blink())
loop.run_forever()