//based on exercise 5-3 - move rectangle across the screen /* float x = 0; void setup() { size(200,200); smooth(); } void draw() { background(255); fill(0); rect(x,100,20,20); x = x + 1; if (x > 200) { x = 0; } } */ import processing.serial.*; //import processing library int linefeed = 10; Serial myPort; //the serial port int pot1; //variable for potentiometer position float x = 0; void setup() { size(400,400); smooth(); //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 pot1 = 0; } void draw() { background(255); fill(0); rect(x,100,20,20); x = pot1; if (x > width) { x = 0; } } 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"); pot1 = sensors[sensorNum]; } //add a linefeed after all the sensor values are printed println(); } }