Musical Instrument

Concept

In this assignment, we were to create musical instruments but in groups. I could not find someone to work with so i decided to work on my own. One other reason was that i wanted to also have time to explore and learn more. My musical instrument is a basic one which uses two external libraries that are adapted for the ultrasonic sensor and the piezo buzzer. The libraries are the NewPing.h and the ToneAC.h library. The NewPing.h library is adapted to work with all types of ultrasonic sensors and increases the accuracy of the distance calculated. The ToneAC library has a higher frequency range, twice the volume and higher quality than the tone library. By combining these two libraries my instrument improved a lot more and had better quality sounds.

#include <NewPing.h> // newping library for the ultrasonic sensor
#include <toneAC.h> // tine library for the piezo buzzer

//pins of the ultrasonic sensor
const int trig=12; 
const int echo=11;

NewPing sonar( trig, echo, 35 ); // creating a new class for the ultrasonic sensor

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600); // for debugging and printing the values stored
}



void loop() {
  // put your main code here, to run repeatedly:
  //calculate the distance to the obstacle
int f= sonar.ping_cm();
Serial.print("freq");
Serial.println(f);


//map the distance calculated to the notes c1 to e7
int frequency =map(f, 0, 35, 33, 4000);

//play the sound on the buzzer
toneAC(frequency, 10);

delay(200);
}

 

Leave a Reply