Last active
August 15, 2017 00:39
-
-
Save GreenMoonArt/271b5b741fd5a208661ca5ee7a71e535 to your computer and use it in GitHub Desktop.
Ultrasonic Range Finder
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 trigPin = 12; | |
int echoPin = 13; | |
void setup() | |
{ | |
Serial.begin (9600); | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
} | |
void loop() | |
{ | |
int duration, distance; | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(1000); | |
digitalWrite(trigPin, LOW); | |
duration = pulseIn(echoPin, HIGH); | |
distance = (duration/2) / 29.1; | |
if (distance >= 500 || distance <= 0) | |
{ | |
Serial.println("Out of range"); | |
} | |
else | |
{ | |
Serial.print(distance); | |
Serial.println(" cm"); | |
} | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment