Finals Update #2: The coding is DONE!!!

HORRAY! The coding side of my project is finished! I have to give thanks where it’s deserved: Mimi, Tom, Mathura, Mithru thank you all so much for your help and support.

Here is the final-ish p5.js code (it will include more songs)
let playbutton, stopbutton, nextbutton, backbutton;
let slider;
let fader = 0;
let songBFileNames = ['SpiritInTheSky', 'RunThroughTheJungle', 'HesSoFine','UnderPressure', 'YouNeedLove', 'AmericanGirl', 'SweetLittleSixteen','MaryJanesLastDance', 'BrownSugar', 'LadyMadonna','DontStopTilYouGetEnough', 'TwoPrinces', 'Mmmbop', 'PumpItUp','ObLaDiObLaDa', 'IWontBackDown', 'Taurus', 'TheAirThatIBreathe','PictureBook','TomorrowNeverKnows', 'LittleMermaid'];
let songAFileNames = ['BabyDidABadBadThing', 'TheOldManDownTheRoad', 'MySweetLord','IceIceBaby', 'WholeLottaLove', 'LastNight', 'SurfinUSA','DaniCalifornia', 'WalkAndTalkIt', 'WhatIGot','SteppinOut', 'SemiCharmedLife', 'Unpretty', 'GetOnYourBoots','WhyDontYouGetAJob', 'StayWithMe', 'StairwayToHeaven', 'Creep','SaladDays','LetForeverBe', 'Nude'];
let songAs = [];
let songBs = [];
let albumAs = [];
let albumBs = [];
let currentIndex = 0;
let serial;
let portName = '/dev/cu.usbmodem1421';
let hasStarted = false;
let isPlaying = false;
let volumeA = 1;
let volumeB = 1;
function preload() {
for (let i = 0; i < songAFileNames.length; i++) { songAs[i] = loadSound("assets/sounds/" + songAFileNames[i] + ".mp3"); songBs[i] = loadSound("assets/sounds/" + songBFileNames[i] + ".mp3"); albumAs[i] = loadImage("assets/images/" + songAFileNames[i] + ".jpg"); albumBs[i] = loadImage("assets/images/" + songBFileNames[i] + ".jpg"); } } function setup() { createCanvas(400, 400); background(255); serial = new p5.SerialPort('192.168.1.151'); // make an instance of the serial library serial.open(portName); // open port serial.on('connected', function(){ console.log("CONNECTED"); background(215); }); serial.on('data', gotData); // declare serial data callback function } function draw() { //checkSliderAndUpdateSound(); if(hasStarted) checkFaderAndUpdateSound(); } function checkFaderAndUpdateSound() { console.log("checkFaderAndUpdateSound"); volumeA = map(fader, 0, 1023, 0, 1); volumeB = map(fader, 0, 1023, 1, 0); console.log(volumeA); songAs[currentIndex].setVolume(volumeA); songBs[currentIndex].setVolume(volumeB); tint(255,255); image(albumBs[currentIndex], 55, 20); tint(255, fader/4); image(albumAs[currentIndex], 55, 20); } function playsound() { songAs[currentIndex].loop(); songBs[currentIndex].loop(); } function pausesound(){ songAs[currentIndex].pause(); songBs[currentIndex].pause(); } function stopsound() { songAs[currentIndex].stop(); songBs[currentIndex].stop(); } function nextsetsound() { stopsound(); currentIndex++; if (currentIndex > songAs.length - 1) {
currentIndex = 0;
}
playsound();
}
function lastsetsound() {
stopsound();
currentIndex--;
if (currentIndex < 0) { currentIndex = songAs.length - 1; } playsound(); } function gotData() { let arduinoInput = serial.readLine(); // read an ASCII-encoded string if(arduinoInput.length > 0) {
if (arduinoInput === 'P') {
if(isPlaying) {
pausesound();
isPlaying = false;
} else {
playsound();
isPlaying = true;
} hasStarted = true;
}
else if (arduinoInput === 'B') {
lastsetsound();
}
else if (arduinoInput === 'N') {
nextsetsound();
}
else {
if(arduinoInput == NaN) arduinoInput = 0;
fader = arduinoInput;
console.log(fader);
}
}
}

And the Arduino code:
const int playButton = 2;
const int backButton = 3;
const int nextButton = 4;
int threshold = 2;
const int fader = A0;
int faderValue = 0;
int playButtonState = LOW;
int backButtonState = LOW;
int nextButtonState = LOW;
int prevFaderValue = 1;
int prevPlayButtonState;
int prevBackButtonState;
int prevNextButtonState;
void setup() {
pinMode(playButton, INPUT);
pinMode(backButton, INPUT);
pinMode(nextButton, INPUT);
Serial.begin(9600);
}
void loop() {
faderValue = analogRead(fader);
backButtonState = digitalRead(backButton);
playButtonState = digitalRead(playButton);
nextButtonState = digitalRead(nextButton);
if (playButtonState != prevPlayButtonState && playButtonState == HIGH) {
Serial.println('P');
}
if (backButtonState != prevBackButtonState && backButtonState == HIGH) {
Serial.println('B');
}
if (nextButtonState != prevNextButtonState && nextButtonState == HIGH) {
Serial.println('N');
}
if (abs(faderValue - prevFaderValue) > threshold) {
Serial.println(faderValue);
prevFaderValue = faderValue;
}
prevPlayButtonState = playButtonState;
prevBackButtonState = backButtonState;
prevNextButtonState = nextButtonState;
}

Final proto from Marco Wylie on Vimeo.

Things I need to fix on the coding side:

  1. When streaming the p5.js sketch to the iphone, you have to press on the iphone’s screen in order to start the sketch, even after you press the physical Play button.
  2. When you start the sketch the album cover from the other side of the slider starts on the opposite side and then suddenly switches to the correct one and acts normally when you move the fader.

Leave a Reply

Your email address will not be published. Required fields are marked *