Don't give up !
If you could not find examples on how to deal with an array of structures and implement a "circular array" here is something for you to study.
I simplified the structure to make it obvious you record 3 data points (called tempSensor1, tempSensor2, tempSensor3) in the structure.
The code basically defines the structure and an array with maxCycleCount entries (10 in the example) and you have a couple functions
- one for adding a new entry in the array
- one for printing an entry
- one for printing the full array
the main loop just adds an entry with 3 random values and print the full array.
The secret sauce is to know what to print. Until the array is not full, you just need to print from index 0 (the start of the array) to the last entry that was filled to see all the samples but once the array has been entirely filled in then as we overwrite the oldest entry when recording a new one, the start of the array (the oldest value) is no longer at position 0 but it evolves.
have a look at the code and run the simulation and let me know if you have questions.
click to see the code
struct BoilerData {
int16_t tempSensor1;
int16_t tempSensor2;
int16_t tempSensor3;
};
constexpr size_t maxCycleCount = 10;
BoilerData historicalData[maxCycleCount];
size_t nextHistoricalEntry = 0;
bool HistoricalDataIsFull = false;
void addHistoricalData(int16_t sensor1, int16_t sensor2, int16_t sensor3) {
historicalData[nextHistoricalEntry].tempSensor1 = sensor1;
historicalData[nextHistoricalEntry].tempSensor2 = sensor2;
historicalData[nextHistoricalEntry].tempSensor3 = sensor3;
nextHistoricalEntry = (nextHistoricalEntry + 1) % maxCycleCount; // Move to the next index, cycling if necessary
if (nextHistoricalEntry == 0) HistoricalDataIsFull = true; // If nextHistoricalEntry is back to 0, then we kknow the array is full
}
void printHistoricalDataAtIndex(size_t idx) {
Serial.print(idx + 1); // for display start numbering at 1 (in C++ arrays starts at index 0)
Serial.write('\t');
Serial.print(historicalData[idx].tempSensor1);
Serial.write('\t');
Serial.print(historicalData[idx].tempSensor2);
Serial.write('\t');
Serial.println(historicalData[idx].tempSensor3);
}
void printHistoricalData() {
Serial.println(F("\n-----------------------------"));
// print the number of valid samples
if (HistoricalDataIsFull) Serial.print(maxCycleCount);
else Serial.print(nextHistoricalEntry);
Serial.println(F(" Historical Data sample(s)"));
Serial.println(F("-----------------------------"));
Serial.println(F("#\tsensor1\tsensor2\tsensor3"));
if (HistoricalDataIsFull) {
// the array has been fill up so we have to go through all entries
// because we use a circular buffer, the oldest entry is no longer at index 0
// but it is at nextHistoricalEntry (the one we should overwrite)
// so this is our start index, and we want to print maxCycleCount entries
// we use modulo so that our index stays within the bounds of our array
for (size_t i = nextHistoricalEntry; i < nextHistoricalEntry + maxCycleCount; ++i) {
printHistoricalDataAtIndex(i % maxCycleCount); // Ensure the index stays within bounds
}
} else {
for (size_t i = 0; i < nextHistoricalEntry; ++i) {
// the array is not full yet, so we print samples from index 0 to nextHistoricalEntry (not included)
printHistoricalDataAtIndex(i);
}
}
}
void setup() {
Serial.begin(115200);
}
void loop() {
addHistoricalData(random(200), random(200), random(200)); // add a new random entrty
printHistoricalData();
delay(1000);
}