// SCALE is used to scale the size of the // visual representation of the sensor values int SCALE = 10; import processing.serial.*; int counter = 0; // is used to create a numerical order in the exported files. Serial myPort; // The serial port to listen to. int gotByte = -1; // which byte has last been received int command; // stores the value of the last command byte received short iID, iValue; // stores the value of the last id and value byte received int frames = 0; // used for measuring fps long startTime = 0; // used for measuring fps int fps = 0; // used for measuring fps int sensorWidth = SCALE; int sensorHeight = SCALE; int sensorsWide = 9; // number of sensors horizontally in the matrix int sensorsHigh = 7; // number of sensors vertically in the matrix float[][] punchValues; // value of the punch since the last reset float[][] sensorValues; // actual values of the sensors float[][] zeroValues; // values of the sensors when no force is applied void setup() { size(sensorWidth*sensorsWide,sensorHeight*sensorsHigh); // List all the available serial ports: println(Serial.list()); // Open the port your hardware is plugged into. Just use trial and error if you are unsure. myPort = new Serial(this, "COM8", 19200); noStroke(); // initialise the arrays punchValues = new float[sensorsWide][sensorsHigh]; sensorValues = new float[sensorsWide][sensorsHigh]; zeroValues = new float[sensorsWide][sensorsHigh]; for(int i=0;i 5) sendValuesNow = true; // Display a visual representation of the values you would like to see fill(sensorValues[i][j]*4); //fill(punchValues[i][j]*15); rect(i*sensorWidth, j*sensorHeight, sensorWidth, sensorHeight); } } if(sendValuesNow) sendPunchValues(); } byte[] inBuffer = new byte[100]; while (myPort.available() > 0) { int bytesReceived = myPort.readBytes(inBuffer); for(int i=0; i1000) { fps = (int)(1000*frames/t); startTime += t; frames = 0; } /////////////////////////////// } void keyPressed(){ // reset the punch bag if(keyCode==32){ zero(); // zero the sensor values sendPunchValues(); // send a file to reset the renderer } } void processByte(byte serial){ // We have to do some checking here to see that we have the // right bytes for the command, id and value. if(gotByte<1){ command = serial^0xFFFFFF00; // Check if the command is a noteOn command. // This is the only command being sent by our Arduino code. if(command==0x90) gotByte = 1; } else if(gotByte<2){ iID = parseShort(serial); gotByte = 2; } else if(gotByte<3){ iValue = parseShort(serial); gotByte = 0; processMidiPacket(command, iID, iValue); } } void processMidiPacket(int command, short id, short value){ //once we have the MIDI packet we can do something with it if(id==63)return; // since I only have 63 sensors I don't nead this one. int x = (int)(id/sensorsHigh); int y = id%sensorsHigh; // just in case we get ids we are not expecting int sensorX = (x>=sensorsWide)?sensorsWide-1:x; int sensorY = (y>=sensorsHigh)?sensorsHigh-1 :y; if(sensorValues[sensorX][sensorY] != value){ sensorValues[sensorX][sensorY] = value; } //if(id==8)println("8="+ value); //if(value!=0)println(id); //if(x==8 && y==0)println(value); } short parseShort(byte bVal){ short retVal = bVal; if(retVal<0) retVal = (short)(bVal^0xFF00); return retVal; } void zero(){ for(int i=0;i