Skip to content

Instantly share code, notes, and snippets.

@houmei
Created July 4, 2016 00:46
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 houmei/55564a6a0b790cc997d2a62f8b24b8ec to your computer and use it in GitHub Desktop.
Save houmei/55564a6a0b790cc997d2a62f8b24b8ec to your computer and use it in GitHub Desktop.
// 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