Week 10 – Musical Instrument

Concept and Implementation

For this assignment, we wanted to make a musical instrument that was controlled by a user’s motion. So, we came up with the idea of changing the output tone based on the input from a flex sensor. For this, we attached a flex sensor to a glove so that when a user wears the glove and bends their hand, the input changes, and depending on how much the user bends their hand, the tone changes. To implement this, we saved a number of notes in an array. The input from the flex sensor is mapped to values between 0 and the size of the array so that for different measurements of the ‘bend’ of the hand, a different number is obtained after mapping and a note is picked from the array at that number. Moreover, we used a switch to play another tone, so that when the switch is turned on, a different tone made up of a set of notes is played. When the switch is turned off, the user can control the output melody using their hand. We played with different durations and frequencies until we found the one we liked.

Schematic

Video

Code

#include "pitches.h"

const int Buzzer1=3;
const int flexPin = A0;
int value; 

//array of notes
int melody[] = {
  NOTE_E5, NOTE_D5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_E5, NOTE_E5,
  NOTE_D5, NOTE_D5, NOTE_D5, NOTE_E5, NOTE_G5, NOTE_G4, NOTE_E5,
  NOTE_D5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
  NOTE_D5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_C5
};

void setup()
{
  Serial.begin(9600);
  pinMode(Buzzer1, OUTPUT);
  pinMode(A2, INPUT);
}

void loop()
{
  int button = digitalRead(A2);
  //when button is pressed, play a predefined tone (happy brithday music)
  if (button == HIGH)
  {
    Serial.print("SWITCH ON\n");
    //tone(Buzzer1, 200, 1200);
    // play a melody using tone()
  tone(Buzzer1, NOTE_C4, 250); delay(250);
  tone(Buzzer1, NOTE_C4, 250); delay(250);
  tone(Buzzer1, NOTE_D4, 500); delay(500);
  tone(Buzzer1, NOTE_C4, 500); delay(500);
  tone(Buzzer1, NOTE_F4, 500); delay(500);
  tone(Buzzer1, NOTE_E4, 1000); delay(1000);
 
  tone(Buzzer1, NOTE_C4, 250); delay(250);
  tone(Buzzer1, NOTE_C4, 250); delay(250);
  tone(Buzzer1, NOTE_D4, 500); delay(500);
  tone(Buzzer1, NOTE_C4, 500); delay(500);
  tone(Buzzer1, NOTE_G4, 500); delay(500);
  tone(Buzzer1, NOTE_F4, 1000); delay(1000);
  }
  //if button is not pressed, read value from flex sensor
  //map the value to a number that is within 0 and the max index of array melody
  //use this mapped value as an index and play the note from that particular index 
  else if (button == LOW)
  {
    Serial.print("OFF\n");
  
  value= analogRead(flexPin);
  // Serial.print("\nvalue is: ");
  // Serial.print(value);
  int size = sizeof(melody)/sizeof(melody[0]);
  value = map(value, 850, 1050, 0, size);
  
  tone(Buzzer1,melody[value],1200);
  //Serial.print("\n Note is: " );
  //Serial.print(melody[value]);
  delay(300);
  }
}

Future Improvements

For later, we can improve by first attaching the flex sensor inside the glove more securely so that the instrument produces more consistent sounds. Also, we can add an additional buzzer with the switch to play a sound continuously in the back while the user uses the flex sensor to add more sounds on top of it.

Groupmate: Mohid Raza

Leave a Reply