//Processing code //inputs xyz data via Bluetooth Mate and outputs color data to the computer screen. //Arlene Ducao, Dec 2010 import processing.serial.*; Serial myPort; // Create object from Serial class int xval, yval, zval; // Data received from the serial port int accelXmin = 400; int accelXMax=650; int accelYmin = 400; int accelYMax=600; int accelZmin = 430; int accelZMax=650; int rgbMin = 0, rgbMax=255; int adjustedX, adjustedY, adjustedZ; // values adjusted to RGB range String xString, yString, zString; void setup() { size(200, 200); println(Serial.list()); //list all the available serial ports //replace the 8 in the next line with your serial port number from the list above. String portName = Serial.list()[4]; //My device is using "port 8" on my laptop myPort = new Serial(this, portName, 115200); //115200 is the data rate associated with this Bluetooth module } void draw() { if ( myPort.available() > 0) { // If data is available, xString = myPort.readStringUntil('\t'); //reads serial port until tab if (xString != null) { //checks if xString is null xString = trim(xString); xval = int(xString); //converts string to integer } yString = myPort.readStringUntil('\t'); //reads serial port until tab if (yString != null) { //checks if yString is null yString = trim(yString); yval = int(yString); //converts string to integer } zString = myPort.readStringUntil('\n'); //reads serial port until end of line if (zString != null) { //checks if zString is null zString = trim(zString); zval = int(zString); //converts string to integer } //maps the sensor value onto the range 1-255 to correspond with color values adjustedX = (xval - accelXmin) * (rgbMax - rgbMin) / (accelXMax - accelXmin) + rgbMin; adjustedY = (yval - accelYmin) * (rgbMax - rgbMin) / (accelYMax - accelYmin) + rgbMin; adjustedZ = (zval - accelZmin) * (rgbMax - rgbMin) / (accelZMax - accelZmin) + rgbMin; background(50); // Set background to dark grey fill(adjustedX, adjustedY, adjustedZ); // set fill to accelerometer values rect(50, 50, 100, 100); // this rect will be filled with the accelerometer values above } }