# How can I do a FIFO Register?

Hello everyone,

I need to realize a FIFO like register that stores progressively the last 512 values from an analog read on the port A1.
For simplicity, this 512 elements vector will be named A.
At the same time, in parallel, the system must extract the last 128 samples from A and create the vector B.

In this moment I want to have: A∈R(1,512) and B∈R(1,128).

Each new sample I need to extract A and B, because I have to do the average of both of them.

Thank you for the help.

All the bests,
Giovanni

Which Arduino board are you using ?
Memory may be the limiting factor

Dear UKHeliBob,

I'm using an MKR Vidor 4000. In this moment I need help with the code since I can consider reducing the buffer.

All the bests,
Giovanni

Where are you stuck ?

Assuming that the readings are going to be saved in an array of ints have you managed to do this as a first step ?

Dear UKHeliBob,

I know how to create a "static" int array from the analogread(A1), but the resulting vector is not refreshable with the recent 512 values, once a new sample occurs.

I know that this is a simple queue, but I'm not capable to do this on Arduino. What a shame !

Giovanni

Is a circular buffer q.v. what you're looking for?

Hi wildbill,

wildbill:
Is a circular buffer q.v. what you’re looking for?

Yes, I think I need two parallel circular buffers or if there is a simple way to do a running average if it can be considered simpler.

Giovanni

I know how to create a "static" int array from the analogread(A1), but the resulting vector is not refreshable with the recent 512 values

Use a variable as the index to the array and increment it each time you add a value to the array. When the index reaches 512 reset it to zero. The array will contain the most recent 512 values

UKHeliBob:
Use a variable as the index to the array and increment it each time you add a value to the array. When the index reaches 512 reset it to zero. The array will contain the most recent 512 values

Yes, ofc but it doesn't permit me to progressively update the vector once a new sample arrives.

I try to simplify with an example:

Step 1:
A=[0 1 3 2 1 9 7];
B=[1 9 7];

average(A)
average(B)

new sample x from sensor:

A=[1 3 2 1 9 7 x]
B=[9 7 x]

average(A)
average(B)

new sample x1 from sensor:

A=[3 2 1 9 7 x x1]
B=[7 x x1]

average(A)
average(B)

and so on...

Giovanni

Declare A as an array of 512 ints
Set the index variable to 0