/** * Test 01: get data from serial port * and plot it in a graph * The graph has diffirent colors for odd and even count numbers * * from http://robotpig.net/blogs/UAVs.php * * based on pre-installed sketch: * Examples -> Libraries -> Serial -> Simple Read * */ import processing.serial.*; Serial myPort; // Create object from Serial class int val; // Data received from the serial port int valx; // normalize the value for the window dimensions int xd = 1; // x-axis int stxr; // stroke colors R int stxg; // stroke colors G int stxb; // stroke colors B int xdd; // even-odd check int xdc; // even-odd check void setup() { size(700, 256); // I know that the first port in the serial list on my mac // is always my FTDI adaptor, so I open Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using. String portName = Serial.list()[0]; myPort = new Serial(this, portName, 9600); background(255); } void draw() { while (myPort.available() >= 3) { if (myPort.read() == 0xff) val = (myPort.read() << 8) | (myPort.read()); valx = val / 4 ; //normalize the value for the window dimensions } //even-odd number check xdd = xd / 2 ; xdc =xd - xdd * 2 ; //swing bar color if (xdc == 0 ) { stxr = 204 ; stxg = 233 ; stxb = 248; } else {stxr = 0 ; stxg = 146 ; stxb = 218;} ; println (xdc) ; //this is just for testing, you can delete it // line: stroke(stxr, stxg, stxb); line(xd, height, xd, height - valx); // redraw if (xd >= width) { xd = 0; background(255); } else { // counter: xd++; } }