import ddf.minim.*;
import processing.serial.*;
Serial myPort;
float inX;
float inY;
float inZ;
float inBend;
float inR;
float inG;
float inB;
float inT;
float inH;
float bX;
float bY;
float bZ;
int check = 0;
float sensors[];
int ch = 0;
String myString;
ArrayList <PVector> pos = new ArrayList <PVector>();
FloatList sw = new FloatList();
ArrayList <PVector>cc = new ArrayList <PVector>();
float travel = 0;
void setup(){
size(800, 800 );
pos = new ArrayList();
pos.add(new PVector(width/2,height/2,0));
cc.add(new PVector(255,255,255));
sw.append(1);
String portName = Serial.list()[2];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
}
void draw(){
background(0);
if(travel != 80){
PVector lastp = pos.get(pos.size()-1);
float rtx = random(-travel,travel);
float rty = random(-travel,travel);
if(rtx + lastp.x > width-20 || rtx + lastp.x < 20){
rtx = rtx * -1; }
if(rty + lastp.y > height-20 || rty + lastp.y < 20){
rty = rty * -1;
}
float newx = rtx + lastp.x;
float newy = rty + lastp.y;
pos.add(new PVector(newx,newy,0));
float ww = (travel/15);
if(ww< 0){
sw.append(1);
}else{
sw.append(4*(travel/15));
}
cc.add(new PVector(random(255),random(255),random(255)));
}
for(int i = 0; i < pos.size()-1; i++){
PVector fp = pos.get(i);
PVector fe = pos.get(i+1);
PVector c = cc.get(i);
strokeWeight(sw.get(i));
stroke(c.x,c.y,c.z);
line(fp.x,fp.y,fe.x,fe.y);
}
noStroke();
fill(80);
rect(100,40,200,20);
textSize(12);
fill(255);
textAlign(RIGHT);
text("FLEX",90,55);
fill(0,255,255);
rect(100,40,250-inBend*.8,20);
travel = (250 - inBend*.8) *.4;
if(inX > 0 && check == 0){
bX = inX;
bY = inY;
bZ = inZ;
check = 1;
}
if (myString != null) {
myString = trim(myString);
float sensors[] = float(split(myString, ','));
inX=sensors[0];
inY=sensors[1];
inZ=sensors[2];
inBend=sensors[6];
inR=sensors[3];
inG=sensors[4];
inB=sensors[5];
inT=sensors[7];
inH=sensors[8];
}
}
void serialEvent(Serial myPort) {
// read the serial buffer:
myString = myPort.readStringUntil('\n');
}