Kinect -> Processing -> Arduino -> LED strip

Hello, Im having trouble with the setup as mentioned in the title. The code works great, but when I leave the Kinect camera frame and come back, the led strips completely freeze up. If anyone could provide some insight or some ideas it'd be appreciated.

Arduino Code:

/* This code includes the FastLed library It recieves a 3 integers from the serial port, decodes them and outputs them to 3 led strips on pins 8, 9 and 10. @author */ #include <FastLED.h> #define NUM_STRIPS 3 #define NUM_LEDS 120 //led strip array CRGB leds[NUM_STRIPS][NUM_LEDS]; // Int value for each strip int headLED; int leftLED; int rightLED; void setup() { //adding strips FastLED.addLeds<NEOPIXEL, 8>(leds[0], NUM_LEDS); FastLED.addLeds<NEOPIXEL, 9>(leds[1], NUM_LEDS); FastLED.addLeds<NEOPIXEL, 10>(leds[2], NUM_LEDS); Serial.begin(9600); } void loop() { //updates data and sets led strips receiveData(); printLEDS(); } void printLEDS() { //sets the led strips to be on up until the value from the serial port FastLED.clear(); for (int led = 0; led < leftLED; led++) { leds[0][led] = CRGB::Blue; } for (int led = 0; led < headLED; led++) { leds[1][led] = CRGB::Blue; } for (int led = 0; led < rightLED; led++) { leds[2][led] = CRGB::Blue; } FastLED.show(); } void ledsRed() { //sets all leds to red, for testing purposes FastLED.clear(); for (int led = 0; led < 120; led++) { leds[0][led] = CRGB::Red; } for (int led = 0; led < 120; led++) { leds[1][led] = CRGB::Red; } for (int led = 0; led < 120; led++) { leds[2][led] = CRGB::Red; } FastLED.show(); } void receiveData() { //fucnction for recieving data. It waits for data from the serial port, then checks for the letter 'a'. //it then sets the first integer it finds to be 'headLED' and so forth. if (Serial.available() > 0) { if (Serial.find("a")) { headLED = Serial.parseInt(); leftLED = Serial.parseInt(); rightLED = Serial.parseInt(); } } } 

Processing code: import cc.arduino.; import org.firmata.;

/* This code uses kinect4WinSDK to find the y coordinates of the head and hands, preps them for serial sending and sends them on the COM3 serial port. It only does this when a body is in front of the camera. */ //libraries import processing.serial.*; import kinect4WinSDK.Kinect; import kinect4WinSDK.SkeletonData; //creating kinect object, other kinect stuff Kinect kinect; ArrayList <SkeletonData> bodies; //serial object Serial myPort; //boolean for transmitting data boolean transmitData = false; //float values for the coordinates of the persons head, left and right hands float headY; float l_HandY; float r_HandY; void setup() { //kinect stuff by kinect library author size(640, 480); background(0); kinect = new Kinect(this); smooth(); bodies = new ArrayList<SkeletonData>(); //setting up serial port String portName = "COM3"; myPort = new Serial(this, portName, 9600); //delay to ensure serial port connects to arduino ok delay(5000); } void draw() { //more stuff by author background(0); image(kinect.GetImage(), 320, 0, 320, 240); image(kinect.GetDepth(), 320, 240, 320, 240); image(kinect.GetMask(), 0, 240, 320, 240); for (int i=0; i<bodies.size (); i++) { drawSkeleton(bodies.get(i)); } } void drawSkeleton(SkeletonData _s) { //called when theres a body on screen. //gets the coordinates of the head and hands and prepares them for serial sending, making sure they stay as positive integers, mapping between 0 and the length of the strips, 120. headY = (GetCoords(_s, Kinect.NUI_SKELETON_POSITION_HEAD, Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER)*100); l_HandY = (GetCoords(_s, Kinect.NUI_SKELETON_POSITION_WRIST_LEFT, Kinect.NUI_SKELETON_POSITION_HAND_LEFT)*100); r_HandY = (GetCoords(_s, Kinect.NUI_SKELETON_POSITION_WRIST_RIGHT, Kinect.NUI_SKELETON_POSITION_HAND_RIGHT)*100); headY = map(headY, 0f, 255, 0f, 120); if (headY <= 0) { headY=1; } l_HandY = map(l_HandY, 0f, 255, 0f, 120); if (l_HandY <= 0) { l_HandY=1; } r_HandY = map(r_HandY, 0f, 255, 0f, 120); if (r_HandY <= 0) { r_HandY=1; } //if transmitData is true (theres a body on screen) write values to the serial port if (transmitData) { println(headY, l_HandY, r_HandY); myPort.write("a" + int(headY) + "," + int(l_HandY) + "," + int(r_HandY) + "\n"); } else { //if not print Waiting println("Waiting"); } } float GetCoords(SkeletonData _s, int _j1, int _j2) { // function for finding the body part y coordinates return(_s.skeletonPositions[_j1].y + _s.skeletonPositions[_j2].y); }; //the rest is code written by the library author, i dont know how it works really. appearEvent is called when a body appears, and disappearEvent when a body leaves. Same with moveEvent but I havent touched it. //I added the transmitData flip and made it say START and END to make it easier to read. void appearEvent(SkeletonData _s) { println("START"); //myPort.write("b" + 255 + "," + 255 + "," + 255 + "\n"); transmitData = true; if (_s.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED) { return; } synchronized(bodies) { bodies.add(_s); } } void disappearEvent(SkeletonData _s) { println("END"); transmitData = false; //myPort.write('\t'); synchronized(bodies) { for (int i=bodies.size ()-1; i>=0; i--) { if (_s.dwTrackingID == bodies.get(i).dwTrackingID) { bodies.remove(i); } } } } void moveEvent(SkeletonData _b, SkeletonData _a) { if (_a.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED) { return; } synchronized(bodies) { for (int i=bodies.size ()-1; i>=0; i--) { if (_b.dwTrackingID == bodies.get(i).dwTrackingID) { bodies.get(i).copy(_a); break; } } } } 

Touch here for the full post on the Arduino Apprentices tumblr

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s