In this recitation, I used two potentiometers to control the values of the tint and blur filter. The image changes from clear to blur and dark to bright according to the values from the potentiometers.
code for Arduino
// IMA NYU Shanghai
// Interaction Lab
// For sending multiple values from Arduino to Processing
void setup() {
Serial.begin(9600);
}
void loop() {
int sensor1 = analogRead(A0);
int sensor2 = analogRead(A1);
//int mappedsensor1 = map(sensor1,0,1023,0,255);
//int mappedsensor2 = map(sensor2,0,1023,0,255);
// keep this format
Serial.print(sensor1);
Serial.print(“,”); // put comma between sensor values
Serial.print(sensor2);
Serial.println(); // add linefeed after sending the last sensor value
// too fast communication might cause some latency in Processing
// this delay resolves the issue.
delay(100);
}
code for Processing
PImage img1; import processing.serial.*; String myString = null; Serial myPort; int NUM_OF_VALUES = 2; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ int[] sensorValues; /** this array stores values from Arduino **/ void setup() { size(800, 800); background(0); setupSerial(); img1 = loadImage("im.jpeg"); } void draw() { updateSerial(); printArray(sensorValues); float mappedsensor1 = map(sensorValues[0],0,1023,0,255); float mappedsensor2 = map(sensorValues[1],0,1023,0,255); float mappedsensor3 = map(sensorValues[0],0,1023,0,800); float mappedsensor4 = map(sensorValues[1],0,1023,0,800); image(img1,0,0,800,800); filter(BLUR,sensorValues[1]/100); tint(mappedsensor1,mappedsensor2); noFill(); circle(mappedsensor3,mappedsensor4,200); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[ 13 ], 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]); } } } } }