Professional Documents
Culture Documents
Arduino Ultrasonic Musical Instrument
Arduino Ultrasonic Musical Instrument
Date Submitted:
____________________
Mr. Randy Mendoza
Professor
Introduction:
The objective here is to perform the musical instrument,
using ultrasonic sensor.
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.