from machine import Pin, ADC, Timer
from time import sleep, ticks_ms, ticks_diff
# -----
S1, S2, S3 = Pin(0, Pin.IN), Pin(1, Pin.IN), Pin(2, Pin.IN)
LED1, LED2, S = Pin(4, Pin.OUT), Pin(5, Pin.OUT), Pin(11, Pin.OUT)
AD1 = ADC(Pin(26))
debounce = ticks_ms()
ticker = Timer()
ON, OFF = 65535, 0
CONF = True
error = False
# -----
def check():
global OFF, ON, LED2, LED2, error
if OFF > ON:
LED1.on()
LED2.on()
print("ERROR")
error = True
else:
LED1.off()
LED2.on()
error = False
def click_S1(p):
global CONF, debounce, ON, AD1
if not CONF or ticks_diff(ticks_ms(), debounce) < 200:
return
else:
debounce = ticks_ms()
ON = AD1.read_u16()
print("ON=" + str(ON))
check()
def click_S2(p):
global CONF, debounce, error, OFF, AD1
if not CONF or ticks_diff(ticks_ms(), debounce) < 200:
return
else:
debounce = ticks_ms()
OFF = AD1.read_u16()
print("OFF=" + str(OFF))
check()
def click_S3(p):
global debounce, CONF, S, LED1, LED2
if error or ticks_diff(ticks_ms(), debounce) < 200:
return
else:
debounce = ticks_ms()
CONF = not CONF
if CONF:
print("CONF")
S.off()
LED1.off()
LED2.on()
else:
print("WORK")
LED1.on()
LED2.off()
def evaluate(p):
global CONF, AD1, OFF, ON, S
if (not CONF):
value = AD1.read_u16()
if value < OFF:
S.off()
elif value > ON:
S.on()
LED1.off()
LED2.on()
S.off()
S1.irq(handler=click_S1, trigger=Pin.IRQ_RISING)
S2.irq(handler=click_S2, trigger=Pin.IRQ_RISING)
S3.irq(handler=click_S3, trigger=Pin.IRQ_RISING)
ticker.init(period=20, mode=Timer.PERIODIC, callback=evaluate)
while True:
sleep(0.02)