Arduino transmitting
Using the the Arduino example “AnalogReadSerial” we get a simple source of values to transmit.
Processing code to receive:
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);
// set a communication port!
myPort = new Serial(this, Serial.list()[2], 9600);
// here we're saying we need to buffer until 'linefeed'
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);
}
}
Processing receiving TWO values!
If we want to send several values we can have the Arduino do that with a comma between as shown in the altered Arduino code beneath. 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 receive several 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 parts into integers.
String sensors[] = split(myString, ',');
if(sensors.length>1)
{
value1 = int(sensors[0]);
value2 = int(sensors[1]);
//remember to create an extra 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).
Processing transmitting
We can also do the opposite. Control stuff in the real world with stuff on the computer. Keyboard, mouse, GUI, network etc.
Processing code to transmit
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 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");
}
}
Arduino code to receive
int linefeed = 10;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the LED pin output:
pinMode(3, OUTPUT);
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0)
{
// look for the next integer in the serial stream:
int intensityVal = Serial.parseInt();
// look for the linefeed. That's the end of your
// sentence.
if (Serial.read() == linefeed) {
// set the intensity of the LED:
analogWrite(3, intensityVal);
}
}
delay(20);
}
Sending several values from Processing
myPort.write("200,90\n");
Receiving several values on Arduino
It’s just a question of listening for more values. But it’s important that you program to receive as many as you send. Otherwise you can get unpredictable behaviour.
// look for the next integer in the incoming serial stream: int firstValue = Serial.parseInt(); // do it again: int secondValue = Serial.parseInt();