Friday, 22 October 2010


Got the shake button going - great, just threw away the code lifted from the web and started from scratch - much easier in the end!

/** * Read data from the serial port
*/
import processing.serial.*;
Serial myPort; // Create object from Serial classint potX; // Data received from the serial portint potY;int button;
int val;void setup() { //size(1024, 1024); size(512, 512); background(255); String portName = Serial.list()[2]; //choose from [0] [1] [2] myPort = new Serial(this, portName, 9600); myPort.bufferUntil('\n');}
void draw(){ // background(255); shake(); if(button == 1){ fill(255); rect(0, 0, width, height); shake(); }}
void shake(){ //mechanism to start again fill(255, 0, 0); // ellipse(potX, potY, 12, 12);
}void serialEvent(Serial myPort){
String inString = myPort.readStringUntil('\n'); //get the data in and divide it with each new line if(inString != null){ //check there is a full string inString = trim(inString); //remove any gaps String[] list = split(inString, ','); //split where there are commas potX = Integer.parseInt(list[1]); //make the Sting into an int potY = Integer.parseInt(list[2]); //ditto button = Integer.parseInt(list[0]);
//print them out so I can see what's happening println(" " + Integer.parseInt(list[1]) + " "); //pot right hand println(" " + list[2] + " "); //pot left hand println(" " + list[0] + " "); //button fill(255, 0, 0); //ellipse(Integer.parseInt(list[1]), Integer.parseInt(list[2])/2, 10, 10); // ellipse(potX, potY, 12, 12); stroke(0); // line(512 - valuesx[i], 512 - valuesy[i], 512-valuesx[i + 1], 512 - valuesy[i + 1]);
// line(512 - potX, 512 - potY, 512-potX + 1, 512 - potY + 1); strokeWeight(5); //dont allow values to go outside of screen int xVal = constrain(potX, 0, width); int yVal = constrain(potY, 0, width); point(xVal, potY); }
}

No comments:

Post a Comment