Help here!

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;

void setup()
{
  Serial.begin(9600);
}
void loop()
{
callUltrasonic();
mean = calculateMean(rangeValue);

}
void callUltrasonic()
{
  total=0.00;
  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);
      duration = pulseIn(pingPin, HIGH);
    cm= microsecondsToCentimeters((float)duration);
    rangeValue[i]=(float)cm;
   
    rangeValue[i]=cm;
   
//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;
}

float calculateMean(float array[])
{
for(int j=0;j<arraySize;j++)
  {
    total+=(float)array[j];
  }
mean=(float)total/(float)arraySize;
Serial.println(mean);
return mean;

}*
```

int arraySize=10;
float rangeValue[5]={0};
   for(int i=0;i<arraySize;i++)
    {
     rangeValue[i]=(float)cm;

I think you have a problem here.

You declare an array of 5 values, initialize one of them, and then write into all 10 positions of the 5 element array.

oh wait, it's suppose to be

float rangeValue[10]={0};

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!

Your sketch is badly structured - you clear "total" in "callUltrasonic", but sum it in "calculateMean".

Why not simply zero it before you use it?

I'd revisit the scoping of your variables - there is no point in "total" having global scope.

This is the serial monitor print after i uploaded the code i tidied up based on your advice.

Mean:19.64
Mean:19.65
Mean:9.00
Mean:9.07
Mean:9.14
Mean:9.13
Mean:9.05
Mean:9.05
Mean:16.90
Mean:19.73
Mean:9.09
Mean:19.52
Mean:8.91
Mean:9.06
Mean:19.56
Mean:9.03
Mean:8.91
Mean:11.48
Mean:8.92
Mean:9.00
Mean:8.84
Mean:19.57
Mean:8.82
Mean:19.65
Mean:11.68
Mean:8.99
Mean:20.11
Mean:9.06
Mean:11.57
Mean:19.60
Mean:11.74
Mean:9.06
Mean:19.61
Mean:8.91
Mean:9.13
which my code would be

#include <stdlib.h>
int arraySize=10;
float rangeValue[10]={0};
char buffer[10];
const int pingPin = 9;
float cm,duration;
float mean;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
 callUltrasonic();
 mean = calculateMean(rangeValue);
}
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);
      duration = pulseIn(pingPin, HIGH);
      cm= microsecondsToCentimeters((float)duration);
   
      rangeValue[i]=(float)cm;
     
//this is still hidden  
     //Serial.print(i);
     //Serial.print(":");
     //Serial.println(rangeValue[i]);
   
}
    delay(1000);

} 

float microsecondsToCentimeters(float microseconds)
{
  return microseconds / 29 / 2;
}
float calculateMean(float array[])
{
  float total=0.0;
  for(int j=0;j<arraySize;j++)
  {
    total+=(float)array[j];
  }
 mean=(float)total/(float)arraySize;
 
 Serial.print("Mean:");
 Serial.println(mean);
 
 return mean;

}

But after i unhidden the printout of the rangeValue*;*
this is the following serial monitor:
0:21.81
1:21.91
2:21.81
3:21.81
4:21.93
5:21.91
6:21.91
7:21.91
8:21.91
9:21.81
Mean:21.87
0:21.79
1:21.81
2:21.93
3:21.91
4:21.91
5:21.91
6:21.91
7:21.81
8:21.81
9:21.91
Mean:21.87
0:21.91
1:21.91
2:21.91
3:21.91
4:21.91
5:21.79
6:21.81
7:21.91
8:21.91
9:21.91
Mean:21.89
0:21.91
1:21.91
2:21.97
3:21.83
4:21.81
5:21.91
6:21.91
7:21.91
8:21.91
9:21.97
Mean:21.91
0:21.91
1:21.97
2:21.83
3:21.91
4:21.91
5:21.91
6:21.91
7:21.91
8:21.91
9:21.83
Mean:21.90
0:21.81
1:21.86
2:21.91
3:21.91
4:21.91
5:21.91
6:21.91
7:21.83
8:21.83
9:21.91
Mean:21.88
0:21.91
1:21.93
2:21.91
3:21.91
4:21.91
5:21.79
6:21.81
7:21.91
8:21.91
9:21.91
Mean:21.89
0:21.93
1:21.97
2:21.91
3:21.81
4:21.81
5:21.93
6:21.91
7:21.91
8:21.91
9:21.91
Mean:21.90
0:21.91
1:21.97
2:21.81
3:21.93
4:21.91
5:21.91
6:21.91
7:21.97
8:21.93
9:21.81
Mean:21.91
0:21.83
1:21.91
2:21.91
3:21.91
4:21.91
5:21.91
6:21.91
7:21.83
8:21.79
9:21.91
Mean:21.88
0:21.91
1:21.91
2:21.91
3:21.91
4:21.91
5:21.79
6:21.81
7:21.91
8:21.91
9:21.91
Mean:21.89
0:21.91
1:21.97
2:21.91
3:21.81
4:21.81
5:21.93
6:21.91
7:21.91
8:21.91
9:21.91
Mean:21.90
0:21.91
1:21.81
2:21.81
3:21.91
4:21.91
5:21.91
6:21.91
7:21.97
8:21.83
9:21.81
Mean:21.88
0:21.83
1:21.93
2:21.91
3:21.91
4:21.91
5:21.91
6:21.91
7:21.83
8:21.91
9:21.91
Mean:21.90
0:21.91
1:21.97
2:21.91
3:21.93
4:21.91
5:21.83
6:21.83
7:21.91
8:21.91
9:21.91
Mean:21.90
0:21.93
1:21.97
2:21.91
3:21.79
4:21.81
5:21.91
6:21.91
7:21.91
8:21.91
9:21.91
Mean:21.90
0:21.91
1:21.86
2:21.81
3:21.91
4:21.91
5:21.91
6:21.91
7:21.93
8:21.83
9:21.83
Mean:21.88
0:21.83
1:21.91
2:21.91
3:21.91
4:21.91
5:21.97
6:21.91
7:21.79
8:21.91
9:21.91
Mean:21.90
0:21.93
1:21.97
2:21.91
3:21.91
4:21.91
5:21.81
6:21.91
7:21.91
8:21.91
9:21.91
Mean:21.91
0:21.91
1:21.97
2:21.91
3:21.83
4:21.79
5:21.91
6:21.91
7:21.91
8:21.91
9:21.93
Mean:21.90
The actual value from the ultrasonic sensor feed is around 21 but as you can see, the difference between printing out rangeValue and not is big!
AWOL, thanks for your feedback. Now can you see the difference? Please advice on alternatives to solve, thanks! Im a newbie to arduino.

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! :smiley:

well ,i have put in delay(10) and it still have the same problem. Wonder still have other alternatives to solve my current issue?

Your program doesn't look that big, but whne I see issues like this I think it might be a memory issue.

Then if it's a memory issue, how can i solve it? Pauly, u have ideas to solve?

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!

Good to know it is working - why not put a video in the Exhibition section?

thanks for your help, this module is a small part of a bigger project. Im actually working on it right now and havent finalized yet. =)