Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
code from the paper https://cdn.hackaday.io/files/23081987497504/IEEE Project AWAAZ.pdf
const int trigPin = 3;
const int echoPin = 2;
void setup()
{
// initialize serial communication:
Serial.begin(115200);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
repeat:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: 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(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
if( duration>10000)
{
goto repeat;
}
Serial.print(cm);
Serial.print(" ");
Serial.println();
int OutputPin=9;
unsigned int thisPitch = map(cm, 2, 400, 150, 15000);
// play the pitch:
tone(OutputPin, thisPitch, 10);
// delay in between reads for stability
Serial.print(thisPitch);
Serial.print(" ");
delay(5);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.