Using the the Arduino example “AnalogReadSerial” we get a simple source of values to transmit.
import processing.serial.*; int linefeed = 10; // new line ASCII = 10 Serial myPort; int value1 = 0; //this variable will contain the reading void setup () { size(800, 600); // change port! myPort = new Serial(this, Serial.list()[2], 9600); // here we're saying that we need to buffer until 'NEW LINE' myPort.bufferUntil(linefeed); } void draw () { //do something with "value1" } void serialEvent (Serial myPort) { // read serial buffer as string String myString = myPort.readString(); // if we have any other bytes than linefeed if (myString != null) { // trim crap myString = trim(myString); value1 = int(myString); //make string to integer println(value1); } }
If we want to send more values we can have the Arduino do that with a comma between. Note the use of both print and println to make it all be one transmitted row.
Serial.print(oneValue); Serial.print(","); Serial.println(otherValue);
By changing the processing code for the serialEvent we can recieve more values.
void serialEvent (Serial myPort) { // read serial buffer as string String myString = myPort.readString(); // if we have any other bytes than linefeed if (myString != null) { // trim crap myString = trim(myString); // split the string at commas // and convert sections into integers. String sensors[] = split(myString, ','); if(sensors.length>1) { value1 = int(sensors[0]); value2 = int(sensors[1]); //remember to create another value variable print(value1); print(','); println(value2); } } }
This means you now have the tool to influence anything you played around with in Processing (text, images, sound, filters, etc) with whatever input you put on Arduino (buttons, distance/light/pressure-sensors, knobs, sliders, etc).
We can also do the opposite. Control stuff in the real world with stuff on the computer. Keyboard, mouse, GUI, network etc.
import processing.serial.*; Serial myPort; // The serial port void setup() { size(800,600); background(0); // List all the available serial ports println(Serial.list()); // I know that the first port in the serial list on my mac // is always my Keyspan adaptor, so I open Serial.list()[0]. // Open whatever port is the one you're using. myPort = new Serial(this, Serial.list()[2], 9600); myPort.clear(); // Throw out the first reading, in case we started reading // in the middle of a string from the sender. } void draw() { } void mousePressed() { if (mouseButton == LEFT) { myPort.write("180\n"); } else { myPort.write("20\n"); } }
// pins for the LEDs: int ledPin = 3; int linefeed = 10; void setup() { // initialize serial: Serial.begin(9600); // make the pins outputs: pinMode(ledPin, OUTPUT); } void loop() { // if there's any serial available, read it: while (Serial.available() > 0) { // look for the next valid integer in the incoming serial stream: int intensityVal = Serial.parseInt(); // look for the newline. That's the end of your // sentence: if (Serial.read() == linefeed) { // fade the red, green, and blue legs of the LED: analogWrite(ledPin, intensityVal); } } delay(20); }
myPort.write("200,90,10\n");
It's just a question of listening for more values. But it's important that you program to recieve as many as you send. Otherwise you can get unpredictable behaviour.
// look for the next valid integer in the incoming serial stream: int firstValue = Serial.parseInt(); // do it again: int secondValue = Serial.parseInt(); // do it again: int thirdValue = Serial.parseInt();