Advertisement
Guest User

Untitled

a guest
Aug 10th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import RPi.GPIO as GPIO
  3. import time
  4.  
  5. colors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF]
  6. pins = {'pin_R':11, 'pin_G':12, 'pin_B':13} # pins is a dict
  7.  
  8. GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
  9. for i in pins:
  10. GPIO.setup(pins[i], GPIO.OUT) # Set pins' mode is output
  11. GPIO.output(pins[i], GPIO.HIGH) # Set pins to high(+3.3V) to off led
  12.  
  13. p_R = GPIO.PWM(pins['pin_R'], 2000) # set Frequece to 2KHz
  14. p_G = GPIO.PWM(pins['pin_G'], 2000)
  15. p_B = GPIO.PWM(pins['pin_B'], 5000)
  16.  
  17. p_R.start(0) # Initial duty Cycle = 0(leds off)
  18. p_G.start(0)
  19. p_B.start(0)
  20.  
  21. def map(x, in_min, in_max, out_min, out_max):
  22. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
  23.  
  24. def setColor(col): # For example : col = 0x112233
  25. R_val = (col & 0xFF0000) >> 16
  26. G_val = (col & 0x00FF00) >> 8
  27. B_val = (col & 0x0000FF) >> 0
  28.  
  29. R_val = map(R_val, 0, 255, 0, 100)
  30. G_val = map(G_val, 0, 255, 0, 100)
  31. B_val = map(B_val, 0, 255, 0, 100)
  32.  
  33. p_R.ChangeDutyCycle(R_val) # Change duty cycle
  34. p_G.ChangeDutyCycle(G_val)
  35. p_B.ChangeDutyCycle(B_val)
  36.  
  37. try:
  38. while True:
  39. for col in colors:
  40. setColor(col)
  41. time.sleep(0.5)
  42. except KeyboardInterrupt:
  43. p_R.stop()
  44. p_G.stop()
  45. p_B.stop()
  46. for i in pins:
  47. GPIO.output(pins[i], GPIO.HIGH) # Turn off all leds
  48. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement