Created
February 18, 2016 15:30
-
-
Save PseudoSky/6ec6bba89a7652d8f007 to your computer and use it in GitHub Desktop.
Nicer plotter for differential visualization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.serial.*; | |
Serial myPort; // The serial port | |
int xPos = 1; // horizontal position of the graph | |
float inByte = 0; | |
float prev=0; | |
float prev2=0; | |
float norm=0; | |
void setup () { | |
// set the window size: | |
size(400, 900); | |
// List all the available serial ports | |
// if using Processing 2.1 or later, use Serial.printArray() | |
//println(Serial.list()); | |
int port=0; | |
String[] ser=Serial.list(); | |
for(int i=0; i<ser.length; i++){ | |
if(ser[i].contains("usbmodem")){ | |
port=i; | |
} | |
} | |
// I know that the first port in the serial list on my mac | |
// is always my Arduino, so I open Serial.list()[0]. | |
// Open whatever port is the one you're using. | |
myPort = new Serial(this, Serial.list()[port], 9600); | |
// don't generate a serialEvent() unless you get a newline character: | |
myPort.bufferUntil('\n'); | |
// set inital background: | |
background(0); | |
} | |
void draw () { | |
// draw the line: | |
stroke(127, 34, 255); | |
if(inByte>10){ | |
line(xPos, height/2-inByte, xPos, height/2+inByte); | |
//norm=pow((inByte-prev),15); | |
println(inByte); | |
//line(xPos, 50+norm, xPos, 50-norm); | |
prev2=prev; | |
prev=inByte; | |
// at the edge of the screen, go back to the beginning: | |
if (xPos >= width) { | |
xPos = 0; | |
background(0); | |
} else { | |
// increment the horizontal position: | |
xPos+=2; | |
} | |
} | |
} | |
void serialEvent (Serial myPort) { | |
// get the ASCII string: | |
String inString = myPort.readStringUntil('\n'); | |
if (inString != null) { | |
// trim off any whitespace: | |
inString = trim(inString); | |
// convert to an int and map to the screen height: | |
inByte = 0+ int(inString)*(pow((int(inString)-prev),2)/int(inString)); | |
prev=int(inString); | |
//inByte=inByte-(inByte-100); | |
//println(inByte); | |
//println(inByte); | |
inByte = map(inByte, 0, 256, 0, height); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment