Advertisement
belrey10

7 – LED W/Brightness Control

Nov 7th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | Source Code | 0 0
  1. import machine
  2. import time
  3.  
  4. # Initialize button on GPIO 15
  5. button = machine.Pin(1, machine.Pin.IN, machine.Pin.PULL_UP)
  6.  
  7. # Initialize LED on GPIO 14 as a PWM pin
  8. led = machine.PWM(machine.Pin(0))
  9.  
  10. # Predefined brightness levels (0-65535)
  11. brightness_levels = [0, 8192, 16384, 32768, 65535]
  12. current_level = 0
  13.  
  14. # Function to set the next brightness level
  15. def set_next_brightness():
  16.     global current_level
  17.     current_level = (current_level + 1) % len(brightness_levels)
  18.     led.duty_u16(brightness_levels[current_level])
  19.  
  20. # Main Loop
  21. while True:
  22.     if button.value() == 1:  # Button pressed
  23.         set_next_brightness()
  24.         time.sleep(0.2)  # Debounce button
  25.     time.sleep(0.01)
  26.  
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement