Created
August 29, 2015 15:58
-
-
Save carloscarcamo/2d2bf6a22ef8c7f081a1 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
#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