AUDIO
The 3 different tracks from last week all had a lot of pauses in between so sometimes it was hard to tell if they were playing even when they were activated. To fix this, I tried to populate the soundtracks so there won’t be long pauses. I also added ghost sound effects from the game Luigi’s Mansion on the Beats track.
Music sheet reference: https://freshsheetmusic.com/vic-mizzy-the-addams-family-theme-84757/
Ghost sounds source: https://www.sounds-resource.com/gamecube/luigismansion/sound/1810/
Bass
Chorus
Beat & SE:
I also started testing the audio in p5.js with keyboard keys before connecting to Arduino. We had to make sure all 3 tracks are in sync whenever they are playing, so we decided to have all of the soundtracks start playing continuously once one of the 3 sensors are activated. The activated sensor will then trigger and turn on the volume of the corresponding track.
https://editor.p5js.org/zixin.cheng/sketches/I1Y1mt1xLM
CONNECTING P5 TO SERIAL INPUT
We tried testing out the serial input in p5 starting with just one sensor first. We were having some difficulties trying to get the LED as well as the audio working on p5 both at the same time, but after reconnecting a few times we were able to get it to work.
const int potPin = A0;
int potVal = 0;
void setup() {
Serial.begin(9600);
pinMode(4, OUTPUT);
}
void loop() {
//reading sensor value
potVal = analogRead(potPin);
Serial.println(potVal);
delay(10);
//turning on LED
if (potVal > 50) {
digitalWrite(4, HIGH);
} else{
digitalWrite(4, LOW);
}
}
let serial;
let latestData = "waiting for data";
function preload() {
bass = loadSound("bass.mp3", loaded);
chorus = loadSound("chorus.mp3", loaded);
beat = loadSound("beat.mp3", loaded);
}
function setup() {
createCanvas(400, 400);
serial = new p5.SerialPort();
serial.open('/dev/tty.usbmodem21401');
serial.on('data', gotData);
}
function gotData() {
let currentString = serial.readLine(); // store the data in a variable
trim(currentString); // get rid of whitespace
if (!currentString) return; // if there's nothing in there, ignore it
console.log(currentString); // print it out
latestData = currentString; // save it to the global variable
}
//don't play audio when it is finished loading
function loaded() {
bass.stop();
chorus.stop();
beat.stop();
}
//if any of the sensors are activated, start playing all 3 sound tracks, but mute all
function sensorActivated(){
if (latestData > 10){
if (!bass.isPlaying()) { //only play when the audio is not playing
bass.loop();
chorus.loop();
beat.loop();
bass.setVolume(0);
chorus.setVolume(0);
beat.setVolume(0);
}
}
}
function draw() {
background(220);
let sensor1 = latestData;
text(sensor1,10,10);
if (sensor1 > 50) { //only when it is continuesly activated
sensorActivated();
bass.setVolume(1); //volume turned up
text("bass",width/2,height/2-20);
} else {
bass.setVolume(0); //volume muted, but music still playing in background
}
}
We continued to add another sensor. We had to store the different sensor values into an array in p5 then split each value from each line to make the individual values that will activate the audio.