Hello. I have implemented an ultrasonic sensor but the measurements were too noisy so I decided to take the average of some samples. Here is the code:
int trigger_pin = 12;
int echo_pin = 11;
int samples = 30;
float samplesf = 30.0;
int travel =0.0;
float aggregate;
int baud = 9600;
float distance_mm;
float time_s;
float avg;
void setup()
{
pinMode(trigger_pin, OUTPUT);
pinMode(echo_pin, INPUT);
Serial.begin(baud);
}
void loop()
{
for (int i = 0; i<samples; i++)
{
if (i==0) {
aggregate = 0;
}
digitalWrite(trigger_pin,LOW);
delayMicroseconds(10);
digitalWrite(trigger_pin,HIGH);
delayMicroseconds(10);
digitalWrite(trigger_pin,LOW);
travel = pulseIn(echo_pin,HIGH);
aggregate = aggregate + travel;
Serial.print("aggregate:"); <------ This is the line I'm talking about
Serial.println(aggregate);
}
avg = (aggregate/(samplesf));
Serial.print("Average:");
Serial.println(avg);
distance_mm = measure2mm(avg);
//Serial.print(0);
//Serial.print(",");
Serial.print("Distance[mm]");
Serial.println(distance_mm);
//Serial.print(",");
//Serial.println(400);
}
float measure2mm ( int measure)
{
int mm;
mm = 0.17*measure + 11.36;
return mm;
}
And it works fine like this. However, I have tried to use the serial plot to visualize the data but when I commented out the line with the arrow (see above) the code doens't work properly. This doesn't make any sense to mee so I was wondering if somebody could explain what is causing this odd behaviour. Thanks! (Some things in the code are unnecessary like the samplesf variable but I was trying every option to make this work since it didnt make sense)
for (int i = 0; i<samples; i++)
{
if (i==0) {
aggregate = 0;
}
Wuh?
float measure2mm ( int measure)
...and then you go and return an int.
Why?
travel = pulseIn(echo_pin,HIGH)
pulseIn returns an unsigned long, not an int.
I'm going to guess that you're pinging too frequently.
Put a delay(100) at the end of loop, and report back.
Wuh?
Well I said there was a lot of unnecessary stuff I put in trying to debug this that was one of them, dont worry about it, pretend that it is before the loop
...and then you go and return an int.
Well yeah that makes no sense. It worked sufficiently so I never bothered to look at that method again but yeah that's pretty stupid, I'll fix it
pulseIn returns an unsigned long, not an int.
Didn't know that! thanks
I'm going to guess that you're pinging too frequently.
Put a delay(100) at the end of loop, and report back.
That did work! In fact, only a delay(10) worked too. Thanks!
But I'm still wondering what the serial.print had to do with it? I suppose I was inadvertedly causing a delay by printing?
Additionally, why exactly do I have to wait there? Is it because the echo hasn't settled yet when I send a pulse to trigger again?
Thanks for your help!
You never explained what
the code doens't work properly
means.
The print would have slowed the code down by around 12 to 15 ms.
You can't ping too frequently or the sensor will mistake distant returns from the previous ping as nearby ones from the most recent.
Try to limit your ping rate to no more than about 20 - 30 Hz, depending on environment.
You never explained what
Quote
the code doens't work properly
means.
The measured distance was wrong! It was way shorter than it should.
The print would have slowed the code down by around 12 to 15 ms.
I think those delays were making it work..I was only thinking in terms of software so this made no sense but now I get it, thanks!