Hello guys, im a newbie and recently im using a ping ultrasonic sensor to write a mean filter coding to filter off the noise. And it works great. However, if i were not to serial printout :
Serial.print(i);
Serial.print(":");
Serial.println(rangeValue*);* for the rangeValue, the mean value would be totally wrong. Meaning, it would produce correct value if only i serial printout the above coding. But if i were to ignore the above serial printout, i found out that the mean value is totally wrong. Wonder how can i solve it? Because i would only need the Serial.println(mean) as i would use it as a serial data to plot a graph. Thanks! Please help. ```
*#include <stdlib.h>
int arraySize=10;
float rangeValue[5]={0};
char buffer[10];
const int pingPin = 9;
float cm,duration;
float total, mean;
//serial print of mean value error occurs if the following rangeValue printout is ignored.
Serial.print(i);
Serial.print(":");
Serial.println(rangeValue[i]);
}
delay(1000);
}
float microsecondsToCentimeters(float microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
but i forgot to put it back from 5 because im trying to debug it on the process. Anyway, it produces incorrect results for the mean value unless the rangeValue is printed.
Serial.println(rangeValue[i]);
Any idea what's happening with arduino? Thanks for your feedback anyway!
Maybe the extra time taken in the serial prints is allowing spurious echoes to die before taking the new measurement.
Instead of the serial prints that you commented out, what happens if you put in a simple "delay" of say 10 milliseconds?
Still some minor scope issues that could be tidied.
AWOL, so what re you suggest has something to do with delay intervals between the data transmitted. It's possible and i ll give it a try later. I have several classes to attend for the continuous hours now and i left my arduino kits in my lab. Will update with my findings later. Thanks!
Unless you've still not corrected the initial array overflow, or there's something else you haven't shown us, it isn't a memory issue.
Uncompiled, untested:
const int arraySize = 10;
const int pingPin = 9;
const int printPin = 2;
bool withPrint = false;
float rangeValue[arraySize];
void setup()
{
Serial.begin(9600);
pinMode (printPin, INPUT);
digitalWrite (printPin, HIGH);
}
void loop()
{
withPrint = digitalRead (printPin); // ground this pin to switch off printing
callUltrasonic();
Serial.println(calculateMean(rangeValue));
delay(1000);
}
void callUltrasonic()
{
for(int i = 0; i < arraySize; i++) {
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
long duration = pulseIn(pingPin, HIGH);
rangeValue[i] = microsecondsToCentimeters((float)duration);
if (withPrint) {
Serial.print(i);
Serial.print(":");
Serial.println(rangeValue[i]);
}
// maybe a short delay here to allow echoes to die away.
}
}
float microsecondsToCentimeters(float microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
float calculateMean(float array[])
{
float total = 0.0;
for(int i = 0; i < arraySize; ++i) {
total += array[j];
}
float mean = total / (float)arraySize;
return mean;
}
Alright Awol, i'll give it a try once i get my hands on my arduino kit. Oh ok, never knew that it can be wrote this way turn off the serial print. I ll follow up with alternating the codes corrected by you, Thanks!
Awol, thanks to your tidy code and ur last command;
// maybe a short delay here to allow echoes to die away.
and i add in delay of 20ms, the echoes die straight!
YES, thank you very much, since the nature of the problem is the echoes timing and so on.
Thank you all for the help!