Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from machine import Pin, PWM, ADC
- import time
- # Initialize the LED pins for PWM and digital output
- led_main = PWM(Pin(15))
- led_main.freq(1000)
- led1 = Pin(16, Pin.OUT)
- led2 = Pin(17, Pin.OUT)
- # Initialize the photoresistor pin for ADC and button pins for input
- photoresistor = ADC(Pin(26))
- button1 = Pin(14, Pin.IN, Pin.PULL_DOWN)
- button2 = Pin(13, Pin.IN, Pin.PULL_DOWN)
- # Variables to keep track of button states and modes
- mode = 0
- last_button1_state = 0
- last_button2_state = 0
- while True:
- # Read the value from the photoresistor
- light_level = photoresistor.read_u16()
- # Map the light level to the PWM duty cycle (inverse relationship)
- duty_cycle = 65535 - light_level
- # Set the main LED brightness
- led_main.duty_u16(duty_cycle)
- # Read the button states
- button1_state = button1.value()
- button2_state = button2.value()
- # Toggle mode with button 1
- if button1_state == 1 and last_button1_state == 0:
- mode = (mode + 1) % 2
- # Control additional LEDs with button 2
- if button2_state == 1 and last_button2_state == 0:
- if led1.value() == 0:
- led1.on()
- led2.off()
- else:
- led1.off()
- led2.on()
- # Update last button states
- last_button1_state = button1_state
- last_button2_state = button2_state
- # Mode-specific behaviors
- if mode == 0:
- # Mode 0: Main LED controlled by photoresistor, additional LEDs toggle with button 2
- pass
- elif mode == 1:
- # Mode 1: Main LED blinks, additional LEDs toggle with button 2
- led_main.duty_u16(32767) # Set main LED to half brightness
- time.sleep(0.5)
- led_main.duty_u16(0) # Turn off main LED
- time.sleep(0.5)
- # Delay for a short period
- time.sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement