Intro
This purpose of this recitation was to further connect Processing and Arduino. This time we used media as the bridge between them, in hopes that we can use this knowledge to help with our final projects.
Processing Code
import processing.serial.*;
String myString = null;
Serial myPort;
int NUM_OF_VALUES = 3; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues; /** this array stores values from Arduino **/
PImage photo;
void setup() {
size(500, 500);
background(0);
setupSerial();
img = loadImage(“IMG_5849.JPG”)
rectMode(CENTER);
}
void draw() {
updateSerial();
printArray(sensorValues);
fill(sensorValue[0]);
rect(width/2, height/2, width/2, height/2);
map(sensorValue[0], 0, 1023, 0, 255);
if (sensorValue[0]>= 0) {
tint(0, 0, 255, 150);
}
}
void setupSerial() {
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[67], 9600);
myPort.clear();
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]);
}
}
}
}
}