Adding data to array

Hi, I use ultrasonic sensor to calculate distance to a target plate. I use temperature sensor to calculate air speed as function of the temperature. On the serial screen I see that the air speed is change from reading to reading (I sample every second). The distance reading is changing while the target plate is stationary, air temp is stable. I want to enter the readings of the distance into array of 1000 readings. I have two sensors, therefore the array should be 2X1000, Then calculate the average of the array data and use the average distance for other actions. Every time a new data is entered it should shift the others, remove the one in the 1000 place and accept the new data.
I need the code how to do so.
Thnx

Which Arduino are you using?

Hello DanielMeidanToronto

Take this examples and start your own studies.

... that's going to use quite a lot of memory. What Arduino do you have? Do you really need 1000 readings to get an average?

consider an an Arduino Mega which has 8K of SRAM
I have stored ADC readings in a 10000 element array using a Arduino Due

Based on what you said, you don't need an array. You can get a similar effect using a low pass filter.

Do you want someone to write this code for you? If so, I can move your topic to a section of the forum where you can offer money for someone to write it for you.

No, that's the Wrong WayTM to go about it.
Moving data around wastes memory bandwidth and processor cycles.
Leave the data where it is, and move the pointers to it.

a moving average will do similar without the need of an array with 1000 fields.

No, a moving average requires you know the oldest and newest sample values, so will require a buffer

If you reduce the arrays to 100 entries...

Assuming the values are stored as "float"... each one will take 4 bytes (32 bits). So 200 x 4 = 800 bytes. The Nano has 2KB of SRAM, so should be able to handle this. Really depends what else you are doing.

Don't do this... it is very inefficient. Just keep track of where in your buffer the newest and oldest entries are.

A simple or weighted moving average does require old samples, but an exponential moving average does not (it requires only the current moving average and the current sample).

Hi
I would like to declare an array as follows:
int d=20;
flowat mydata[d];
arduino does not accept the length of the array as variable.
How to achieve this?
Thnx

The easiest thing for a newbie is to create an array that's as large as you're ever going to need. Then only use the first 'd' elements of it.

If d doesn't change, then

const int d=20;

You can't easily declare an array that doesn't have a fixed length... the compiler needs to know how much memory to reserve.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.