Skip to content

Instantly share code, notes, and snippets.

@axiomsofchoice
Created November 3, 2011 19:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axiomsofchoice/1337475 to your computer and use it in GitHub Desktop.
Save axiomsofchoice/1337475 to your computer and use it in GitHub Desktop.
Code pulled together during a joint session at BarCamp London 9 on Arduino and Kinect
// Control a line of LED (connected to pins 8-11) via the serial port
// Sending a character '0', '1', '2' or '3' lights up one of the LEDs
// Note that for debugging the Arduino responds by sending 'A', 'B', 'C' or 'D' respectively
// Code pulled together during a joint session at BarCamp London 9 on Arduino and Kinect
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
Serial.begin(115200);
}
void loop() {
int commandByte = 0 ;
if(Serial.available() > 0) {
// read the incoming byte:
commandByte = Serial.read();
switch(commandByte) {
case'0':
digitalWrite(8, HIGH); // set the LED on
digitalWrite(9, LOW); // set the LED off
digitalWrite(10, LOW); // set the LED off
digitalWrite(11, LOW); // set the LED off
Serial.println('A', BIN);break;
case'1':
digitalWrite(8, LOW); // set the LED off
digitalWrite(9, HIGH); // set the LED on
digitalWrite(10, LOW); // set the LED off
digitalWrite(11, LOW); // set the LED off
Serial.println('B', BIN);break;
case'2':
digitalWrite(8, LOW); // set the LED off
digitalWrite(9, LOW); // set the LED off
digitalWrite(10, HIGH); // set the LED on
digitalWrite(11, LOW); // set the LED off
Serial.println('C', BIN);break;
case'3':
digitalWrite(8, LOW); // set the LED off
digitalWrite(9, LOW); // set the LED off
digitalWrite(10, LOW); // set the LED off
digitalWrite(11, HIGH); // set the LED on
Serial.println('D', BIN);break;
}
}
}
"""Sending Kinect commands to the Arduino via the serial port
Sends a character '0', '1', '2' or '3' to the Arduino depending on the height of the user's left hand
Code pulled together during a joint session at BarCamp London 9 on Arduino and Kinect
"""
import OSC
from OSC import *
import serial
# Need to change if running on a different box
port=r'\\.\COM15'
baud=115200
ser = serial.Serial(port, baud, timeout=0)
s = OSC.OSCServer(('127.0.0.1',7110))
s.addDefaultHandlers()
def printing_handler(addr, tags, stuff, source):
"""Does the actual handling of sending the command to the Arduino
"""
msg_string = "%s [%s] %s" % (addr, tags, str(stuff))
#sys.stdout.write("OSCServer Got: '%s' from %s\n" % (msg_string, getUrlStr(source)))
if(stuff[0] == 'r_hand'):
outval = '0'
#sys.stdout.write("OSCServer Got: '%s' from %s\n" % (msg_string, getUrlStr(source)))
height_val = stuff[3] # Usable numbers are from 0.2 (heightest) to 0.7 (lowest)
height_val = height_val*5
if height_val > 4:
outval = '3'
elif height_val >3:
outval = '2'
elif height_val > 2:
outval = '1'
else:
outval = '0'
print "height_val: %s" % height_val
ser.write(outval)
def user_handler(addr, tags, stuff, source):
pass
def new_user_handler(addr, tags, stuff, source):
print "New user: %s" % stuff
def new_skel_handler(addr, tags, stuff, source):
pass
def lost_user_handler(addr, tags, stuff, source):
print "Lost user"
# XXX: for now this is to denote a fail-safe (but it will only work when it ignores non-calibrated users)
#n.updateCommand(0.8)
s.addMsgHandler('/joint', printing_handler)
s.addMsgHandler('/user/1', user_handler)
s.addMsgHandler('/user/2', user_handler)
s.addMsgHandler('/user/3', user_handler)
s.addMsgHandler('/user/4', user_handler)
s.addMsgHandler('/user/5', user_handler)
s.addMsgHandler('/user/6', user_handler)
s.addMsgHandler('/user/7', user_handler)
s.addMsgHandler('/user/8', user_handler)
s.addMsgHandler('/user/9', user_handler)
s.addMsgHandler('/new_user', new_user_handler)
s.addMsgHandler('/new_skel', new_skel_handler)
s.addMsgHandler('/lost_user', lost_user_handler)
print "\nStarting OSCServer. Use ctrl-C to quit."
s.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment