Codes:
/*
*Name:Alan Yang
*VIs 147 Midterm Project
*The sound of a space
*
*
*More info:
*THis is a small electronic devide which can detect objects from the distance.
*And then the distance will be transalted into the sound.
*The duration of the sound is depends on the distance.
*/
// these constants won't change:
const int ledPin = 13; // led connected to digital pin 13
const int electret = 1; // the amplifier output is connected to analog pin 1.(Mircophone)
const int pingPin = 7; // ultra sonic sensor
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int sensorMax = 0;
int sensorMin = 1023;
int threshold;
//Starting up the device and detecter the background noise.
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
pinMode(4, OUTPUT);
while (millis() < 3000) {
threshold = analogRead(electret);
// record the maximum sensor value
if (threshold > sensorMax) {
sensorMax = threshold;
}
}
// signal the end of the calibration period
digitalWrite(13, LOW);
threshold = sensorMax;
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(electret);
// if the sensor reading is greater than the threshold: (activate the distance sensor)
if ((sensorReading >= 200)) {
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
digitalWrite(13,HIGH);
int freq = duration;//sensorReading*10; // the warning sound has a pitch, it depends on the volume of the input sound.
//delay(inches); // Making an echo effects
buzz(4,freq,cm); // the output, make a warning sound.
digitalWrite(13,LOW);
}
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
// class to make buzzer work.
void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++)
{ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
}
}