-
-
Save RuiSantosdotme/9ad7a2cf1fe4d6c24c7892880fa3def6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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