Read serial data into array for smoothing filter

i’m newbie and trying to smoothing my data…
the data in 14byte with cr n nl…
pls help me
for exm= 0000.1000010

0000.2000010
0001.1000010
0000.1000010
0000.1000010
0000.1000010
0000.1000010
0000.1000010

i’m refer to this code
/*

Smoothing

Reads repeatedly from an analog input, calculating a running average
and printing it to the computer. Keeps ten readings in an array and
continually averages them.

The circuit:

  • Analog sensor (potentiometer will do) attached to analog input 0

Created 22 April 2007
By David A. Mellis dam@mellis.org
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

*/

// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input
char readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

int inputPin = A0;

void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}

void loop() {
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;

// if we’re at the end of the array…
if (readIndex >= numReadings) {
// …wrap around to the beginning:
readIndex = 0;
}

// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(500); // delay in between reads for stability
}

Ok, you are new. You want to smooth your data. You have a sample that does exactly that. That much we understand. What you haven't stated is what you Need help on.

What part of the sketch do you not understand? Where do you Need help?

What is the data?

At first look I thought that you were giving us some binary representation on 4 bits and 7 bits but then spotted a 2...

0000.[color=red][b]2[/b][/color]000010
0001.1000010
0000.1000010
0000.1000010
0000.1000010
0000.1000010
0000.1000010

If you want to smooth ASCII strings it's not the same as numbers...

Have a look at the examples in Serial Input Basics

I presume your examples are floating point numbers (but you have not said).

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.

...R

firstly…i’m using rs232 to receive data from ludlum 375(it kind of area radiation monitoring).

i need to smoothing the data and transmit back to a led monitoring.

i don’t know how to add the data into array and calculate it.

to read the data…

i’m using

#include <SoftwareSerial.h>

SoftwareSerial txSerial(10, 11); // RX, TX
char inByte =0;
void setup() {
  Serial.begin(2400);
  txSerial.begin(2400);

}
void loop() { // run over and over
  if (Serial.available()) {
     inByte = Serial.read();
      Serial.println(inByte);
     // txSerial.println(inByte);
  
  }
}

The RS-232 data includes the current radiation readings and the current condition of the status lights

it seems according to the doc that you want only the xxxx.x first bytes for the reading (According to the doc you set a deep switch to select either the 0.1 unit and you get 0xxx.x relevant data or set up the 1 unit and get xxxx.0 data)

The data is sent every 2 seconds as ASCII bytes apparently

So you need to read a full frame till \r\n - see robin suggestion for that - the first one might be incomplete If you connect at the wrong time - so ensure you got 14 bytes with the line termination. Then build up a float or int (see atoi() function for example) with bytes 0,1,2,3 and 5 (byte 4 should be a dot). And use that in the smoothing algorithm

J-M-L

u seen familiar with radiation monitoring…
this output for single chac from robin

This just in … 0
This just in … 0
This just in … 0
This just in … 0
This just in … .
This just in … 8
This just in … 0
This just in … 0
This just in … 0
This just in … 0
This just in … 1
This just in … 0
This just in …

This just in …

i need to store the data in the array and using smoothing algo to get the average…
the prob is i’m dun know how to store in the array for only 6 fisrt byte for exam

0000.5 and store into array…do i need to change the format and cal before transmit??

Different username, same IP address, I hear. Very clever. :( serial input basic with smoothing averaging data

zamashraff:
J-M-L

u seen familiar with radiation monitoring…
this output for single chac from robin

This just in … 0
This just in … 0
This just in … 0
This just in … 0
This just in … .
This just in … 8
This just in … 0
This just in … 0
This just in … 0
This just in … 0
This just in … 1
This just in … 0
This just in …

This just in …

i need to store the data in the array and using smoothing algo to get the average…
the prob is i’m dun know how to store in the array for only 6 fisrt byte for exam

0000.5 and store into array…do i need to change the format and cal before transmit??

As I just read in the doc (not familiar with your unit but understands what it does) you set a deep switch to select either the 0.1 unit and you get 0xxx.x relevant data or set up the 1 unit and get xxxx.0 data.

You need a working array of 5 or 6 chars depending on your deep switches in which you put the bytes you receive at the beginning of the line → that is the working array will hold your bytes x1 x2 x3 ‘.’ x5 ‘\0’ or x0 x1 x2 x3 ‘\0’ once you read the full line

Then call atof() if you have the floating value or atoi() if you have the integer one (personally I would get rid of the dot and just deal with an int 10 times larger and adjust later if needed to print out) to get the number you are interested in.

Then check OldSteve reference to average the data.

zamashraff:
this output for single chac from robin

This just in … 0

Why are you reading single characters?

I thought (from your Original Post) that a single number is (for example) 0000.2000010
If so, why are you not reading all the characters until you get a newline character? (i.e the 2nd example in Serial Input Basics)

…R