from machine import Pin, ADC
import time
switch = Pin(15, Pin.IN, Pin.PULL_UP)
btn_up = Pin(13, Pin.IN, Pin.PULL_UP)
btn_down = Pin(12, Pin.IN, Pin.PULL_UP)
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB)
relay_led1 = Pin(23, Pin.OUT)
relay_led2 = Pin(22, Pin.OUT)
led1 = Pin(21, Pin.OUT)
led2 = Pin(18, Pin.OUT)
led3 = Pin(16, Pin.OUT)
led4 = Pin(2, Pin.OUT)
tempo_total = 0
ultimo_update = time.ticks_ms()
ultimo_blink = time.ticks_ms()
led_blink_estado = False
ultimo_press_up = 0
ultimo_press_down = 0
debounce = 200
intervalo = 500
adicionar = 10
maximo = 40
minimo = 0
modo_atual = -1
while True:
now = time.ticks_ms()
modo = switch.value()
if modo != modo_atual:
relay_led1.value(0)
relay_led2.value(0)
led1.value(0)
led2.value(0)
led3.value(0)
led4.value(0)
tempo_total = 0
ultimo_update = now
ultimo_blink = now
led_blink_estado = False
ultimo_press_up = 0
ultimo_press_down = 0
modo_atual = modo
if modo == 1:
print("Modo Automatico")
valor_luz = ldr.read()
if valor_luz < 1000:
relay_led1.value(1)
relay_led2.value(1)
if time.ticks_diff(now, ultimo_blink) >= intervalo:
led_blink_estado = not led_blink_estado
led1.value(led_blink_estado)
led2.value(led_blink_estado)
ultimo_blink = now
else:
relay_led1.value(0)
relay_led2.value(0)
if time.ticks_diff(now, ultimo_blink) >= intervalo:
led_blink_estado = not led_blink_estado
led1.value(led_blink_estado)
ultimo_blink = now
led2.value(0)
led3.value(0)
led4.value(0)
else:
print("Modo Manual")
if btn_up.value() == 0 and time.ticks_diff(now, ultimo_press_up) > debounce:
if tempo_total < maximo:
tempo_total += adicionar
if tempo_total > maximo:
tempo_total = maximo
ultimo_press_up = now
if btn_down.value() == 0 and time.ticks_diff(now, ultimo_press_down) > debounce:
if tempo_total > minimo:
tempo_total -= adicionar
if tempo_total < minimo:
tempo_total = minimo
ultimo_press_down = now
leds_on = (tempo_total + 9) // 10
led1.value(1 if leds_on >= 1 else 0)
led2.value(1 if leds_on >= 2 else 0)
led3.value(1 if leds_on >= 3 else 0)
led4.value(1 if leds_on >= 4 else 0)
relay_led1.value(1 if tempo_total > 0 else 0)
relay_led2.value(1 if tempo_total > 0 else 0)
if tempo_total > 0 and time.ticks_diff(now, ultimo_update) >= 1000:
tempo_total -= 1
leds_on = (tempo_total + 9) // 10
led1.value(1 if leds_on >= 1 else 0)
led2.value(1 if leds_on >= 2 else 0)
led3.value(1 if leds_on >= 3 else 0)
led4.value(1 if leds_on >= 4 else 0)
ultimo_update = now