Arduino Tutorial 61: Improving Precision of Your Distance Measurements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy calm and we are here today with lesson number 61 in our new our improved our legendary Arduino tutorial series what I need you to do is pour yourself a nice big mug of iced coffee what I need you to do is to get out your project from lesson number sixty which was to build a ultrasonic distance sensor with a go button okay you press the button it makes a measurement okay it prompts you to place the target okay I've got the target place there press the button and make a measurement report it okay that's what we did in Lesson number 60 what your homework was in Lesson number 60 was to improve the precision of this because well it's pretty nice it if I press the button again two point seven six inches I press the button again two point seven I press the button again two point seven four you see there's variation as we do this and every time we do it to point nine two that's like two-tenths of an inch off from what we've gotten earlier two point seven four see see there's noise and the data there's variation and in doing that the target wasn't moving but we weren't getting perfect numbers so how could you improve precision well one thing that you might think about is to check all of your wires make sure everything has a secure connection because if you've got a flaky connection that could cause the problem but man if you look at the neatness of this build this build is nice we're not going to get a different sensor because we're kind of stuck with the sensor that we have so what ideas did you guys have as far as improving your precision well since everything is done by the Arduino why not make a bunch of measurements and average them okay why not make a bunch of measurements in average them so let's go to a code view I don't think we're gonna have to change the hardware here but let's go to a code view and let's see if we can do that we are starting with the software that we ended up with in lesson number 60 if you go to top tech boy comm and if you go to our dueƱo tutorial 60 the code is listed there if you need to get it back together and those earlier videos show you how to hook this thing up I think in 55 we did the basic connections and 59 we made it portable and then in 60 we added the Go button and so if you're just jumping into this lesson you probably need to go back and catch up on some of those earlier lessons but let's see what we can do here we want to average we want to make a bunch of readings and average them so what are we going to do well we would need to decide how many measurements do we want to make I will call that num num mesh and let's say let's do 50 and just see how that does if we're gonna do this we need to know the average the average of the measurements we won't put a number there because we will calculate that and now what else do we need we need a num mesh we're going to need probably a counter for a for loop so we will go ahead and do a J here I like to use J instead of I because I looks too much like L and it looks too much like 1 and if you make a little mistake when you're using I as a variable it can be very very difficult to troubleshoot just one of those Pro tips that I am happy to give you also how do you average numbers well you add a bunch of numbers together and then you divide by the number of numbers which would be num mess but we need a variable like a bucket to put those numbers in as we're getting them in off the sensor so we will just call that bucket ok we will call that bucket I think that is a pretty good set of variables to start with now what do we do here this is our void setup I don't think I know these are just my these are the other variables to run the sensor and to run the LCD and the button and so forth and so we don't need to do anything in the void setup we come down to the void loop what are we doing here we are prompting the user for a measurement prompting him to press the button and then we're looking to see if the button is pressed and then when the button is pressed we come down here and before we were making one measurement but now we don't want to make one measurement we want to make how many num mesh num mesh the number of measurements okay so we will come here and we will say four and then what is our counter J equal one we're sucking to start jackal one and then we'll go as long as J is less than or equal to none measure so how many times will this loop well it will start at one and it will go all the way up to numbness because it's less than or equal to it will loop all the way through num mesh times and then each time through the loop J is equal to J plus 1 as such okay that was not a good there right then puts the closed curly for me well what do we want to do in here well a big part of what we want to do in here is to calculate this distance so all this neato code that we did to calculate distance I'm going to cut that and I'm going to put that inside the for loop by pasting okay so we send the trig pin low we wait we send the trig pen high we wait we send the treat pin low we wait then we measure ping travel time with a pulse in then we delay and then we calculate ping travel distance and then we calculate distance to target you would need to go back to the lessons around probably less than 55 if you want more information on how that works all right but what we want to do is now we have this distance to target what we want to do is we want to put that in the bucket so we're going to say bucket is equal to what well whatever was there before okay plus distance distance to target now since we are saying bucket is equal to bucket the first time through bucket has to have a value or we will get an error so I guess when we come up here and declare bucket we better set it equal to zero we better empty our bucket before we get started right so where were we bucket is equal to bucket plus distance to target okay so every time through the loop it adds the measurement that we are making a distance to the bucket now when we come out of the loop what do we want to say we want to say the average average is equal to what well it's the bucket divided by the number of items what is the number of items well that was numbness numbness number of times you measure mesh now one of the number one mistakes that I see students make is to put this divided inside of the loop what's the problem with that as you're going through you're throwing numbers in the bucket and then you're dividing by that numerator way too many times how many times do you divide the total by the number one time so you do it outside the loop not inside the loop now what we will want to do is distance to the target we might want to say now average this distance to target is and now instead of distance to target we plot or print ever edge ever edge like that okay so now we ought to give something while we're in that loop we ought to give some sort of feedback to the person like I don't know how long it's going to take to make all these measurements so we're while we're inside here we should probably say something where was that for loop okay I'm trying to think I think I'll just do it before the for loop okay so sir I will say always good to kind of set things up so I will say LCD LCD dot set set cursor and I will put it at column zero row zero and then I will do LCD dot print measuring like that and then I think that after we do all of the measuring we do a clear here before we do any more printing in so that should probably clear it so let's let's download this thing please hold your breath Oh denied denied ah what's the problem the problem is you did not hold your breath and the other problem is we needed an end of string there Oh denied again what is this average was not declared what kind of nonsense is that as I called it I'm sorry did you guys kept catch that it is late in the day perhaps a little caffeine would help the situation all yes okay now also we need to print your ab Ness mass okay so now hold your breath no denied problem uploading to board okay I will tell you what that problem probably is I have the C Drive a serial monitor open somewhere I don't what was our last project our last project was to make this portable and when it's portable it is plugged into a battery not plugged into the computer I think I made that same mistake and one of the very recent one of the very very recent tutorials and so we will plug this in we will take a happy little USB noise that's that's good another shot of caffeine okay so now we are going to try to download again please please please please please yes yes yes please boom at least it at least it downloaded okay now we're gonna go over to our live action shot our dramatic overhead shot if I can find the cursor on my 27 screens okay so this is our live action shot and so now we are going to bring this up a little bit I'm gonna get everything okay we are going to press the button measuring target distance 3.48 okay did you see I didn't completely we have that thing cleared so we need to come back to our code view did you see that second line when I was saying measuring did not look good so where did we print that right here so we set the cursor we better do an LCD dot clear here to get rid of that second line all right hold your breath LCD clear what's gonna work boom all right okay so now look at that all right so now we are ready for action if I can get this slowly keyboard out of the way okay let's go to our dramatic overhead shot again I had trouble finding my cursor okay so let's get this set up ah what is it telling me to do it is telling me to place the target and I'm trying to get this where you don't have a glare okay and then press to measure so I will press to measure measuring that looks good 3.0 six inches press to measure 3.08 press to measure now that was crazy that was crazy point Oh - what's wrong 3.08 press to measure 0.03 that's crazy measuring point-o - what's wrong it's like 0.0 - okay we're getting very calm we're getting very tricky results it seems to work the first time let me try to download it again okay let it download it and it seems to be happy okay and now let me press the button measuring we know two inches mm-hmm something very strange is going on let's go in and look at the code where we're doing the averaging for J is equal to 1 J is less than or equal to num mesh and then J is equal to J plus 1 then we write we make the measurement we delay and then we say bucket is equal to bucket plus distance to target which we did and then Ave mesh is equal to bucket divided by the number of measurements mmm this is very strange well one thing that I can think is that when we look up here and we look at distance to target and King travel time and ping travel distance we called those what floats but when we did have measure we made that of what an int what would that be a lot smarter to make that that would be a lot smarter to make that a float okay because we are having a rounding problem when we are doing this because the distance if the distance is the in between numbers the average needs to be the in between numbers okay so that was probably a mistake sometimes I make these mistakes on purpose because these are the mistakes that I see people making over and over and over and declaring something an INT where it really needs to be a float now we are going to say when the button is pressed we do that LCD print okay and then bucket is bucket plus distance to target okay so let's try this again oh I hope it works I hope it works oh it's taking a long time maybe it's getting be kind of a long program okay so let's go to our dramatic overhead shot and then all eyes on the screen because the camera sink is causing a little bit of what looks like a little blinking in the LCD but when I'm looking at it with my eye it looks really good so press 0.02 inches 0.03 inches man we have got ourselves still a problem here so let's see how many measurements are we doing we are doing num mesh is 50 what if we are putting floats in the bucket what should bucket be bucket should be a float too I'm sorry you didn't see that let me go back over here you see we had bucket as an INT and if if the measurement it's a float and we're adding it into bucket we better darn sure add it as a additives of you know put it in a float does that make sense because if we're adding 3.25 to 2.74 that is going to be a floating-point number so we need to declare it as a float so let's now it's asking me again to press the button let's press the button 3.1 2 inches 0.03 inches Wow it doesn't seem to work after the first time three point one four inches work that time three point one two 3.14 now in that cool how I made this come out to be pi takes a pretty sharp eye to get that to happen 3.1 3.1 3.14 okay do you guys see that we've taken out a lot of that noise let's let's do this like five times let's do this like five times and see what variation we get okay so I'm going to try to hold it really still I'm going to try to put it where you can see it okay and so I'm going to get 0.03 I wonder why it is giving me that erroneous result three inches two point nine eight 2.94 2.94 do you see how we're not getting those off numbers like we were before that without averaging it we are getting a much much closer pack number this seems to be going pretty fast and so what if we came in and we did a lot more right what if we did a lot more measuring and a lot more averaging and so how many did we say that we had done here we did 50 what if we do 500 okay that's the nice thing about using you know a microprocessor you know doesn't really cost you anything but a little bit of time and then let's look and look at our timing here delay of 25 in there all right let's try this okay it has downloaded it is wanting a distance to the target I'm going to set this thing flat because it is easier and then I'm going to try to adjust it here where you don't see the glare let's see if it works better on this other view yeah I think that's I think that's a lot better okay and let me see if I can adjust the brightness a little bit where you can see it a little clearer all right so now pressing the button getting everything out of the way pressing the button taking a little while to make the measurement this time because after all we are doing it 500 times do you think maybe I went a little far with 500 3.92 prompts me waiting waiting waiting 3.92 3.93 3.19 do you see how this is just almost like dead-on now with this averaging I think what I'm going to do is that 500 is making it a little bit slow what if we try 100 would somebody have the patience to wait for 100 and once again I didn't show you that so I'm showing you I'm showing none as none measure is a hundred we are now going to download that and then we are going to go over to our dramatic overhead aerial shot and then it is prompting me to press the button 3.88 press it again I think that's not too long for point zero five 4.0 nine 4.0 9 you know I might have jiggled it just a little bit 4.0 5 in general the more times you averaged it the better that you're gonna get but I would say the 500 was a little bit better than a hundred but it was a little bit slow to respond so I think that if it were me I would probably average it about a hundred times and you can see by putting the averaging in there it allows you to get a little bit more precision in your number and also what I hope you see is you kind of see how you can do a verage in in the Arduino you have a for loop that thing you're doing you do over and over you add the numbers to a bucket and then you divide by the number that you have okay that was lesson number I believe lesson number 61 I'm trying to think if there's anywhere else to go with this I think that is pretty good probably in lesson number 62 I'll come back and maybe we'll start going a new direction and you know so I don't really have any homework for you this time okay appreciate you tuning in think about giving it's giving us a thumbs up think about subscribing to the channel make sure you ring that bell hit the bell so that you'll get notifications on upcoming videos when they're posted share this with other people right let's try to get some community let's try to get some some more people working with us here palma quarter from top boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 15,366
Rating: undefined out of 5
Keywords: HC-SR04, Arduino, Precision, Measure Distance
Id: nC2CDaqJomA
Channel Id: undefined
Length: 27min 42sec (1662 seconds)
Published: Tue May 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.