I am fairly new to Arduino, and I’m playing around with gathering data from a sensor. What I want to do is create some form of a time-based array, that will delete entries after a period of time.
I am unsure on how to implement this in Arduino. My thought was to make a class and have one of the attributes be the time the data entry was created, obtain from millis(), and then in every iteration check to see if current_time-time_created>threshold_time. However, this seems like a little of hassle and significant time consumption to go through the array every loop. Since speed is important for my codes application, I wanted to see if anyone in the community knows of a better approach to this problem.
Why delete the entries ? Put the data and time recorded in an array of structs and when you come to use the data ignore those that are too old.
Use a circular array (I'm sure Mr. Google can help you with that). Overwrite the oldest entities.
The trick with arrays is not to move the data once it is written. If you want 1000 measurements per second and you have a 1000 element array then moving all the elemenys by one space when you add a new measurement will require millions of operations per second.
Just keep a pointer to the next available space. This is just an array index, like 0 to 999. When it runs off the end you move it back to the beginning. That is what makes it circular.
Normally you would also keep a pointer to the oldest entry but if you are recording times then you may not need that. Just check the time on the element about to be overwritten to make sure it is old enough to be erased. If it isn't then your buffer is full to overflowing.