For this recitation, I focused on making a parallel pattern with a light sensor and the brightness of a photo. The light sensor composes of the Arduino part while the brightness of the photo is from Processing. This parallel seemed like a natural process and is similar to what happens to the computer screen or phone screen that auto detect brightness and change itself accordingly which was what I aimed to mirror. Essentially, this is similar to how the brightness of a camera or the iso can be modified to be dimmed or brightened according to the brightness of the external background.
Processing Code:
// IMA NYU Shanghai // Interaction Lab // For receiving multiple values from Arduino to Processing /* * Based on the readStringUntil() example by Tom Igoe * https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html */ import processing.serial.*; String myString = null; //string will take the Arudino values , but according to ASICC code Serial myPort; int NUM_OF_VALUES = 1; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ int[] sensorValues; /** this array stores values from Arduino **/ PImage photo; void setup() { background(0); size(1500, 1500); photo = loadImage("HELP.png"); setupSerial(); } void draw() { updateSerial(); //printArray(sensorValues); // use the values like this! // sensorValues[0] tint(map (sensorValues[0], 0, 1023, 0, 255)); image(photo, 0, 0); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[2], 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]); } } } } }
Arduino Code:
// IMA NYU Shanghai // Interaction Lab // For sending multiple values from Arduino to Processing void setup() { Serial.begin(9600); } void loop() { int sensor = analogRead(A0); // keep this format Serial.print(sensor); 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); }
Leave a Reply