You are on page 1of 9

BALIUAG UNIVERSITY

COLLEGE OF ENVIRONMENTAL DESIGN AND


ENGINEERING

Arduino Ultrasonic Musical Instrument

Name: Daryl Angelo Lopez


Pouline Cunanan
Norbien Callos
Dianne Claudine Sagum
Mobail Omar Benito
Jonel Cinco Jr.
Christian Louise Cruz

Date Submitted:

____________________
Mr. Randy Mendoza
Professor
Introduction:
The objective here is to perform the musical instrument,
using ultrasonic sensor.

We will be using this sensor to measure distances.

It detects the distance of the closest object in front of the


sensor (from 3 cm up to 400 cm). It works by sending out a
burst of ultrasound and listening for the echo when it
bounces off of an object. It pings the obstacles with
ultrasound.

The Arduino board sends a short pulse to trigger the


detection, then listens for a pulse on the same pin using the
pulseIn() function. The duration of this second pulse is
equal to the time taken by the ultrasound to travel to the
object and back to the sensor. Using the speed of sound,
this time can be converted to distance.

It's a very logical process and isn't hard to understand.

Now I couldn't get my hands on the PING SEN136B5B


Sensor. Instead, I have a HC-SR04. For all our purposes, they
are EXACTLY the same.

If you do decide to use the SEN136B5B, then you'll have to


change bits of code, and the pins you connect to. It isn't
hard to do.
CONCLUSION:
What I showed you is merely a template. To make things
awesome, you should mix it up!

Maybe you could figure out how to use 2 of these at ones, or


do chords instead of notes (which would be amazing).
PROGRAM:

void setup() {//I declare whether I'm using the pins for input/output
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,INPUT);

//LED
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);

}
int sp = 6; //Speaker Pin
int tp = 7; //Trigger Pin
int ep = 8; // Echo Pin

//LED
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int led5 = 9;
int led6 = 5;
int led7 = 4;
int led8 = 3;

int c = 523;
int d = 587;
int e = 659;
int f = 698; //These are the frequency
int g = 784; //values of the different notes
int a = 880;
int b = 988;
int c2 = 1047;
void loop() { //This is the main loop, that keeps repeating
int notes[] = {c,d,e,f,g,a,b,c2}; //An array storing all the notes. It's kind of like a
dictionary.
long duration, distance; //Variables to store the values of time and length
digitalWrite(tp,LOW);
delayMicroseconds(2);
digitalWrite(tp,HIGH); //This area sends out a pulse from the trigger
delayMicroseconds(5);
digitalWrite(tp,LOW);
duration = pulseIn(ep, HIGH); //The echo is recieved, and saved as the duration
distance = (duration/2)/29.1; // We convert the time into distance in centimeters,
using the speed of sound and other factors.

if(distance>4&&distance<7){ //For a distance between 4 and 7, it plays C for


100 milliseconds
tone(sp,notes[0],100);
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
}
else if(distance>8&&distance<11){
tone(sp,notes[1],100); //For a distance between 8 and 11, it plays D for 100
milliseconds
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
}
else if(distance>12&&distance<15){
tone(sp,notes[2],100); //For a distance between 12 and 15, it plays E for
100 milliseconds
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
}
else if(distance>16&&distance<19){
tone(sp,notes[3],100); //For a distance between 16 and 19, it plays F for
100 milliseconds
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
}
else if(distance>20&&distance<23){
tone(sp,notes[4],100); //For a distance between 20 and 23, it plays G for
100 milliseconds
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, HIGH);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
}
else if(distance>24&&distance<27){
tone(sp,notes[5],100); //For a distance between 24 and 27, it plays A for
100 milliseconds
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, HIGH);
digitalWrite(led7, LOW);
digitalWrite(led8, LOW);
}
else if(distance>28&&distance<31){
tone(sp,notes[6],100); //For a distance between 28 and 31, it plays B for
100 milliseconds
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, HIGH);
digitalWrite(led8, LOW);
}
else if(distance>32&&distance<35){
tone(sp,notes[7],100); //For a distance between 32 and 35, it plays high C
for 100 milliseconds
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, HIGH);
}
}