# importa llibreries
from machine import Pin, ADC
import time
# definició d'objectes
led = Pin(23, Pin.OUT)
boto = Pin (15, Pin.IN)
sensor = ADC (Pin(34))
sensor.width (ADC.WIDTH_10BIT) # per obtenir valors entre 0 i 1023
# variables
estat = False
temps_inicial = time.ticks_ms()
temps_limit = 200 # període d'actualització
# execució contínua
while True:
temps_actual = time.ticks_ms()
lectura = boto.value()
llum = sensor.read() # llegeix l'entrada analògica
if (temps_actual - temps_inicial) >= temps_limit:
if lectura == 1:
estat = not estat
if estat == True:
if llum < 500: # llindar llum
led.on()
else:
led.off()
estat = not estat
else: # para que se pueda apagar con el botón
led.off()
print(llum)
temps_inicial = temps_actual