20 March 2022

Project: NIME, Group 10

 Project: NIME, Group 10

Michael Brucato, Victor Aldana




#include <Servo.h>

#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>

#define POTENTIOMETER_PIN A1

Servo myServo;
float song [] = {293.66, 293.66, 587.33, 440, 415.30, 392, 349.23, 293.66, 349.23, 392};    //The song I chose to make in an array, Megalovania's intro
float lengths[] = {100,100,140,240,200,190,210,100,100,100};    //Spacing between notes
int servoLengths[] = {110, 10, 110, 10, 180, 30, 110, 10, 110, 10, 110, 10, 110, 10};    //The song I chose to make in an array for the servo which is the intro to Kirby 64's Factory inspection

int numNotes = sizeof(song)/sizeof(float);
int servoNotes = sizeof(servoLengths)/sizeof(float);    //These ints define the array size

float adjustedlength = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(1000);
  CircuitPlayground.begin();
  myServo.attach(10);
  pinMode(13, OUTPUT);
  pinMode(10, INPUT_PULLDOWN);
  pinMode(4, INPUT_PULLDOWN);
  pinMode(5, INPUT_PULLDOWN);
}

void loop() {

  // put your main code here, to run repeatedly:
  if(digitalRead(4)){    //Press the right button to start the servo song

    for(int i = 0; i < servoNotes; i++){
      myServo.write(servoLengths[i]);
      Serial.write(i);
      if(i > 3 && i < 6){
        delay(480);                 // These if's change the tempo to match the song being played
      }
      else if (i > 6  && i < 10){
        delay(330);
      }
      else{
        delay(270);
      }

    }

  }
  if(digitalRead(5)){    //Press the left button to start the digital song

    for(int i = 0; i < numNotes; i++){
      float potentiometerValue = analogRead(A1);        //defines potentiometer value
      potentiometerValue = mapfloat(potentiometerValue, 0, 1023, 1,5);        //calls to an external function to map float digits for the potentiometer
      adjustedlength = (lengths[i]) * potentiometerValue;       //Adjusts the length of each note based on potentiometer value
      CircuitPlayground.playTone(song[i], adjustedlength);        //Plays the digital song
    }

  }

}

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}


                So for our NIME project me and my teammate created a music box from a shoebox and tape. The hardware shown off shows no breadboard connecting the modules together, so we did the next best thing of using a bunch of alligator clips together so we can connect the potentiometer and Servo to the CPB. With the press of the right button on the CPB, you will end up playing the intro the Megalovania via the digital speaker on the CPB and with the press of the other button on the CPB you end up playing the intro to Factory Inspection from Kirby Planet Robobot on the servo. The code itself features float mapping via an external function from the loop command that allows the Potentiometer to accurately affect the speed of the song being played. This is to avoid the instant speed changes that would appear if it was a simple int mapping, but with the float mapping it slows down more accurately to the value on the potentiometer. Feedback is given to the person using it via the signifiers of the potentiometer and it’s relation with the speed of the music being played. For those of you reading this, is there any way we could have improved the extent of what the potentiometer could have changed?


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.