from machine import Pin, PWM, ADC
from machine import I2C
from hcsr04 import HCSR04
import ssd1306
import time
import dht
from time import sleep
i2c = I2C(scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB)
led2 = Pin(14, Pin.OUT)
medidor = HCSR04(trigger_pin=4, echo_pin=5)
led = Pin(13, Pin.OUT)
pwm_led = PWM(led)
def intensidad(distancia):
if distancia <= 100:
pwm_led.duty(1023)
print("Ciudadano detectado, ACTIVADO\n")
elif distancia <= 200:
pwm_led.duty(256)
print("Sin detectar ciudadano, REPOSO\n")
else:
pwm_led.duty(0)
print("APAGADO\n")
while True:
distancia = medidor.distance_cm()
print("Distancia: ", distancia)
intensidad(distancia)
time.sleep(1)
#pt2
val_ldr = ldr.read()
oled.fill(0)
oled.text("Iluminacion: ", 14, 10)
oled.text(str(val_ldr), 50, 30)
umbral = 500
if val_ldr < umbral:
led2.on()
else:
led2.off()
oled.show()
sleep(1)