import processing.serial.*; Serial myPort; int hPosition = 1; float currentReading; float lastReading; int count=0; int zeroLinePos=0; float gsrAverage,prevGsrAverage; float baseLine=0; long lastFlatLine=0; color graphColor=color(255,255,255); int baselineTimer=10000; int gsrValue; int gsrZeroCount=0; float gsrRange=0; int downhillCount=0; int uphillCount=0; boolean downhill; boolean peaked=false; float peak, valley; void setup () { size(900, 450); myPort = new Serial(this, Serial.list()[0], 9600); currentReading=0; lastReading=0; gsrAverage=0; background(0); smooth(); } void draw () { //best delay setting for gsr readings delay(50); //image(myMovie, 0, 0); if (gsrValue<15 &&gsrValue>-15){ if( gsrZeroCount>10){ currentReading=0;//flatline gsrAverage=0; baseLine=0; lastFlatLine=millis(); gsrZeroCount=0; // println("reset"); } gsrZeroCount++; } else{ currentReading=gsrValue-baseLine; gsrZeroCount=0; } if(millis()-lastFlatLine>baselineTimer){ baseLine=gsrAverage; } //graph colors if(gsrAverage>0 && gsrAverageheight/2.0*.25 && gsrAverageheight/2.0*.5 && gsrAverageheight/2.0*.75) graphColor=color(255,100,0); gsrRange=peak-valley; // at the edge of the screen, go back to the beginning: if (hPosition >= width) { hPosition = 0; //cover last drawing fill(0,200); noStroke(); rect(0,0,width,height); } else { hPosition+=1; } gsrAverage=smooth(currentReading,.97,gsrAverage); //draw stuff //spike //noStroke(); if(gsrRange>200){ fill(255); ellipse(10,10,20,20); } else{ fill(0); ellipse(10,10,20,20); } //graph strokeWeight(1.5); // stroke(graphColor); // line(hPosition-1, height/2.0-lastReading, hPosition, height/2.0-currentReading); stroke(255,0,100); line(hPosition-1,height/2.0-prevGsrAverage,hPosition,height/2.0-gsrAverage); //draw peaks int thres=7; noFill(); stroke(255,0,0); strokeWeight(2); if (currentReading-thres>lastReading&& peaked==true){ downhill=false; //println(downhillCount); uphillCount++; downhillCount=0; //point(hPosition-1, height/2.0-lastReading); valley=lastReading; peaked=false; } if(currentReading+thres 1){ // check to make sure param's are within range filterVal = .99; } else if (filterVal <= 0){ filterVal = 0; } smoothedVal = (data * (1 - filterVal)) + (smoothedVal * filterVal); return (int)smoothedVal; }