Advertisement
Guest User

Fade?

a guest
Sep 11th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python3
  2.  
  3. from PWM import Pwm
  4. from time import sleep
  5.  
  6. # Use for PWM on /dev/bone/pwm/
  7. # see https://pastebin.com/R70P1wAn
  8.  
  9. led = Pwm('/dev/bone/pwm/1/b', frequency=500, value=0)
  10.  
  11. def led_fade( to_value, seconds, steps=None ):
  12.     if steps is None:
  13.         steps = round( seconds * 20 )   # default to 20 steps per second
  14.  
  15.     from_value = led.value
  16.     for i in range( 0, steps ):
  17.         sleep( seconds / steps )
  18.         led.value = from_value + (1+i) * (to_value-from_value) / steps
  19.  
  20. try:
  21.     while True:
  22.         led_fade( 1.0, 2 )
  23.         led_fade( 0.0, 2 )
  24. except KeyboardInterrupt:
  25.     led.disable()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement