Python + OS X + Arduino + BlinkM
I have a bunch of BlinkMs and an Arduino from an old project and I spent some time with them this weekend. This time, I used Python on a Mac to connect. I learned a little in the process, so I thought I’d share.
1. I used darwinports‘ Python2.4 and the pyserial library.
2. On the Arduino, I flashed the BlinkMCommunicator code available here.
3. When writing to the BlinkM’s eeprom, you need to pause briefly before sending another command.
4. The attached code uses decode(“hex”). I’ll explain that design choice later.
5. The code assumes you have 3 BlinkM’s hooked up to the Arduino with their addresses set as 1, 2 and 3.
import serial import time def toBlinkM (ser, command): print ">\t Sending "+command ser.write(command.decode("hex")) print ">\t\t Sent "+command def setBlinkMToPlaySimpleScript (ser, address, color1, color2, color3, color4, duration, fadespeed): # example: write line 0 of script 0 on BlinkM 1 # 01 Start code # 01 BlinkM address # 08 bytes to send # 00 bytes to receive # 57 command: write line # 00 script number # 00 line number # 20 duration # 63 fade # 20 R # 20 G # 00 B print ("> Playing Simple Script on "+address) toBlinkM(ser, "01"+address+"0800570000"+duration+"63"+color1) time.sleep (.2) toBlinkM(ser, "01"+address+"0800570001"+duration+"63"+color2) time.sleep (.2) toBlinkM(ser, "01"+address+"0800570002"+duration+"63"+color3) time.sleep (.2) toBlinkM(ser, "01"+address+"0800570003"+duration+"63"+color4) time.sleep (.2) # last line: play script 0 1 time toBlinkM(ser, "01"+address+"0800570004"+"00"+"70"+"000100") time.sleep (.2) # set script id 0 to a len. of 5, 1 repeats toBlinkM(ser, "01"+address+"04004C000501") time.sleep (.2) # set fade speed toBlinkM(ser, "01"+address+"020066"+fadespeed) time.sleep (.2) # play script id 0 toBlinkM(ser, "01"+address+"040070008000") time.sleep (.2) ser = serial.Serial('/dev/tty.usbserial-A4001lcU',19200, timeout=1) counter = 0 print "> Waiting for Arduino." while 1: serialline = ser.readline() if (serialline): print serialline.strip() if ('ready' in serialline): break print "> Arduino ready." # tell #1 to stop animating toBlinkM(ser, "010101006f") # tell #1 to show only green and red at 1 bright toBlinkM(ser, "0101040063010100") # tell #2 to stop animating toBlinkM(ser, "010201006f") # tell #2 to show only blue and green at 1 bright toBlinkM(ser, "0102040063000101") # tell #3 to stop animating toBlinkM(ser, "010301006f") # tell #3 to show only red at 3 bright toBlinkM(ser, "0103040063030000") time.sleep (10) setBlinkMToPlaySimpleScript(ser,"01","404040","FF0000","0000FF","FF0000","05","10") setBlinkMToPlaySimpleScript(ser,"02","400000","FF0000","800000","FF0000","05","10") setBlinkMToPlaySimpleScript(ser,"03","000000","FF0000","000000","FF0000","20","10")
Trackbacks / Pingbacks