I'm playing with the EEPROM wear leveling library, with good results so far storing and recovering a single counter from the internal EEPROM at this point based on the provided example (EEWL Example).
My concrete question is: Is possible to store 2 or more counters using this library? If so, how this can be done?
#define BUFFER_LEN 10 // number of data blocks (1 blk = 1 ulong)
#define BUFFER_START 0x10 // EEPROM address where buffer starts
#define BUFFER2_LEN 10 // number of data blocks (1 blk = 1 ulong)
#define BUFFER2_START 0x10 + 40 // EEPROM address where buffer starts
#include "eewl.h"
unsigned long powerCycles = 0;
unsigned long someOtherCycles = 0;
EEWL pC(powerCycles, BUFFER_LEN, BUFFER_START);
EEWL someOtherCount(someOtherCycles, BUFFER2_LEN, BUFFER2_START);
...and so on and so forth up the Yangtze. I'm not sure of the buffer allocations though, I might have made them overlap. I guess the second buffer should begin at 10 x 4 = 40, ten unsigned longs.
AARG:
Thanks for your helpful answer.
I have worked on your example and have turned in to this:
//---------------------------------------------------------
const int MessageDelay = 2500; // Delay between messages in mSeconds
unsigned long LoopCounter = 0; // Loop counter
//---------------------------------------------------------
// Adapted from original code from:
// https://github.com/fabriziop/EEWL
///* .+
//.context : EEWL EEPROM wear level library
//.title : count power up/down cycles
//.kind : c++ source
//.author : Fabrizio Pollastri <mxgbot@gmail.com>
//.site : Revello - Italy
//.creation : 2-Jan-2021
//.copyright : (c) 2021 Fabrizio Pollastri
//.license : GNU Lesser General Public License
//.description
// This application counts the number of power up/down cycles. At each
// power up this counter is read from EEPROM, incremented by one and
// saved again into EEPROM to be preserved accross power cycles.
// A circular buffer len of 10 is defined. This extends the EEPROM life
// 10 times, about 1 million of power cycles.
//
//.- */
//---------------------------------------------------------
// Additional counters:
// https://forum.arduino.cc/t/multiple-counters-using-eewl-library/898907/2
//
#define BUFFER0_LEN 10 // number of data blocks (1 blk = 1 ulong)
#define BUFFER0_START 0x10 // EEPROM address where buffer starts
//
#define BUFFER1_LEN 10 // number of data blocks (1 blk = 1 ulong)
#define BUFFER1_START 0x10 + 40 // EEPROM address where buffer starts
//
#define BUFFER2_LEN 10 // number of data blocks (1 blk = 1 ulong)
#define BUFFER2_START 0x10 + 80 // EEPROM address where buffer starts
//
#include "eewl.h"
// Initialize variables
unsigned long powerCycles = 0;
unsigned long WarningCounts = 0;
unsigned long AlarmCounts = 0;
// Allocate buffers for each counter
EEWL pC(powerCycles, BUFFER0_LEN, BUFFER0_START);
EEWL wC(WarningCounts, BUFFER1_LEN, BUFFER1_START);
EEWL aC(AlarmCounts, BUFFER2_LEN, BUFFER2_START);
//---------------------------------------------------------
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(19200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if (!pC.get(powerCycles))
Serial.println("powerCycles: first time ever");
if (!pC.get(WarningCounts))
Serial.println("WarningCounts: first time ever");
if (!pC.get(AlarmCounts))
Serial.println("AlarmCounts: first time ever");
// increment power cycles counter and save it into EEPROM
powerCycles++;
pC.put(powerCycles);
// Print start message
Serial.println(" ARDUINO RESET");
}
//---------------------------------------------------------
void loop() {
delay(MessageDelay); // Delay between messages in mSeconds
CounterIncrements(); // Increment counters and save it into EEPROM
DisplayCounters(); // Display the current counter values on serial console
LoopCounter++; // Increment loop counter
}
//---------------------------------------------------------
void DisplayCounters () {
// Display the current counter values on serial console
Serial.println("");
Serial.print(" Loop Counter:");
Serial.println(LoopCounter);
Serial.print(" Power cycles:");
Serial.println(powerCycles);
Serial.print(" Warning counts:");
Serial.println(WarningCounts);
Serial.print(" Alarm counts:");
Serial.println(AlarmCounts);
}
//---------------------------------------------------------
void CounterIncrements() {
// Increment counters and save it into EEPROM
WarningCounts++;
wC.put(WarningCounts);
AlarmCounts++;
aC.put(AlarmCounts);
// Wait for data to be totally saved on EEPROM
delay(20);
Serial.println("Data saved on EEPROM.");
}
//---------------------------------------------------------
However, I have noted the following issues:
Only the powerCycles counter is updated correctly upon board reset.
The counters incremented outside the setup() function are nor consistent across board resetting. (The goal here is keep a couple of event counters in memory across board resetting).
I have scratching my head the past week with this without finding a solution, so any help would be welcomed.
TIA.
After leaving the device running for several minutes while playing with the reset button, I have realized the following situation:
Both counters WarningCounts and AlarmCounts are starting upon reset from the powerCycles counter value, so every "powerCycle" increment that counter and the others counters are starting from that value.