For the first part of this recitation, we were tasked with creating a sketch in processing that could be controlled by an Arduino. For this, I loaded an image into processing and then set the tint of the image to be variable. I then created a simple circuit using one potentiometer and connected it to my processing code using the code provided. When the potentiometer was turned, the image’s tint would change from slightly blue to completely dark based on the input received. In practice this looked like this:
The code used was as follows:
import processing.serial.*; int NUM_OF_VALUES = 1; int sensorValues[]; String myString = null; Serial myPort; void setup () { size(450, 400); setupSerial(); } void draw() { getSerialData(); printArray(sensorValues); PImage photo; photo = loadImage("my cat.jpg"); image(photo, 0, 0); tint(0,0,sensorValues[0],255); image(photo, 0, 0); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[1], 9600); myPort.clear(); myString = myPort.readStringUntil( 10 ); myString = null; sensorValues = new int[NUM_OF_VALUES]; } void getSerialData() { 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]); } } } } }
For the second part of this recitation, we were tasked with using an audio element in processing to manipulate a servo motor. For this, I decided to use the microphone input to change the movement of a servo motor. For this, I used a provided code in processing and then slightly modified it to transfer information to arduino. Originally, the code was designed to draw a circle on the screen, based on the noise level recorded. I kept this element but took the data recorded in processing and sent it to an Arduino code created to run a servo motor. When working, the project looked like this:
The Arduino code looked as such:
#define NUM_OF_VALUES 1 int tempValue = 0; int valueIndex = 0; int values[NUM_OF_VALUES]; #include <Servo.h> Servo myservo; int pos = 0; void setup() { Serial.begin(9600); myservo.attach(9); } void loop() { getSerialData(); myservo.write(values[0]); delay(15); } void getSerialData() { while (Serial.available()) { char c = Serial.read(); switch (c) { case '0'...'9': tempValue = tempValue * 10 + c - '0'; break; case ',': values[valueIndex] = tempValue; tempValue = 0; valueIndex++; break; case '\n': values[valueIndex] = tempValue; tempValue = 0; valueIndex = 0; break; } } }
And the processing code looked as such:
import processing.serial.*; int NUM_OF_VALUES = 1; int values[] = new int[NUM_OF_VALUES]; Serial myPort; String myString; import processing.sound.*; AudioIn microphone; Amplitude analysis; void setup() { size(500, 500); background(0); microphone = new AudioIn(this, 0); microphone.start(); analysis = new Amplitude(this); analysis.input(microphone); setupSerial(); } void draw() { background(0); float volume = analysis.analyze(); int vol= int(map(volume, 0, 1, 0, 180)); values[0] = vol; float diameter = map(volume, 0, 1, 0, width); circle(width/2, height/2, diameter); print(vol); println(); sendSerialData(); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[1], 9600); myPort.clear(); myString = myPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII myString = null; } void sendSerialData() { String data = ""; for (int i=0; i<values.length; i++) { data += values[i]; if (i < values.length-1) { data += ","; } else { data += "\n"; } } myPort.write(data); }
In this recitation, I utilized the technology of circuits and computers to create a combined project. For me, I used a potentiometer to control pixels on a screen, and then a microphone to control the movement of a servo.