Final Project: Music Mimicry

So, I changed my mind about which idea to pursue for my final project. I decided to use the instrument Gopika and I made a few weeks ago, and have users mimic a snippet of a song that was played by using the instrument.

Unfortunately I wasn’t able to complete user testing yet, however I have someone lined up after class, and I will update this post with their feedback.

The professor suggested that I use Sforzando and Processing to play the songs, so that they sound better than the buzzer on an Arduino board, and so that is what I will be doing this class.

However, I have a working version that uses the Piezo buzzer on the Arduino, which plays a section of Ode to Joy, which the user than then mimic by playing the instrument themselves.

#define echoPinC 12
#define trigPinC 11
# include "pitches.h"

String notes6[7] = {"C4", "D4", "E4", "F4", "G4", "A4", "B4"};

int notesLow[7] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4};

const int buttonPin = 2;
const int buzzer = 4;
int songLength = 17;
char notes[] = "eefggfedccdeedd ";
int beats[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
int tempo = 288;

long durationC;
int distanceC;

void setup() {

  pinMode(trigPinC, OUTPUT);
  pinMode(echoPinC, INPUT);
  pinMode(buttonPin, INPUT);
  pinMode(buzzer, OUTPUT);

  digitalWrite(buttonPin, LOW);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(trigPinC, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinC, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinC, LOW);
  durationC = pulseIn(echoPinC, HIGH);
  distanceC = durationC * 0.034 / 2; 

  Serial.print("DistanceC: ");
  Serial.print(distanceC);
  Serial.print(" cm");
  Serial.print("mapped: ");


  digitalRead(buttonPin);
  if(buttonPin == LOW){
    Serial.println("Button Not Pushed");
  }
  

  int constrainedInput=constrain(distanceC, 0, 70);
  int mappedNoteC = map(constrainedInput, 1, 70, 0, 7);
  
  if(distanceC > 70){
  noTone(4);
  }else{
    tone(4, notesLow[mappedNoteC]);
 }
  
  Serial.println(notes6[mappedNoteC]);

  int i, duration;

  if (digitalRead(buttonPin) == HIGH){
    for (i = 0; i < songLength; i++){
      duration = beats[i]*tempo;

      if (notes[i] == ' '){
        delay(duration);
      }else{
        tone(buzzer, frequency(notes[i]), duration);
        delay(duration);
      }
      delay(tempo/10);
    }
  }
}

int frequency(char note){

  int i;

  const int numNotes = 8;

  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};

  for (i = 0; i < numNotes; i++){
    if (names[i] == note){
      return(frequencies[i]);
    }
  }
  return(0);
}

 

Leave a Reply