Arduino and Kinect Communication

Processing can directly talk with a serial port. Two sections of code are shown here. The first will be the code for processing to talk to the Arduino. The second part will be code for the Arduino to write to the serial port.

There are a few things I want to point out. First, make sure if you change the BAUD rate of one program, you change the other BAUD rate to match each other. Second, you can add more variables to send but make sure the enoughData value matches the amount of data you plan on sending. This includes the eventTriggerValue value in both programs. Lastly, an important thing to remember is that the Arduino program has a default loop rate of 30 frames per second. The Processing program on the other hand has a 60 frames per second default loop rate. For most applications you don’t have to worry about this but it’s safe to note here.

import processing.serial.*;
 
// serial port object to access serial port
Serial myPort;

// int that will trigger comm with arduino
int eventTriggerValue = 'A';

// value to send to arduino
int testInt = 5;
 
void setup() {
	// prints out available serial ports and their COM port number
	println(Serial.list());

	// creates new port at 9600 BAUD from available port 0
	myPort = new Serial(this, Serial.list()[0], 9600);
}
 
/*---------------------------------------------------------------
Draw method. Writes values to serial port
----------------------------------------------------------------*/
void draw() {
	writeToPort();
}
 
/*---------------------------------------------------------------
Writes values to serial port
----------------------------------------------------------------*/
void writeToPort() {
	//write to serial for start reading values
	myPort.write(eventTriggerValue);

	// write a value to the port
	myPort.write(testInt);
}
// current imcoming value from kinect
int val;

// set data buffer from kinect
int enoughData = 2;

// int that will trigger comm with kinect
int eventTriggerValue = 'A';
 
void setup() {
	// start serial communication
	Serial.begin(9600);
}
 
void loop() {
	// check if enough data has been sent from the computer:
	if (Serial.available()>enoughData){

		// read first value. begin communication
		val = Serial.read();

		// if value is the event trigger char 'A'
		if(val == eventTriggerValue){
			// read the most recent byte, which is the x-value
			val = Serial.read();
			Serial.println(val);
		}
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *