FIFO buffer

Hi, i’m need to store samples on a FIFO buffer. These samples are costituited of two variables:

#define n_samples 100
pressure[n_samples];
elepsed_time[n_samplse];

i need to store pressure and the elapsed time from first element of array ( elepsed_time[0]);
i call a function for read pressure and time.

My fifo buffer is this:

elapsed_time[0] = elapsed_time[1];
for(int c=1; c<n_samples-1; c++){
pressure[c] = pressure[c+1];
elapsed_time[c] = elapsed_time[c+1]-elapsed_time[0];
 
}
pressure[n_samples-1] = read_pressure();
elepsed_time[n_samples-1] = millis()- elepsed_time[0];

in this mode i could have a bidimensional array of “n_samplesx2” elements.

if i sent for example elepsed_time[3]-elepsed_time[8] (Serial.Write(elepsed_time[3]-elepsed_time[8],0)) it could be constant (more or less), instaed this difference is growing!

Does enyone notice the problem?
Thanks!

fifo buffer is a queue, as such you need to implement a simple to queue with simple methods: add(), extract(), ...is_empty()

What are you doing now is not making any sense or related to the "fifo buffer" at all.

My task is this: calculate the climb rate.
I read the pressure and time: in array of pressure i put the new pressure in the last cell. the pressure in the first cell will lost! This is "fifo" method!
The problem isn't in the buffer but in the elapsed_time array......

Matteo1991:
Does enyone notice the problem?

Yes, you do not supply data types, you post partial code, your question is unrelated to FIFO’s and what you are trying to achieve is only vaguely described, so…

All your readings are calculated as “millis() - elapsed_time[0]” - as soon as your for-loop moves all entries one entry backwards, the remaining readings will no longer have their dependency and this will cause the math to go haywire. Instead you should just store the time of the reading:

for (int i = 0; i < n_samples - 1; i++)
{
  pressure[i] = pressure[i+1];
  sample_time[i] = sample_time[i+1];
}
pressure[n_samples-1] = read_sample();
sample_time[n_samples-1] = millis();

This should have the effect that “sample_time[3]-sample_time[8]” is more or less constant. Oh, and your task just screams for a circular array of struct! :wink:

EDIT: LOL, PaulS - stuct is what OP was, error corrected :wink:

Oh, and your task just screams for a circular array of stuct!

Or struct. 8)

Danois90:

sample_time[n_samples-1] = millis();

No, don't need this!

i need the elapsed time from first sample (elapsed_time[0]) to courrent samples!

yies, now i also think rhat this is a cicrular array. Clarified this, what is the solution?

i know that millis() functiont is't a lot of correct for my application, but, this is the kernel of my problem!

Matteo1991:
No, don't need this!

i need the elapsed time from first sample (elapsed_time[0]) to courrent samples!

You need this because it is the correct way of solving your task, the rest is just a matter of how to crunch the numbers. If you want the elapsed time from x[0] to x[10], you just use " elapsed = sample_time[10] - sample_time[0] ".