Exercise 1
I had trouble when I needed to use lots of if functions because it was confusing to place different statements in the right place.
import processing.sound.*; // declare three SoundFile objects SoundFile A; SoundFile B; color c; color C; boolean a; boolean l; void setup() { size(640, 480); // create the objects and load sounds into them A = new SoundFile(this, "monsterwobbleloop.wav"); B = new SoundFile(this, "plunkysynthloop.wav"); c=255; C=255; } void draw() { background(0); fill(c); rectMode(CENTER); rect(width/2-100, height/2, 100, 100); fill(C); rect(width/2+100, height/2, 100, 100); if (keyPressed==true) { if (key=='a') { if (a==true) { A.loop(); c=100; a=false; } } } else { a=true; c=255; } if (keyPressed==true) { if (key=='l') { if (l==true) { B.loop(); C=100; l=false; } } } else { C=255; l=true; } } void keyReleased() { A.stop(); B.stop(); }
Exercise 2
The only problem I encounter was that I tried to remap the prcoessing value in the processing code, but it didn’t work. So I moved the code in the Arduino code. Additionally, I found that my vibration rythm wasn’t that corresponsive to the music’s beats.
Arduino
/* This is the array of values storing the data from Processing. */ int processing_values[NUM_OF_VALUES_FROM_PROCESSING]; void setup() { Serial.begin(9600); pinMode(ZD, OUTPUT); } void loop() { getSerialData(); if(processing_values[0]>0.6){ int vibration = map(processing_values[0],0,1,0,1023); analogWrite(ZD, vibration); }else{ analogWrite(ZD,0); } // 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 9 case '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 array case ',': 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 data case '\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
import processing.serial.*; int NUM_OF_VALUES_FROM_PROCESSING = 1; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ float processing_values[] = new float[NUM_OF_VALUES_FROM_PROCESSING]; /** this array stores values you might want to send to Arduino **/ Serial myPort; String myString; import processing.sound.*; //AudioIn microphone; Amplitude analysis; SoundFile file; void setup() { size(500, 500); background(0); setupSerial(); // create the AudioIn object and select the mic as the input stream file = new SoundFile(this, "Lost On The Freeway.mp3"); // start the mic input without routing it to the speakers file.play(); // create the Amplitude analysis object analysis = new Amplitude(this); // use the microphone as the input for the analysis analysis.input(file); } void draw() { background(0); println(analysis.analyze()); 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); processing_values[0] = volume; // send the values to Arduino. sendSerialData(); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[0], 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; } void sendSerialData() { String data = ""; for (int i=0; i<processing_values.length; i++) { data += processing_values[i]; //if i is less than the index number of the last element in the values array if (i < processing_values.length-1) { data += ","; // add splitter character "," between each values element } //if it is the last element in the values array else { data += "\n"; // add the end of data character linefeed "\n" } } //write to Arduino myPort.write(data); print(data); // this prints to the console the values going to arduino }
Homework
import processing.sound.*; // declare an AudioIn object AudioIn microphone; // declare an Amplitude analysis object to detect the volume of sounds Amplitude analysis; float diameter; void setup() { size(640, 480); // create the AudioIn object and select the mic as the input stream microphone = new AudioIn(this, 0); // start the mic input without routing it to the speakers microphone.start(); // create the Amplitude analysis object analysis = new Amplitude(this); // use the microphone as the input for the analysis analysis.input(microphone); } void draw() { println(analysis.analyze()); background(255); // analyze the audio for its volume level float volume = analysis.analyze(); // map the volume value to a useful scale diameter = map(volume, 0, 1, 30, width); // draw a circle based on the microphone amplitude (volume) //circle(width/2, height/2, diameter); puppet(width/2,height/2,200); } void puppet(float x, float y, float size){ fill(0); circle(x,y,size); triangle(x-size*0.4,y-size*0.2, x, y-size*0.3,x-size*0.3,y-size*0.7); triangle(x+size*0.4,y-size*0.2, x, y-size*0.3,x+size*0.3,y-size*0.7); line(x-size*0.4,y,x-size*0.7,y-size*0.1); line(x-size*0.4,y,x-size*0.7,y+size*0.1); line(x+size*0.4,y,x+size*0.7,y-size*0.1); line(x+size*0.4,y,x+size*0.7,y+size*0.1); fill(255); circle(x-size*0.2, y-size*0.1,size*0.1); circle(x+size*0.2, y-size*0.1,size*0.1); circle(x,y,size*0.1); fill(255,0,0); circle(x,y+size*0.2,diameter); }