Using the the Arduino example “AnalogInOutSerial” we get a simple source of values to transmit. However, start with cleaning up what's transmitted from:
Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue);
to
Serial.println(sensorValue);
import processing.serial.*; int lf = 10; // Linefeed in ASCII int value; String myString = null; 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. myString = myPort.readStringUntil(lf); myString = null; } void draw() { while (myPort.available() > 0) { myString = myPort.readStringUntil(lf); if (myString != null) { myString = trim(myString); value = int(myString); println(value); } } background(0); rect(width/2, height, 100, -value/2); }
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 mouseClicked() { myPort.write('0'); }
int ledPin = 13; // led is hoooked up to this pin int ledState; int nextChar; void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); ledState = LOW; } void loop(){ if (Serial.available()) { nextChar = Serial.read(); if (nextChar == '0') { if (ledState == HIGH) { ledState = LOW; Serial.println("led OFF"); } else { ledState = HIGH; Serial.println("led ON"); } digitalWrite(ledPin, ledState); } } }
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).
Try to build an etch-a-sketch, a digital photoalbum, or a game. And can you figure out how you would do to send several values at the same time from Arduino and recieve it in Processing? A hint: it involves substring on the Processing side.
myString = trim(myString); //Trim any spaces from the sides of the string myInt = int(myString); //Actually converting the string value into an integer
When you want to send several values (as with the joystick) you need to put some kind of character inbetween the values to make out what is what. In this case I use “|”.
dividerIndex = myString.indexOf('|'); //at what position in the string is my divider? stringValueX = myString.substring(0,dividerIndex); //get value before divider stringValueY = myString.substring(dividerIndex+1); //get value after divider stringValueX = trim(stringValueX); //trim away crap stringValueY = trim(stringValueY); //trim away crap myValueX = int(stringValueX); //convert the string value into an integer myValueY = int(stringValueY); //convert the string value into an integer
Remember that you can use map() to translate any input values to match a different set of numbers as pixels, color or frequency.
If you have 64 bit windows 7 you might find that you don't get any values to Processing. A sure sign is that when you start your sketch theres a printout along the lines of “library mismatch”. This is because Processing came with a serial library that doesn't play well with 64 bit. Have no fear. To correct this, do the following:
goto http://rxtx.qbang.org/wiki/index.php/Download and download rxtx-2.1-7-bins-r2.zip from "Binary" column. goto C:\Users\Downloads\rxtx-2.1-7-bins-r2.zip\rxtx-2.1-7-bins-r2 and copy RXTXcomm.jar to C:\Program Files\processing-1.5.1\java\lib\ext goto C:\Users\Downloads\rxtx-2.1-7-bins-r2.zip\rxtx-2.1-7-bins-r2\Windows\i368-mingw32 and copy both rxtxParallel.dll and rxtxSerial.dll to C:\Program Files\processing-1.5.1\java\bin
If you run into the same thing on OSx, try this:
dont use the RXTXcomm.jar and librxtxserial.jinlib thats with processing. copy the versions that are in your arduino folder under (contents/resources/java) from arudino -22 they are dated 24th dec and are 2.1-7 - ensure these versions are in the processing folder , the arduino folder you just copied them from (obviously they will be the right ones) and under macintosh hd/library/java/extensions