I have a grove starter kit and i try to using Ultrasonic ranger. I don’t find informations for using argon/xenon/boron with grove starter kit, specially of how initialise the correct PIN.
I use the D2 connector on my grove shield for particle. here is the (simple code):
#include "Ultrasonic.h"
Ultrasonic ultrasonic(2);
void setup()
{
}
void loop()
{
get_ultrasonic_value();
}
void get_ultrasonic_value() {
long RangeInCentimeters;
RangeInCentimeters = ultrasonic.MeasureInCentimeters(); // two measurements should keep an interval
delay(250);
Particle.publish("ultrasonic",String(RangeInCentimeters));
}
One thing I can say for sure it that you should not use 2 for the pin designation but go with the actual pin label D2.
If D2 happens to line up with the anonymous pin number 2 (as does on Gen1 & 2 devices) that’s not always guaranteed and hence should not be counted on.
Also which of the Ultrasonic libraries are you using?
I am not sure how this grove ultrasonic ranger works. The HC-SR04 uses 2 pins, first the ping or trigger and second a receiver pin whereas this just has one pin. Try printing out to serial like the example below.
> #include "Ultrasonic.h"
> long RangeInInches;
> long RangeInCentimeters;
>
> Ultrasonic ultrasonic(D7);
>
> void setup()
> {
> Serial.begin(9600);
> }
>
> void loop()
> {
> Serial.println("The distance to obstacles in front is: ");
> RangeInInches = ultrasonic.MeasureInInches();
> Serial.print(RangeInInches);//0~157 inches
> Serial.println(" inch");
> delay(250);
>
> RangeInCentimeters = ultrasonic.MeasureInCentimeters(); // two measurements should keep an interval
> Serial.print(RangeInCentimeters);//0~400cm
> Serial.println(" cm");
> delay(250);
> }