1

I'm a newbie at Arduino, and I'm trying to make a project with ultrasonic sensor, I'm using the grove ultrasonic ranger, not the HC-SR04, and I already did test with distance and it worked very well, but now I need help with the code.

I need to create a vector that save the values of the distance to after calculate the speed using distance/time calculus, but the case of my project its to use in open places, so will be times that the distance will be "max" because there's nobody in the range, so I need a condition to exclude those values of the array (the maximum distance printed when the sensor can't feel nothing is 516 cm)
I'm not experienced with Arduino coding, so please help me, in anex is my code for basic scanning.

#include "Ultrasonic.h"

Ultrasonic ultrasonic(7);
void setup()
{
    Serial.begin(9600);
}
void loop()
{
    long RangeInCentimeters;
    Serial.println("The distance to obstacles in front is: ");

    // two measurements should keep an interval
    RangeInCentimeters = ultrasonic.MeasureInCentimeters();
    if(RangeInCentimeters > 400)
    Serial.print ("distancia nula");
    else
    Serial.print(RangeInCentimeters);//0~400cm
    Serial.println(" cm");
    delay(1000);
}
2
  • the question is how can i do this, create a vector that save the distances above 400cm and exclude the rest
    – rokazz
    Dec 13 '18 at 15:04
  • Do you need the distance up to 400 cm and ignore the rest, or over 400cm and ignore all values below 400cm?
    – Tom
    Jan 3 '19 at 15:11
1

If you don't know how many data points will be recorded, look into vectors or an external storage device. Alternatively you could use a circular array if you don't mind losing recorded data after a while.

If you know how many data points will be recorded, a basic array will suffice.

As for excluding invalid values:

// Keep trying to get a valid data point 
While (RangeInCentimeters > 512 && RangeInCentimeters < 400)
{
 delay(200);  // The minimum value of this delay depends on the sample rate of your sensor
 RangeInCentimeters = ultrasonic.MeasureInCentimeters();
}

//... Store data to array/linked list/etc

I changed your max range to 512 because this is likely the max real value (2^9 = 512).

8
  • while?.......
    – Juraj
    Dec 13 '18 at 15:43
  • why not let the loop() loop?
    – Juraj
    Dec 13 '18 at 15:56
  • 1
    @rokazz For only 10 data points just use an array. However I would suggest changing your delay value because in 10 seconds (the time it takes to make 10 measurements) the runner will have gone much farther than 512cm
    – Rekamanon
    Dec 13 '18 at 16:06
  • 1
    yes, the delay time will be 100 or 200, it depends from the results of precision
    – rokazz
    Dec 13 '18 at 16:22
  • 1
    i was thinking in another solution, initially i was thinking about the sensor pointing in to the athlete running in the straight line of the field and with the data calculate the speed, but i also can make it stand by all the lap, when the runner pass it starts a time and when pass again stop and make a calculus of the distance of the field and time, giving the speed, what do you guys think about it?
    – rokazz
    Dec 13 '18 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.