Advertisement
belrey10

Day 7: The Road Back – Creating an Interactive System with Sensors and LEDs

Nov 5th, 2024 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. from machine import Pin, ADC
  2. import time
  3.  
  4. # Initialize the LEDs, photoresistor, and button
  5. led1 = Pin(13, Pin.OUT)
  6. led2 = Pin(14, Pin.OUT)
  7. led3 = Pin(15, Pin.OUT)
  8. photoresistor = ADC(Pin(26))
  9. button = Pin(16, Pin.IN, Pin.PULL_DOWN)
  10.  
  11. while True:
  12.     # Read the value from the photoresistor
  13.     light_level = photoresistor.read_u16()
  14.  
  15.     # Read the value from the button
  16.     button_state = button.value()
  17.  
  18.     # Print the light level and button state (optional for debugging)
  19.     print(f"Light level: {light_level}, Button state: {button_state}")
  20.  
  21.     # Control the LEDs based on the sensor inputs
  22.     if light_level < 20000 and button_state == 1:
  23.         led3.on()
  24.         led2.off()
  25.         led1.off()
  26.     elif light_level < 20000 and button_state == 0:
  27.         led3.off()
  28.         led2.off()
  29.         led1.on()
  30.     elif button_state == 1:
  31.         led3.off()
  32.         led2.on()
  33.         led1.off()
  34.     else:
  35.         led3.off()
  36.         led2.off()
  37.         led1.off()
  38.  
  39.     # Delay for a short period
  40.     time.sleep(0.5)
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement