Exercise 1:
Processing Code:
import processing.sound.*; // declare a SoundFile object SoundFile sound1; SoundFile sound2; SoundFile sound3; SoundFile sound4; boolean step1= false; boolean step2= false; void setup() { size(1200, 1000); // create the object and load a file into it sound1 = new SoundFile(this, "crashbreak2rideout.wav"); sound3 = new SoundFile(this, "kickcrash.wav"); background(255); } void draw() { if (keyPressed) { if (key == 'd' || key == 'D') { sound3.loop(); noStroke(); fill(#500B6C); rect(500, 300, 100, 100); fill(255); } } } void keyPressed(){ if (key == 'b' || key == 'B') { step1=!step1; } if (step1==true) { sound1.loop(); fill(#500B6C); noStroke(); rect(700, 300, 100, 100); } else { sound1.stop(); fill(255); noStroke(); circle(400, 500, 62); } }
Video:IMG_1326
Exercise2:
Arduino Code:
// IMA NYU Shanghai // Interaction Lab /** This example is to send multiple values from Processing to Arduino. You can find the Processing example file in the same folder which works with this Arduino file. **/ #define ZD 3 #define NUM_OF_VALUES_FROM_PROCESSING 1 /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ /** DO NOT REMOVE THESE **/ int tempValue = 0; int valueIndex = 0; /* This is the array of values storing the data from Processing. */ int processing_values[NUM_OF_VALUES_FROM_PROCESSING]; #include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = A0; // analog pin used to connect the potentiometer int val; void setup() { Serial.begin(9600); pinMode(ZD,OUTPUT); } void loop() { getSerialData(); analogWrite(ZD,200); delay(5000); analogWrite(ZD,0); delay(1000); analogWrite(ZD,100); delay(5000); analogWrite(ZD,0); delay(1000); // add your code here using elements in the values array //this is an example connecting a buzzer to pin 8 /* if (processing_values[0] == 1) { //turn on an LED when the mouse is pressed digitalWrite(13, HIGH); // map values from mouseX to frequency from (0 – 500 pixels) //to the output pitch range (120 – 1500Hz) int f = map(processing_values[1], 0, 500, 120, 1500); // map values from mouseY to frequency from (0 – 500 pixels) //to the output duration range (10 – 2000 milliseconds) int d = map(processing_values[2], 0, 500, 10, 2000); // play the pitch: tone(8, processing_values[1], processing_values[2]); delay(1); // delay in between reads for stability } else { digitalWrite(13, LOW); } */ //end of example } //receive serial data from Processing void getSerialData() { while (Serial.available()) { char c = Serial.read(); //switch – case checks the value of the variable in the switch function //in this case, the char c, then runs one of the cases that fit the value of the variable //for more information, visit the reference page: https://www.arduino.cc/en/Reference/SwitchCase switch (c) { //if the char c from Processing is a number between 0 and 9case ‘0’…’9′: //save the value of char c to tempValue //but simultaneously rearrange the existing values saved in tempValue //for the digits received through char c to remain coherent //if this does not make sense and would like to know more, send an email to me!tempValue = tempValue * 10 + c – ‘0’; break; //if the char c from Processing is a comma //indicating that the following values of char c is for the next element in the values arraycase ‘,’: // processing_values[valueIndex] = tempValue; //reset tempValue value tempValue = 0; //increment valuesIndex by 1 valueIndex++; break; //if the char c from Processing is character ‘n’ //which signals that it is the end of datacase ‘\n’: //save the tempValue //this will b the last element in the values array // processing_values[valueIndex] = tempValue; //reset tempValue and valueIndex values //to clear out the values array for the next round of readings from Processing tempValue = 0; valueIndex = 0; break; } } }
Processing code:
import processing.serial.*; import processing.sound.*; int NUM_OF_VALUES_FROM_ARDUINO = 1; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ int sensorValues[]; /** this array stores values from Arduino **/ String myString = null; Serial myPort; // declare an AudioIn object SoundFile sound; // declare an Amplitude analysis object to detect the volume of sounds Amplitude analysis; void setup() { size(500, 500); sound= new SoundFile(this, "crashbreak2rideout.wav"); sound.play(); // create the Amplitude analysis object // use the microphone as the input for the analysis analysis = new Amplitude(this); analysis.input(sound); setupSerial(); } void draw() { background(0); println(analysis.analyze()); background(0); float volume = analysis.analyze(); // map the volume value to a useful scale float diameter = map(volume, 0, 1, 0, width); // draw a circle based on the microphone amplitude (volume) circle(width/2, height/2, diameter); getSerialData(); sensorValues[0]=int(map(analysis.analyze(),0,1,0,200)); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list( )[4], 9600); myPort.clear(); myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII myString = null; sensorValues = new int[NUM_OF_VALUES_FROM_ARDUINO]; } 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_FROM_ARDUINO) { for (int i=0; i<serialInArray.length; i++) { sensorValues[i] = int(serialInArray[i]); } } } } }
Video:
IMG_1336
Homework:
video:IMG_1332
Arduino Code
// IMA NYU Shanghai // Interaction Lab /** This example is to send multiple values from Processing to Arduino. You can find the Processing example file in the same folder which works with this Arduino file. **/ #define ZD 3 #define NUM_OF_VALUES_FROM_PROCESSING 1 /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ /** DO NOT REMOVE THESE **/ int tempValue = 0; int valueIndex = 0; /* This is the array of values storing the data from Processing. */ int processing_values[NUM_OF_VALUES_FROM_PROCESSING]; #include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = A0; // analog pin used to connect the potentiometer int val; void setup() { Serial.begin(9600); pinMode(ZD,OUTPUT); } void loop() { getSerialData(); analogWrite(ZD,200); delay(5000); analogWrite(ZD,0); delay(1000); analogWrite(ZD,100); delay(5000); analogWrite(ZD,0); delay(1000); // add your code here using elements in the values array //this is an example connecting a buzzer to pin 8 /* if (processing_values[0] == 1) { //turn on an LED when the mouse is pressed digitalWrite(13, HIGH); // map values from mouseX to frequency from (0 – 500 pixels) //to the output pitch range (120 – 1500Hz) int f = map(processing_values[1], 0, 500, 120, 1500); // map values from mouseY to frequency from (0 – 500 pixels) //to the output duration range (10 – 2000 milliseconds) int d = map(processing_values[2], 0, 500, 10, 2000); // play the pitch: tone(8, processing_values[1], processing_values[2]); delay(1); // delay in between reads for stability } else { digitalWrite(13, LOW); } */ //end of example } //receive serial data from Processing void getSerialData() { while (Serial.available()) { char c = Serial.read(); //switch – case checks the value of the variable in the switch function //in this case, the char c, then runs one of the cases that fit the value of the variable //for more information, visit the reference page: https://www.arduino.cc/en/Reference/SwitchCase switch (c) { //if the char c from Processing is a number between 0 and 9case ‘0’…’9′: //save the value of char c to tempValue //but simultaneously rearrange the existing values saved in tempValue //for the digits received through char c to remain coherent //if this does not make sense and would like to know more, send an email to me!tempValue = tempValue * 10 + c – ‘0’; break; //if the char c from Processing is a comma //indicating that the following values of char c is for the next element in the values arraycase ‘,’: // processing_values[valueIndex] = tempValue; //reset tempValue value tempValue = 0; //increment valuesIndex by 1 valueIndex++; break; //if the char c from Processing is character ‘n’ //which signals that it is the end of datacase ‘\n’: //save the tempValue //this will b the last element in the values array // processing_values[valueIndex] = tempValue; //reset tempValue and valueIndex values //to clear out the values array for the next round of readings from Processing tempValue = 0; valueIndex = 0; break; } } }
Processing Code
import processing.sound.*; AudioIn microphone; Amplitude analysis; float r; void setup() { size(600, 400); microphone = new AudioIn(this, 0); microphone.start(); analysis = new Amplitude(this); analysis.input(microphone); } void draw() { println(analysis.analyze()); background(#FFFFFC); float volume = analysis.analyze(); float r= map(volume, 0, 1, 255, 0); float d=map(volume, 0, 1, 0, 100); fill(255, r, 64); ellipse(300, 200, 200, 200); fill(#FA0810); strokeWeight(5); line(290,260,300,260); fill(0); ellipse(280,180,5,5); ellipse(320,180,5,5); }