Created
July 4, 2016 00:46
-
-
Save houmei/55564a6a0b790cc997d2a62f8b24b8ec 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
// Grove UltraSonic Sensor | |
#define Sensor 7 | |
void setup() | |
{ | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
long RangeInCentimeters; | |
Serial.println("The distance to obstacles in front is: "); | |
RangeInCentimeters = UltrasonicMeasureInCentimeters(Sensor); | |
Serial.print(RangeInCentimeters);//0~400cm | |
Serial.println(" cm"); | |
delay(250); | |
} | |
// copy from https://github.com/Seeed-Studio/Grove_Ultrasonic_Ranger/blob/master/Ultrasonic.cpp | |
long UltrasonicMeasureInCentimeters(int _pin) | |
{ | |
pinMode(_pin, OUTPUT); | |
digitalWrite(_pin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(_pin, HIGH); | |
delayMicroseconds(5); | |
digitalWrite(_pin,LOW); | |
pinMode(_pin,INPUT); | |
long duration; | |
duration = pulseIn(_pin,HIGH); | |
long RangeInCentimeters; | |
RangeInCentimeters = duration/29/2; | |
return RangeInCentimeters; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment