Skip to content

Instantly share code, notes, and snippets.

@fvdbosch
Created May 29, 2015 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fvdbosch/86a3e7d409cdf3216895 to your computer and use it in GitHub Desktop.
Save fvdbosch/86a3e7d409cdf3216895 to your computer and use it in GitHub Desktop.
Control LED strip based on Serial input
import time
import serial
from neopixel import *
# LED strip configuration:
LED_COUNT = 60 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
# Define functions which reads serial input.
def readline(port):
received = ""
while True:
character = port.read()
received += character
if character=='\n':
return received
# Define function which animates LEDs.
def button(strip, r, g, b, wait_ms=0):
"""Button animation"""
# Gradually light up
for j in range(100):
for i in range(strip.numPixels()):
strip.setPixelColor(i, Color(r*j/100, g*j/100, b*j/100))
strip.show()
time.sleep(wait_ms/1000.0)
# Gradually light off
for j in range(100):
for i in range(strip.numPixels()):
strip.setPixelColor(i, Color(r*(100-j)/100, g*(100-j)/100, b*(100-j)/100))
strip.show()
time.sleep(wait_ms/1000.0)
# Turn off
for i in range(strip.numPixels()):
strip.setPixelColor(i, Color(0, 0, 0))
strip.show()
# Main program logic follows:
if __name__ == '__main__':
# Create NeoPixel object with appropriate configuration.
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
# Intialize the library (must be called once before other functions).
strip.begin()
# Create serial port object with approoriate configuration.
port = serial.Serial("/dev/ttyACM0", baudrate=115200)
print 'Press Ctrl-C to quit.'
while True:
# Wait for serial input
readline(port)
# Then flash LEDs
button(strip, 255, 255, 255)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment