Arduino Tutorial 61: Improving Precision of Your Distance Measurements

preview_player
Показать описание
You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming:

In this lesson you learn how to improve the precision of your distance measurements by averaging a large number of readings.

You can get the kit I am using for this series at the following link:

I strongly suggest picking up an arduino nano, since it can plug directly into the breadboard, making a portable system more practical. You can pick one up here:

As projects get more complicated in these lessons, you guys really need to get a set of breadboard jumper wires which allow you to make neater connections on the board in your projects. You can pick a pack of these wires up here:

In addition as projects get more complicated, you are going to need a bigger breadboard. This is a reasonable one here:

You can get more details of this project at our WEB site here:

#Arduino
Рекомендации по теме
Комментарии
Автор

Hi Paul!
Thanks for the tutorial!

Unfortunately, you made several mistakes. You display avMeas only to Serial, not to the LCD :(
To the LCD you display the last measured distanceToTarget.

And if you display avMeas to the LCD, you will immediately see that you need to clean the bucket, set it to 0 before the loop.

oleksandr_honcharov
Автор

Hey Paul, thanks for this amazing Arduino Series. Completing the assignment was a breeze.

i noticed that you made a big mistake because you were not printing the avMeas to the LCD, instead you were printing it to the Serial Monitor. So the measurement you were getting on the LCD was the last distanceToTarget measurement before exiting the for loop.

Another mistake you made was that you were not setting the bucket to 0 before each for loop. I made this same mistake when i was doing the assignment but i noticed immediately after the second measurement.

Apart from that, this was a great lesson. I have been learning so much and i am excited to keep learning. Thank you.

alexm.
Автор

Hello Paul,

Great lesson as always Wouldn't it be a good idea to clear the bucket after displaying the distance on the LCD each time. Seems this would make the average more accurate? Thanks for all your hard work !!!! Rick

rbonari
Автор

@ 13:28 *“Measuring”* not “measureing.”

brucesmith
Автор

You need to reset the bucket variable before the loop starts or your value will slowly get larger (as your sum keeps getting slightly bigger and you are always dividing by the same amount). Also, how did the display print a decimal number originally when your average was an integer type?

stefanonicolini
Автор

Great lesson! I did the averaging using arrays. I remember studying them in school but I had to remind myself how they worked. If anyone is interested, here is the code:
#include <LiquidCrystal.h>
const int dOutPin = 13;
const int dInPin = 12;
const int rs = 10;
const int en = 9;
const int db4 = 8;
const int db5 = 7;
const int db6 = 6;
const int db7 = 5;
const int pushPin = 11;
int buttVal=1;
int i=1;
float sum=0.;
float av;
int bucket[20];
float dInV[20];
float dFinV[20];
LiquidCrystal lcd(rs, en, db4, db5, db6, db7);

void setup() {
Serial.begin(9600);
pinMode(dOutPin, OUTPUT);
pinMode(dInPin, INPUT);
pinMode(pushPin, INPUT_PULLUP);
lcd.begin(16, 2);
}

void loop() {
lcd.print("Push the button");
buttVal = digitalRead(pushPin);
while(buttVal==1){
buttVal = digitalRead(pushPin);
}
lcd.clear();
int i;
for(i=0; i<20; i=i+1){
lcd.setCursor(0, 0);
lcd.print("Wait...");
digitalWrite(dOutPin, LOW);
delayMicroseconds(10);
digitalWrite(dOutPin, HIGH);
delayMicroseconds(10);
digitalWrite(dOutPin, LOW);
dInV[i] = pulseIn(dInPin, HIGH);

sum=sum+dFinV[i];
delayMicroseconds(60);
}
lcd.clear();
av=sum/20;
lcd.setCursor(0, 0);
lcd.print(av);
lcd.print("cm");
delay(6000);
lcd.clear();
}

mariorosenov
Автор

Great lesson as always. I did a version of averaging but have to reset 'bucket' back to zero at the beginning of loop. I'm not sure why you didn't have to do it.

Concan
Автор

I'm not sure where I screwed up... kept racking my brain. Each time I'd take a measurement, the distance would increase. I fixed the glitch by setting bucket=0 and j=1 at the end of each loop.

orndorff
Автор

We have initialized bucket to 0 at start and not clearing it in the loop. so, bucket is taking its last measured value . Therefore its better to set bucket to 0 in the for loop.

uppey_dhanush
Автор

Thank you Mr.McWhorter, this is very useful, although I folded like a cheap lawn chair and couldn't do it on my own sorry...

aynursunagatullin
Автор

Might you have shortened the delay time between measurements to keep your sampling high without the long delay in results?

boydrogers
Автор

Paul you are not printing avMeas to the lcd, you are doing Serial.print. You are printing distanceToTarget. None of the averaging makes a difference to this number. Since I was printing avMeas to the lcd, I was getting crazier numbers like 90 inches for something 2 inches away. After emptying the bucket, I was able to get closer but still wrong measurements. To be honest, I don't understand why, it should work.

for (j=1;j<=nummeas;j=j+1){
digitalWrite(triggerPin, LOW);
delayMicroseconds(dtm);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(dtm);
digitalWrite(triggerPin, LOW);
delayMicroseconds(dtm);
pingTravelTime=pulseIn(echoPin, HIGH);



}
avmeas=bucket/nummeas;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Target Distance: ");
lcd.setCursor(0, 1);
lcd.clear();
lcd.print(avmeas);
lcd.print(" Inches");
delay(dt);
bucket=0;

TheRealFrankWizza
Автор

EXPLANATION FOR 0.03 INCH MEASUREMENTS:
Paul - your tutorials are generally very good. Unfortunately, this lesson wasn't up to the usual standard. You said (11:08) "it is late in the day... perhaps a little caffeine will help", but it didn't seem to, and you really should do a follow up to correct the errors.
Others have pointed out not clearing "bucket" and not displaying the average, but I've been digging into those spurious "0.0-something" measurements, and I'm pretty sure I've figured it out now.
It would probably have helped if you'd commented the code, because you have that delay statement there but there's nothing that says why you're delaying, or why the delay is 25 instead of some other number. Looking way back in lesson 51, I see you inserted the delay while saying "... I'm sending out new pings before the old ping has the time to come back...". If there'd been a comment on the line to that effect, it might have prompted you to think about the delay as a possible source of the error.
(It could also be used to teach a lesson about checking the documentation for devices you're using instead of guessing. Also, the datasheet says the minimim mesasurable distance is 2cm, which is more than three quarters of an inch, so obviously 0.03 can't ever be a valid result).

RottnRobbie
Автор

Hi Paul Thank you for the tutorial.
For this lesson there is a mistake the "float bucket = 0" that that declare before the void set up. When run the program it will contiune to sum up the previous number and did not reset to '0' after it completed. I confirm by add a Serial.println (bucket); after
After I move the "float bucket = 0" before the for loop. So when button press it start from the first reading instead of contiune sum up the previous number.

gimteelee
Автор

Don't forget to clear the bucket. And don't calculate distances in the for loop. Add the raw values, but calculate the total distance after the for loop has ended.

split
Автор

I don't see where you ever reset "bucket" to zero after you display and average measurement. This would allow previous measurements to accumulate into the new average, thus making secondary averages inaccurate. You should reset bucket to zero before entering the FOR() loop to accumulate measurements.

robertherzog
Автор

I Was able to do this on my own, really getting the hang of this, excellent tutor, I increased the number of measurements to 1000 and reduced the delay time to 1millisecond and only wait 1 second for result.

tretty
Автор

how many more episodes do you have planned for this series.

michaelnoel
Автор

Thanks again Paul. I might have my code mixed up, but unless I put a bucket=0 before the j for loop inside the void loop i get the bucket value accumulating each time I do a measure.

johnmorris
Автор

The lessons you teach are interesting but its literally the advice you are giving in the middle of videos is helping me a lot.
Thanks Paul for your beautiful lessons.

MKARTHICKARAVINDBEE