In this week’s recitation, we were told to use Arduino and processing to manipulate the media attributes using the physical controller in Arduino. I decided to use an image to be manipulated. At first, i was using the position of the x and y of the mouse to change the size of the image, but i decided that it does not fit what i was going to do. I want to use two potentiometers to control two different things in the image. One potentiometer to control the length of the snow on the bottom of the image, the other one is for the snow that falls. I made the snow with for loops that we learned in class, but i made some modifications to make it seem smaller and use the value from the potentiometer to control the frequency of the for loop.
The code is attached below:
Arduino
void setup() {
Serial.begin(9600);
}
void loop() {
int sensor1 = analogRead(A0);
int sensor2 = analogRead(A1);
/// int sensor3 = analogRead(A2);
// map(sensor1, 0, 1023, 0, 255);
// keep this format
Serial.print(sensor1);
Serial.print(“,”); // put comma between sensor values
Serial.print(sensor2);
// Serial.print(“,”);
// Serial.print(sensor3);
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
import processing.serial.*; PImage photo; 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 **/ //float psensorValue0; //float psensorValue1; void setup() { size(800, 800); photo = loadImage("image.jpeg"); setupSerial(); } void draw() { background(#74CFD8); updateSerial(); printArray(sensorValues); float m = map(sensorValues[0], 0, 1023, 0, 100); float n = map(sensorValues[1], 0, 1023, 0, height); //left image(photo, 0, 0, width, height); for (int i=0; i<m; i++) { int size = int( random(1, 15) ); int x = int( random(photo.width) ); int y = int( random(photo.height) ); // get the pixel color color c = photo.get(x, y); // draw a circle with the color fill(#FFFFFF); noStroke(); ellipse(x, y, size, size); rectMode(CENTER); rect(400, 800, 800, n); // filter(BLUR, m); }} void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[ 3 ], 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[2]; } 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]); } } } } }