Skip to content

Instantly share code, notes, and snippets.

@carloscarcamo
Created August 29, 2015 15:58
Show Gist options
  • Save carloscarcamo/2d2bf6a22ef8c7f081a1 to your computer and use it in GitHub Desktop.
Save carloscarcamo/2d2bf6a22ef8c7f081a1 to your computer and use it in GitHub Desktop.
#define EPin 7 // Echo Pin
#define TPin 8 // Trigger Pin
#define LPin 9 // LED
int maxD = 200;
int minD = 0;
long duration, distance;
void setup()
{
Serial.begin (9600);
pinMode(TPin, OUTPUT);
pinMode(EPin, INPUT);
pinMode(LPin, OUTPUT);
}
void loop()
{
digitalWrite(TPin, LOW);
delayMicroseconds(2);
digitalWrite(TPin, HIGH);
delayMicroseconds(10);
digitalWrite(TPin, LOW);
duration = pulseIn(EPin, HIGH);
distance = duration/58.2;
if (distance >= maxD || distance <= minD)
{
Serial.println("-1");
digitalWrite(LPin, HIGH);
}
else
{
Serial.println(distance);
digitalWrite(LPin, LOW);
}
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment