0

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);
}
  • 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 at 15:11
0

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).

  • while?....... – Juraj Dec 13 '18 at 15:43
  • why not let the loop() loop? – Juraj Dec 13 '18 at 15:56
  • its for track athletes, so i did a calculus, and it will print around 10 moves every lap, my think about the memory trouble its to every lap the array placing the values, them calculating the speed and putting in another array, then reset the array, so with 2 arrays, 1 for speed and 1 for data, reseting the data array will not solute the memory trouble? – rokazz Dec 13 '18 at 16:00
  • Having the while loop would let you reduce the delay value inside and pick up the next valid measurement as soon as possible, instead of waiting the full 1000ms. Other than than it's not needed. – Rekamanon Dec 13 '18 at 16:03
  • 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

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.