Skip to content

Instantly share code, notes, and snippets.

@axiomsofchoice
Created September 5, 2011 23:35
Show Gist options
  • Save axiomsofchoice/1196171 to your computer and use it in GitHub Desktop.
Save axiomsofchoice/1196171 to your computer and use it in GitHub Desktop.
Using the kinect to control LEDs on an arduino
"""Sending commands to the IR channel on the Arduino via the serial port
"""
import serial
import time
import OSC
from OSC import *
port = r'\\.\COM9'
baud = 9600
ser = serial.Serial(port, baud)
# first close before (re)-opening
# See also: http://stackoverflow.com/questions/2063257/trying-to-open-a-serial-port-with-pyserial-on-winxp-access-denied
ser.close()
try:
ser.open()
except:
print "Could not connect to: %s" % port
exit(-1)
print "Connected to port: %s" % ser.portstr
s = OSC.OSCServer(('127.0.0.1',7110))
s.addDefaultHandlers()
#y_max = 0.0
#y_min = 0.0
outval = 0
def printing_handler(addr, tags, stuff, source):
#global y_max
#global y_min
global outval
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'):
#sys.stdout.write("OSCServer Got: '%s' from %s\n" % (msg_string, getUrlStr(source)))
y_val = stuff[2]
#if stuff[2] > y_max: y_max = stuff[2]
#if stuff[2] < y_min: y_min = stuff[2]
#print "y_max: %s y_min: %s" % (y_max,y_min)
t = int(y_val * 100)
if t >= 0 and t < 10:
outval = 0
elif t >= 10 and t < 20:
outval = 1
elif t >= 20 and t < 30:
outval = 2
elif t >= 30 and t < 40:
outval = 3
elif t >= 40 and t < 50:
outval = 4
# Write data to the port
serCommand = "%s" % (outval)
ser.write(serCommand)
time.sleep(0.1)
def user_handler(addr, tags, stuff, source):
pass
def new_user_handler(addr, tags, stuff, source):
pass
def new_skel_handler(addr, tags, stuff, source):
pass
def lost_user_handler(addr, tags, stuff, source):
print "Lost user"
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('/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()
# Finally close the serial port
ser.close()
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor
This example code is in the public domain.
*/
int num = 0;
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
}
void loop() {
switch (num) {
case 0:
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;
case 1:
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;
case 2:
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
break;
case 3:
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
break;
case 4:
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
break;
}
//delay(1000); // waits for a second
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
}
//num++;
//num = num % 5;
num = incomingByte % 5;
//int sensorValue = analogRead(A0);
//Serial.println(sensorValue, DEC);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment