04-24-2018 04:39 PM - edited 04-24-2018 04:41 PM
I need to make circular buffer to read value each 100 count by using 2 while the first put the value into array the second read from it.
thanks
04-24-2018 04:46 PM
Can you explain what you mean? (not clear are e.g. the terms: "100 count", "2 while", "first" , "second"??)
What is the size of the circular buffer? (100?) Do you just want to read the oldest before replacing it with a new value? Where does the data come from? What is the purpose?
04-24-2018 05:01 PM
Yes sir first thanks for your answer, i will explain what i want.
I have array with 100 element it's the size of it, the first while loop will count from 1 to inf and the counter value will be add to the array but when it's reach to 100 the data will over flow, so i need the second while loop takes the value from array.
Note the first while loop let's say it's work under 1000 Hz and the second 100 Hz so how can i make it work.
04-24-2018 06:11 PM
Still not clear at all. Can you show us some simplified code?
04-24-2018 06:26 PM
As you can see i have two while loop the first one with delay 1 ms the second loop with 100 ms.
i need to make buffer. but the delay time is different so what should i do.
the array size is 100.
first loop counter will count from 1 to Inf.
the second loop will read the values from array.
hope it's clear this time and thanks
04-24-2018 06:27 PM - last edited on 08-13-2024 03:17 PM by Content Cleaner
@Mohammed_Kandeel wrote:I have array with 100 element it's the size of it, the first while loop will count from 1 to inf and the counter value will be add to the array but when it's reach to 100 the data will over flow, so i need the second while loop takes the value from array.
Note the first while loop let's say it's work under 1000 Hz and the second 100 Hz so how can i make it work.
This sounding a lot like a Producer/Consumer.
04-24-2018 08:22 PM
@Mohammed_Kandeel wrote:
As you can see i have two while loop the first one with delay 1 ms the second loop with 100 ms.
OK, there are two while loops running in parallel (no first or second).
@Mohammed_Kandeel wrote:
first loop counter will count from 1 to Inf.
Integers don't have "Inf".
@Mohammed_Kandeel wrote:i need to make buffer. but the delay time is different so what should i do.
the array size is 100.
Since the loop on the left runs 100x faster, a 100 element buffer will only be sufficient during the first 100ms, then it will start dropping data.
@Mohammed_Kandeel wrote:
the second loop will read the values from array.
What array? Why do you think that "delete from array" is the right way to read an element? Should the second loop read all 100 elements? There still needs to be some synchronization.
Can you step back for a few seconds and instead tell us what you are trying to achieve? Where does the data come from? What should the second loop do with the data? Why do you even have two loops?
04-24-2018 09:37 PM
If you are describing a Producer/Consumer setup, as crossrulz guesses, then you might want to take a look at Channel Wires. These might simplify your understanding of the pattern - alternatively, stick with the Queue as detailed in the already linked White Paper regarding P/C, since these have many more examples available on the forums in questions like this.
A circular buffer usually describes an array of the most recent N points, with the insertion point rotating around the array (or the array rotating around - probably altenbach can tell us which is the fastest but I'd guess the former).
You could use this, but it sounds like what you really want is to stream data from one place to another. For that, the Queue and the Channel Wire are better choices.
04-25-2018 03:02 AM
Is this what you're meaning?
/Y
04-25-2018 06:47 AM - edited 04-25-2018 06:48 AM
Hi all,
sorry if i my explanation didn't get for you.
we have queue and queue is just an array but i know it's size and it's FIFO.
so i ask my self if i need to replace the queue by array what will change. and the data it will be just the value of iteration of while loop.