The video to show the program working was too large to post, but the idea behind the project was to learn how to use the processing with the arduino and make them interact with more than one value between each other.
import processing.serial.*;
String myString = null;
Serial myPort;
int NUM_OF_VALUES = 2; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues;
int preSensorValues;
int preSensorValues2;
/** this array stores values from Arduino **/
void setup() {
size(500, 500);
background(255);
setupSerial();
}
void draw() {
updateSerial();
printArray(sensorValues);
line(preSensorValues,preSensorValues2,sensorValues[0],sensorValues[1]);
preSensorValues=sensorValues[0];
preSensorValues2=sensorValues[1];
// use the values like this!
// sensorValues[0]
// add your code
//
}
void setupSerial() {
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
// WARNING!
// You will definitely get an error here.
// Change the PORT_INDEX to 0 and try running it again.
// And then, check the list of the ports,
// find the port “/dev/cu.usbmodem—-” or “/dev/tty.usbmodem—-”
// and replace PORT_INDEX above with the index number of the port.
myPort.clear();
// Throw out the first reading,
// in case we started reading in the middle of a string from the sender.
myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII
myString = null;
sensorValues = new int[NUM_OF_VALUES];
}
void updateSerial() {
while (myPort.available() > 0) {
myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII
if (myString != null) {
String[] serialInArray = split(trim(myString), “,”);
if (serialInArray.length == NUM_OF_VALUES) {
for (int i=0; i<serialInArray.length; i++) {
sensorValues[i] = int(serialInArray[i]);
}
}
}
}
}
After that the project required the arduino to interact from the information received from the processing, this was more complicated but in turn it helped to show how to send messages from processing to arduino.
// IMA NYU Shanghai
// Interaction Lab
// This code sends one value from Processing to Arduino
import processing.serial.*;
Serial myPort;
int valueFromArduino;
void setup() {
size(500, 500);
background(0);
printArray(Serial.list());
// this prints out the list of all available serial ports on your computer.
myPort = new Serial(this, Serial.list()[ 0], 9600);
// WARNING!
// You will definitely get an error here.
// Change the PORT_INDEX to 0 and try running it again.
// And then, check the list of the ports,
// find the port “/dev/cu.usbmodem—-” or “/dev/tty.usbmodem—-”
// and replace PORT_INDEX above with the index number of the port.
}
void draw() {
// to send a value to the Arduino
if (mouseX> 200) {
myPort.write(‘H’);
} else {
myPort.write(‘L’);
}
}
// IMA NYU Shanghai
// Interaction Lab
// This code receives one value from Processing to Arduino
char valueFromProcessing;
int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
// to receive a value from Processing
while (Serial.available()) {
valueFromProcessing = Serial.read();
}
if (valueFromProcessing == ‘H’) {
digitalWrite(ledPin, HIGH);
} else if (valueFromProcessing == ‘L’) {
digitalWrite(ledPin, LOW);
} else {
// something esle
}
// too fast communication might cause some latency in Processing
// this delay resolves the issue.
delay(10);
}