/*Serial string reader Reads a string of characters from a serial port Converts the strings into ints and prints Defines sensor value as variable "pot0" Uses "pot0" to control height of rectangle Data from sensor is visualized */ import processing.serial.*; //import processing library int linefeed = 10; Serial myPort; //the serial port int pot0; //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; } void draw() { background(0); fill(255,0,0); noStroke(); rect(width/2, 10, 20, pot0); } 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]; } //add a linefeed after all the sensor values are printed println(); } }