/*Serial string reader Reads a string of characters from a serial port Converts the strings into ints and prints */ import processing.serial.*; //import processing library int linefeed = 10; Serial myPort; //the serial port int pot0; //variable for potentiometer position int pot1; //variable for potentiometer position int pot2; //variable for potentiometer position void setup() { size(400, 400); //list all available serial ports println(Serial.list()); //plug in the serial port number that is attached to Arduino, should be 0 on Mac myPort = new Serial(this, Serial.list()[0], 9600); //read bytes into a buffer until linefeed (ASCII 10) myPort.bufferUntil(linefeed); //initialize sensor value pot0 = 0; pot1 = 0; pot2 = 0; } void draw() { background(0); noStroke(); fill(255,0,0); rect(50, 10, 20, pot0); fill(0,255,0); rect(150, 10, 20, pot1); fill(0,0,255); rect(250, 10, 20, pot2); } void serialEvent(Serial myPort) { //read serial buffer String myString = myPort.readStringUntil(linefeed); //if you have any bytes other than linefeed if (myString != null) { myString = trim(myString); //convert sensor values into integers int sensors[] = int(split(myString, ',')); //print out the values from the variable sensor for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) { print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t"); pot0 = sensors[0]; pot1 = sensors[1]; pot2 = sensors[2]; } //add a linefeed after all the sensor values are printed println(); } }