serial input basic with smoothing averaging data

greetings,

i am using this coding about serial input

const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data

boolean newData = false;

void setup() {
 Serial.begin(2400);
 Serial.println("<Arduino is ready>");
}

void loop() {
 recvWithEndMarker();
 showNewData();
}

void recvWithEndMarker() {
 static byte ndx = 0;
 char endMarker = '\n';
 char rc;
 
 // if (Serial.available() > 0) {
           while (Serial.available() > 0 && newData == false) {
 rc = Serial.read();

 if (rc != endMarker) {
 receivedChars[ndx] = rc;
 ndx++;
 if (ndx >= numChars) {
 ndx = numChars - 1;
 }
 }
 else {
 receivedChars[ndx] = '\0'; // terminate the string
 ndx = 0;
 newData = true;
 }
 }
}

void showNewData() {
 if (newData == true) {
 Serial.print("This just in ... ");
 Serial.println(receivedChars);
 newData = false;
 }
}

but i want to smoothing the data.

would you guys help me?

Posting the same question multiple times will not necessarily help you in getting answers.

JaBa:
Posting the same question multiple times will not necessarily help you in getting answers.

Are you sure? This poster has only ever made one post on these forums - the one above.

@ogydavinci, what do you mean by “smoothing the data”?
A bit more information might help.

And I’ve given you your first Karma point for being one of the very few people who post within code tags in their first post. Well done. :slight_smile:

This Thread has a similar question.

I will repeat what I said in that Thread

When you receive a line you can convert it from a string of characters to a float (see the parse example) and save the value in an array with enough elements for the number you want to average. When you have all the numbers you can calculate the average.

In this Thread the OP (@ogydavinci) has not told us what sort of data he wants to smooth.

...R

The smoothing idea - saving all the data in an array , then getting an average by adding them up and diving by the number of data points is ok, but you're very limited in the amount you can store in arduino.

A trick I've used is to calculate a running average over a number of points in a long datastream

is as below

ave0 += L0val; ave0 *= (1 - 1 / sampleLen); ave1 = ave0 / (sampleLen - 1);

ave0 and ave1 are floats, samplelen an int and L0val anything - use a cast if you must

L0val contains the incoming data, one piece at a time, sampleLen is the number of samples over which to average,and ave1 is the output smoothed value.

regards

Allan

okay first of all,

here is the data receive at serial monitor after applying that coding.

This just in ... 0000.0000010

This just in ... 0000.5000010

This just in ... 0000.6000010

This just in ... 0000.5000010

This just in ... 0000.7000010

This just in ... 0000.0000010

using this data. i want it to be store in array about 10 data. an calculate it for averaging an display back the output in serial monitor.

If you’re happy with just 10 data points averaging , the arduino would handle that.

try
int i,j ; // initialise to 0 in setup
float inputval;
float indata[10]};
float average;

// part of loop - run every time you geta new data point

indata = inputval;

  • i++;*
  • if (i>=10) i=0;*
  • average = 0;*
  • for (j=0;j<10;j++){*
  • average += indata[j];*
  • }*
  • average = average/10;*
    should work - but I think my way is neater
    regards
    Allan.

Robin2: This Thread has a similar question.

. . . and a very identical IP address. I smell something fishy, and I'm not talking about the contents of Baldrick's apple crumble.

Don't understand - I'm just trying to help the guy..

regards

Allan.

Well congratulations ogydavinci, you got your Karma point in this thread by cheating. Cross-posting by creating a second username is low.

@JaBa, I wish you'd given more info like a link when you said that this person had posted multiple times. I looked at the post count and figured that you were mistaken. My error, but I won't make another by trying to help. Pity there's no way to remove Karma points. :(

allanhurst: Don't understand - I'm just trying to help the guy.. regards Allan.

I wouldn't help him now if his life depended on it.

And you should know better than to post code without code tags.

OldSteve: Pity there's no way to remove Karma points. :(

Even moderators can't remove karma. User accounts, posts, even entire threads, on the other hand . . .

AWOL: Even moderators can't remove karma. User accounts, on the other hand . . .

:D

JaBa: Posting the same question multiple times will not necessarily help you in getting answers.

we see this every year.

the instruor give an assignment and 3 guys post the questions 3 ways.