Week 10 – Slapping Music

Concept:

For this assignment of designing an unusual musical instrument, instead of making an instrument that can be actually played by people, we decided to make something that produces music on its own. Among all the electronic components we have, we think that the servo motor will be the perfect choice to show the mechanical movement of the music. And we decide to use the ultrasonic distance sensor as the main part that produces music. While the servo motor moves, the pad attached to it will move as well, leading to the changes in the distance measured by the ultrasonic sensor. According to different distances, the speaker plays sounds of different frequencies.

IMG_4401

Highlight of the code

For coding this self-playing musical instrument, we used the functions like map() that match the distance to the frequencies. We used the distance sensor code from the documentation example. While manipulating the servo motor’s position, the simple codes can produce surprising music. More over, we have the button that changes the frequency range. Whenever the button is pressed, the whole melody will be played at a higher frequency.

// assign the variables for distance and button 
#define trigPin 13
#define echoPin 12
#define buttonPin 2

// include library for Servo motor 
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

// assign the first position
  // variable to store the servo position
int pos = 0;


void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);  //distance
  pinMode(echoPin, INPUT);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  
  pinMode(buttonPin, INPUT); // switch
}

int frequency; // assign the frequency variable

void loop() {
  // from distance sensor example
  long duration, distance; 
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2);        // Added this line
  digitalWrite(trigPin, HIGH);
  //  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10);  // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;


  if (distance >= 200 || distance <= 0) {
    Serial.println("Out of range");
  } else {
    Serial.print(distance);
    Serial.println(" cm");
  }


// we contrain the distance to be from 0 to 50 and map the distance values to the frequency values 
// if the button is not pushed then the melody plays in low frequencies
// if the button is pushed, then the frequency  is higher 


  if (digitalRead(buttonPin) == LOW) {
    frequency = map(constrain(distance, 0, 50), 0, 50, 100, 1000);
  } else {
    frequency = map(constrain(distance, 0, 50), 0, 50, 500, 2000);
  }


  //code for SERVO position 
  myservo.write(90);  
  delay(50);
  myservo.write(45);
  delay(50);
  myservo.write(25);
  delay(50);
  myservo.write(70);
  delay(50);
  myservo.write(100);
  delay(50);
  myservo.write(60);
  delay(50);
  // waits 50ms for the servo to reach the position
 

// play the sound from pin 6

  tone(6, frequency);


}

Reflections and Improvement

As for improvements, the appearance of the instrument could definitely be improved. For example, the entire device could be enclosed into a box that’s called music box. That will make the project more interesting and appealing. Moreover, we could include the function that allows the user to change melodies as they want. They can, for instance, manipulate the movement of the arm and the hand with a potentiometer. This gives more interactivity to the project.

Leave a Reply