import utime
import machine
from machine import I2C, Pin
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
#Test function for verifying basic functionality
print("Contador de 0 a 1000!")
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
lcd.putstr("Contador de 0 a 1000!")
utime.sleep(2)
lcd.clear()
lcd.putstr(" Eventos: 0000")
up = Pin(14, Pin.IN, Pin.PULL_UP)
down = Pin(15, Pin.IN, Pin.PULL_UP)
ini_bu = utime.ticks_ms()
ini_bd = utime.ticks_ms()
count = 0
def fun_up(up):
global count, ini_bu
actual = utime.ticks_ms()
if utime.ticks_diff(actual, ini_bu) > 250:
count = count + 1
if count > 1000:
count = 0;
lcd.move_to(6, 1)
aux = "{:04d}".format(count)
lcd.putstr(aux)
def fun_down(down):
global count, ini_bd
actual = utime.ticks_ms()
if utime.ticks_diff(actual, ini_bu) > 250:
count = count - 1
if count < 0:
count = 1000;
lcd.move_to(6, 1)
aux = "{:04d}".format(count)
lcd.putstr(aux)
up.irq(trigger = Pin.IRQ_FALLING, handler = fun_up)
down.irq(trigger = Pin.IRQ_FALLING, handler = fun_down)
while True:
pass