We have seen in previous posts (here and here) that if the time between two measurements is too short then the sensor outputs a 0 to signal a measurement error. We call these kind of errors ‘oversampling’, i.e. sampling too frequently beyond the capacity of the sensor.
From data collected in this post, we have seen that a delay of 26ms is enough to solve the problem. This note tries to answer the following question:
can we minimize this time?
The idea
We have conceived a bisection procedure which will try to determine the minimal delay to use in order to avoid oversampling. The idea is to start with delay D=26ms. Therefore the search interval will be
I0=(a0,b0)=[0,D]
Then, if there are no oversampling errors, I_0 is halved and a new sample batch is run. If at some stage i oversampling errors are more than a certain threshold ε, we search in the interval
otherwise we keep on halving the interval. The procedure is repeated until |ai-bi|=1ms.
(ai+1,bi+1)=[b,(b-ai)/2] for i>1
otherwise we keep on halving the interval. The procedure is repeated until |ai-bi|=1ms.
At this point, a second procedure will search for the right number of us. According to Arduino specifications (see here), this second procedure is stopped if the length of the interval is less than 3us (i.e. |ai-bi|<=3us).
The program
The program which performs the experiment is given at the end of this post. Let us comment its parts. First of all, the constants
#define SAMPLESIZE 1000 #define NUMTESTS 5
are to indicate that we are going to perform 5 (NUMTESTS) batches and that each batch will consist of 1000 (SAMPLESIZE) tests. A test fails if the percentage of errors (zeroes) which are measured in a batch is greater than a TOLERANCE. Here the tolerance is fixed at 1% (see below).
#define TOLERANCE .01
Finally the following two constants define the initial length of the search interval for milliseconds and for microseconds.
#define INITDELAYMS 26 #define INITDELAYUS 1000
As we have already said, the value for INITDELAYMS is fixed to 26ms because of previous experiences. INITDELAYUS is naturally fixed to 1000us since it is the amount of micros in a millisecond.
The following portion of code is taken directly for here and just performs one read of the sensor. Remark that echo pin is connected to Arduino pin 2 and trigger pin is connected to Arduino pin 4.