Advertisement
ScratchMonkey

8x8 RGB LED on CrowPi2

May 1st, 2023
3,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | Source Code | 0 0
  1. import time
  2. from rpi_ws281x import PixelStrip, Color
  3.  
  4. # LED strip configuration:
  5. LED_COUNT = 64        # Number of LED pixels.
  6. LED_PIN = 12          # GPIO pin connected to the pixels (18 uses $
  7. LED_FREQ_HZ = 800000  # LED signal frequency in hertz (usually 800$
  8. LED_DMA = 10          # DMA channel to use for generating signal ($
  9. LED_BRIGHTNESS = 10  # Set to 0 for darkest and 255 for brightest
  10. LED_INVERT = False    # True to invert the signal (when using NPN $
  11. LED_CHANNEL = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
  12.  
  13. RIGHT_BORDER = [7,15,23,31,39,47,55,63]
  14. LEFT_BORDER = [0,8,16,24,32,40,48,56]
  15.  
  16. # Define functions which animate LEDs in various ways.
  17. def colorWipe(strip, color, wait_ms=50):
  18.     """Wipe color across display a pixel at a time."""
  19.     for i in range(strip.numPixels()):
  20.         strip.setPixelColor(i, color)
  21.         strip.show()
  22.         time.sleep(wait_ms / 1000.0)
  23.  
  24.  
  25. def theaterChase(strip, color, wait_ms=50, iterations=10):
  26.     """Movie theater light style chaser animation."""
  27.     for j in range(iterations):
  28.         for q in range(3):
  29.             for i in range(0, strip.numPixels(), 3):
  30.                 strip.setPixelColor(i + q, color)
  31.             strip.show()
  32.             time.sleep(wait_ms / 1000.0)
  33.             for i in range(0, strip.numPixels(), 3):
  34.                 strip.setPixelColor(i + q, 0)
  35.  
  36.  
  37. def wheel(pos):
  38.     """Generate rainbow colors across 0-255 positions."""
  39.     if pos < 85:
  40.         return Color(pos * 3, 255 - pos * 3, 0)
  41.     elif pos < 170:
  42.         pos -= 85
  43.         return Color(255 - pos * 3, 0, pos * 3)
  44.     else:
  45.         pos -= 170
  46.         return Color(0, pos * 3, 255 - pos * 3)
  47.  
  48.  
  49. def rainbow(strip, wait_ms=20, iterations=1):
  50.     """Draw rainbow that fades across all pixels at once."""
  51.     for j in range(256 * iterations):
  52.         for i in range(strip.numPixels()):
  53.             strip.setPixelColor(i, wheel((i + j) & 255))
  54.         strip.show()
  55.         time.sleep(wait_ms / 1000.0)
  56.  
  57.  
  58. # Create NeoPixel object with appropriate configuration.
  59. strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
  60. # Intialize the library (must be called once before other functions).
  61. strip.begin()
  62.  
  63. while True:
  64.     # start animation
  65.     print('Color wipe animations.')
  66.     colorWipe(strip, Color(255, 0, 0))  # Red wipe
  67.     colorWipe(strip, Color(0, 255, 0))  # Green wipe
  68.     colorWipe(strip, Color(0, 0, 255))  # Blue wipe
  69.     print('Theater chase animations.')
  70.     theaterChase(strip, Color(127, 127, 127))  # White theater chase
  71.     theaterChase(strip, Color(127, 0, 0))  # Red theater chase
  72.     theaterChase(strip, Color(0, 0, 127))  # Blue theater chase
  73.     print('Rainbow animations.')
  74.     rainbow(strip)
  75.     print('Wipe LEDs')
  76.     colorWipe(strip, Color(0, 0, 0), 10)
  77.  
Tags: RaspberryPi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement