Skip to content

Instantly share code, notes, and snippets.

@GreenMoonArt
Last active August 15, 2017 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GreenMoonArt/271b5b741fd5a208661ca5ee7a71e535 to your computer and use it in GitHub Desktop.
Save GreenMoonArt/271b5b741fd5a208661ca5ee7a71e535 to your computer and use it in GitHub Desktop.
Ultrasonic Range Finder
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