W13-Project3-Sound Balls
Final result
You may find a longer demo featuring a Beatles song here.
p5
Here is the p5 sketch code.
Arduino
const int fanPinW = 10; // connected to the base of the transistor const int fanPinG = 9; // connected to the base of the transistor const int fanPinO = 6; // connected to the base of the transistor const int fanPinY = 5; // connected to the base of the transistor void setup() { // set the transistor pin as output: pinMode(fanPinW, OUTPUT); pinMode(fanPinG, OUTPUT); pinMode(fanPinO, OUTPUT); pinMode(fanPinY, OUTPUT); Serial.begin(9600); } void loop() { if (Serial.available()) { String fromSerial = Serial.readStringUntil('\n'); // read until you see a \n //First Value int firstValueEnd = fromSerial.indexOf(','); // find the first comma and tell me how deep into the string it is String firstValueString = fromSerial.substring(0, firstValueEnd); // give me a new string that includes everything till the first comma int firstValue = firstValueString.toInt(); // give me the int interpretation of that string float outputValue1 = map(firstValue, 0, 500, 0, 255); analogWrite(fanPinW, outputValue1); //Second Value int secondValueEnd = fromSerial.indexOf(',', firstValueEnd + 1); // search for the second comma, start searching after the first one String secondValueString = fromSerial.substring(firstValueEnd + 1, secondValueEnd); //give me a new string with everything beween first and second comma int secondValue = secondValueString.toInt(); // give me the int interpretation of that string float outputValue2 = map(secondValue, 0, 500, 0, 255); analogWrite(fanPinG, outputValue2); //Third Value int thirdValueEnd = fromSerial.indexOf(',', secondValueEnd + 1); String thirdValueString = fromSerial.substring(secondValueEnd + 1, thirdValueEnd); int thirdValue = thirdValueString.toInt(); float outputValue3 = map(thirdValue, 0, 500, 0, 255); analogWrite(fanPinO, outputValue3); //Forth Value int forthValueEnd = fromSerial.indexOf(',', thirdValueEnd + 1); String forthValueString = fromSerial.substring(thirdValueEnd + 1, forthValueEnd); int forthValue = forthValueString.toInt(); float outputValue4 = map(forthValue, 0, 500, 0, 255); analogWrite(fanPinY, outputValue4); } }
Fabrication
Failed experiments
This is the best attempt of my original plan: