Music with Distance

Concept.

How would playing the piano with the feet at an arcade center look on a small scale? That was how I arrived at this mini-project. To make it easier for any user, I specifically picked notes (of Fur Elise) in ascending order of distance (cm).

Implementation.

The distance instrument works simply by moving an obstacle in front of an ultrasonic sensor at an interval of 1cm. This was implemented using if-else statements.

Tools Used.

The project features an ultrasonic sensor, a buzzer, jumper wires, a 10k ohm resistor, and a switch.

Challenges

Some challenges I encountered while undertaking this project involved difficulty in figuring out the specific notes to make up the music used. Once that was figured out, all the was left was to provide the best if-else condition statement to control the notes.

 

Code

/*Assignment

Author: Aaron Wajah
Concept: Making Fur Elise melody with arduino
Date: 11-04-2023

*/


// Define pins for ultrasonic sensor, switch, and buzzer.
const int trigPin = 10;
const int echoPin = 11;
const int switchPin = 2;
const int buzzerPin = 8;

// Define variables
long duration;
int distance;
int switchState = LOW;
int prevswitchState=0;

bool buttonPressed=false;

//defining notes for melody. 
const int notes[] = { 659, 622, 659, 622, 659, 494, 587, 523, 440};

const int quarterNote = 250;
// Define durations for each note (in quarter notes)
const int durations[] = { 4, 8, 8, 4, 8, 8, 4, 8, 8 };


void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Set pins as input/output
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(switchPin, INPUT_PULLUP); // Use internal pull-up resistor
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  // Read switch state
  switchState = digitalRead(switchPin);

  // Only take a measurement if switch is pressed
  if (switchState == HIGH ) {
    

    int duration =  quarterNote*1.2;

    // Send a pulse to trigger the sensor
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    // Measure the duration of the echo pulse
    duration = pulseIn(echoPin, HIGH);

    // Calculate distance in centimeters
    distance = duration / 58;

    // Print distance to serial monitor
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");


    if(distance==2){
      tone(buzzerPin, notes[0], duration);
      delay(duration * 1.3);
    }
    if(distance==3){
      tone(buzzerPin, notes[1], duration);
      delay(duration * 1.3);
    }
    if(distance==4){
      tone(buzzerPin, notes[2], duration);
      delay(duration * 1.3);
    }
    if(distance==5){
      tone(buzzerPin, notes[3], duration);
      delay(duration * 1.3);
    }
    if(distance==6){
      tone(buzzerPin, notes[4], duration);
      delay(duration * 1.3);
    }
    if(distance==7){
      tone(buzzerPin, notes[5], duration);
      delay(duration * 1.3);
    }
    if(distance==8){
      tone(buzzerPin, notes[6], duration);
      delay(duration * 1.3);
    }
    if(distance==9){
      tone(buzzerPin, notes[7], duration);
      delay(duration * 1.3);
    }
    if(distance==10){
      tone(buzzerPin, notes[8], duration);
      delay(duration * 1.3);
    }
    
    noTone(8);
  
  }
  
  // Wait before taking another measurement
  delay(500);
}

 

Leave a Reply