Hey everyone!
So, as some of you might’ve seen over the last couple of days, me and my group are working on a project, where sensor values from an Arduino need to control some sort of sound input. At this point, we’ve gotten processing to revieve the data and to play some sound, however, we’re unsure as to how exactly we’ll control the sound with these inputs; specifically, controlling the pitch with the plant value and the volume with the potentiometer.
Arduino code:
int potVal;
int plantVal;
void setup() {
Serial.begin(115200);
}
void loop() {
potVal = (analogRead(A2));
Serial.write(potVal);
plantVal = (analogRead(A5));
Serial.write(plantVal);
delay(100);
}
Processing code:
import beads.*; //import library for sound and controlling it
import processing.serial.*; //import library for communication
Serial myPort;
int potVal;
int plantVal;
int[] guitarPitches = {0, 5, 10, 15, 19, 24}; // hlf steps from low e, for each open string
int ctr = 0;
AudioContext ac;
Gain g;
void setup() {
//fullScreen();
size(500, 500);
myPort = new Serial(this, Serial.list()[0], 115200);
ac = new AudioContext();
g = new Gain(ac, 1, 0.5);
ac.out.addInput(g);
ac.start();
}
void draw() {
if (potVal != 0) {
ambientSound();
}
if (myPort.available() > 0) {
potVal = myPort.read();
plantVal = myPort.read();
println("Potentiometer value: ");
println(potVal);
println("Plant value: ");
println(plantVal);
println("_____________________________");
println();
}
}
void ambientSound() //if statement with any plant changes
{
int pitchIdx = guitarPitches[ctr];
ctr = (ctr + 1) % guitarPitches.length;
float pitchRatio = pow(2, pitchIdx/12.0);
String sourceFile = dataPath("ambient_single_note001.wav");
try {
SamplePlayer sp = new SamplePlayer(ac, new Sample(sourceFile));
sp.setRate(new Glide(ac, pitchRatio));
sp.setKillOnEnd(true);
g.addInput(sp);
sp.start();
}
catch (Exception e) {
println("Exception loading sample: " + sourceFile);
e.printStackTrace();
}
}
Hope someone can help!