Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
int tonePin = 4;
int trigPin = 9;
int echoPin = 10;
int proximity=0;
int duration;
int distance;
void setup() {
//Serial Port
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(tonePin, OUTPUT);
}
void loop() {
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
proximity=map(distance, 0, 45, 8, 0);
Serial.println(proximity);
if (proximity <= 0){
proximity=0;
}
else if (proximity >= 3 && proximity <= 4){
tone(tonePin, 200000, 50);
}
else if (proximity >= 5 && proximity <= 6){
tone(tonePin,20000, 100);
}
else if (proximity >= 7 && proximity <= 8){
tone(tonePin, 20000, 200);
}
delay(600);
noTone(tonePin);
}
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.