As it's the season of goodwill ( and hackers covering everything in LEDS ) Nixiebot now has a string of WS2811 based RGB LEDs added. Since it's nixiebot they are, of course, controllable by hashtag too. An arduino nano is driving them using the wonderful fastLED library .
The arduino is connected to the USB on the pi and appears as /dev/ttyUSB0 , I added a routine to nixiebot to pick up on a new #lights hashtag and the arduino has a routine to check serial input and receive commands from nixiebot.
The lights are purely USB powered so I enabled the max_usb_current=1 parameter on the pi after making sure that the 5V regulator on nixiebot's power board can take the extra current. It's a 2A regulator and with the max_USB_current flag set then USB can deliver 1.2A.
Current for a raspberry Pi 2B and camera module should be under 700mA so we're good... as long as I don't turn on too many LEDs. There are 50 LEDs in the string, each of which can take up to 60mA so you can see there's ample possibility for exceeding the 1200mA avalable from USB. However the code just twinkles a few LEDs at a time so we're good there.
To change them just add the hashtag #lightsX to your nixiebot command, where X is a number from 1 to 7, to choose from some preset colour schemes:
1: Red, White, Blue, Green
2: Purple, Green, Red
3: Green, Gold, Blue
4: Purple, Pink, Yellow, Blue
The ardiuno code (see files) runs a loop that:
- fades all Leds towards black a bit
- If it's time, picks a random LED to turn a random colour from the current set
- Checks to see if there are any characters waiting in serial input and, if so, interprets the command.
- delays 50ms then loops back
So the overall effect is twinkling leds that each fade out over a few seconds. Of course this is all going on according to the arduino timer loop and the pictures taken by nixiebot are timed according to nixiebot's code which spoils the twinkle a bit in the movies that nixiebot makes. So I might have a go at making the code synchronous with the Pi when composing movies next, the idea is to enter a mode where the arduino waits for a "do the next frame" command from nixiebot whicih is issued after each frame of a movie is taken. This will mean quite a rewrite of the arduino loop ( which is recycled from another project of mine ) as all the timers currently run on the arduino millis() function.
The arduino runs a simple command protocol, all commands are two characters and have ':' as the third, followed by any arguments the newline. Currently Nixiebot only issues the "SC:" to choose a colour set.
Here's the python side of things, it's based off a copy of the glitch setting function:
def setLights(tweet) : #sets fairy ligts according to #Lights:[1-6] print("setting lights") level = re.compile(r'lights[1-7]') for tx in tweet['entities']['hashtags'] : t=tx['text'].lower() if level.match(t) : try : gl = int(t.split("lights")) print("lights req to ", gl) lights = gl lcom = serial.Serial("/dev/ttyUSB0",baudrate=19200,timeout=1.0) time.sleep(2.1) #allow time for arduino to reboot when port opened lcmd = "SC:"+str(lights)+'\n' print(lcmd) lcom.write(bytes(lcmd,"utf-8")) time.sleep(0.5) lcom.close() except : print("lights setting exception text = ", t, "split = ", gl) passThe only thing that had me mildly stumped was the fact that opening the serial port was resetting the arduino and subsequently the command was getting lost during arduino reboot. To cover this I just added a time.sleep(2.1) . In future I'm going to need to keep the port open globally if it's ever to be used for frame sync and so on. But for now this quick hack does the trick!
The arduino code should be available over in the files section of this project by the time you read this, merry Xmas and happy new year to all on hackaday!