The sketch that i try come from the wiki of ultrasonic sensor Ultra Sonic range measurement module - Wiki
I only change the input ping to 3. I read in the release notes that for 1º gen the pings 2 and 3 give accurate results.
The circuit diagram is easy. Only the ultrasonic sensor and the arduino galileo. Signal pin conected to ping 3, vcc to 5v and gnd to gnd.
**********************
#include "Arduino.h"
class Ultrasonic
{
public:
Ultrasonic(int pin);
void DistanceMeasure(void);
long microsecondsToCentimeters(void);
long microsecondsToInches(void);
private:
int _pin;//pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger.
long duration;// the Pulse time received;
};
Ultrasonic::Ultrasonic(int pin)
{
_pin = pin;
}
/*Begin the detection and get the pulse back signal*/
void Ultrasonic::DistanceMeasure(void)
{
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW);
delayMicroseconds(2);
digitalWrite(_pin, HIGH);
delayMicroseconds(5);
digitalWrite(_pin,LOW);
pinMode(_pin,INPUT);
duration = pulseIn(_pin,HIGH);
}
/*The measured distance from the range 0 to 400 Centimeters*/
long Ultrasonic::microsecondsToCentimeters(void)
{
return duration/29/2;
}
/*The measured distance from the range 0 to 157 Inches*/
long Ultrasonic::microsecondsToInches(void)
{
return duration/74/2;
}
Ultrasonic ultrasonic(3);
void setup()
{
Serial.begin(9600);
}
void loop()
{
long RangeInInches;
long RangeInCentimeters;
ultrasonic.DistanceMeasure();// get the current signal time;
RangeInInches = ultrasonic.microsecondsToInches();//convert the time to inches;
RangeInCentimeters = ultrasonic.microsecondsToCentimeters();//convert the time to centimeters
Serial.println("The distance to obstacles in front is: ");
Serial.print(RangeInInches);//0~157 inches
Serial.println(" inch");
Serial.print(RangeInCentimeters);//0~400cm
Serial.println(" cm");
delay(100);
}
************************