Arduino Code
// For sending multiple values from Arduino to Processing void setup() { Serial.begin(9600); } void loop() { int sensor1 = analogRead(A0); int theMappedValue = map(sensor1, 0, 1023, 0, 255); // keep this format Serial.write(theMappedValue); //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); }
Processing Code
// IMA NYU Shanghai // Interaction Lab // This code receives one value from Arduino to Processing import processing.serial.*; Serial myPort; int valueFromArduino; PImage photo; float opacity; void setup() { size(500, 413); background(0); photo = loadImage ("bob.jpg"); // this prints out the list of all available serial ports on your computer. myPort = new Serial(this, Serial.list()[ 15 ], 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 read the value from the Arduino //float opacity = map (valueFromArduino,0,1023,0,255); //we need a float because we want processing to catch all the numbers from arduino while ( myPort.available() > 0) { valueFromArduino = myPort.read(); } printArray(Serial.list()); println(valueFromArduino); //tint (0,0,255, opacity); image(photo, 0,0); if (valueFromArduino <150){ tint(255,0,0); } else if(valueFromArduino > 200) { tint (0,255,0); } //This prints out the values from Arduino }
During this week’s recitation, I used a potentiometer to tint the image from red to green. The main challenge I faced was inserting an “if else” statement in processing. At first, when I did not have the “if else” statement, the Spongebob image would tint red but would not change after that. However, once I added the “if else” statement, I told processing when the colors should change, so then the image tinted colors.
In this week’s reading, the Suicide Box relates well to what we did in this task. Whenever the camera sensed a vertical motion, then it would record people falling and track information. This reminds me of the “if else” statement that we used in this task to control the color tint. In both projects, we are giving the computer parameters to know when they should perform tasks.