EEPROM storage no longer working

Something very weird! Had my code working fine logging ADC values to EEPROM. Then I read about backup RAM and thought “Hey, another 4k locations I can use for logging”. This meant I could save info at 4 minute intervals instead of 10 minutes (over a 112 hour total period). So I changed my code to have
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));

retained byte readings[3000];

as the first two lines of code. I adapted the rest of the code to save the first 2k samples in the EEPROM, then the next 3k in the backup RAM. That’s when the problems started. I have a routine which sets all the EEPROM values to 255 (EEPROM.clear()) which works fine. As soon as I call my recording routine (not changed from the original working code) all the EEPROM locations are set to zero apart from the first three locations which correctly hold day of the week, hour and minute of starting logging. Here is the recording function:

void DoRecord() {
    // calculate the average voltage over a 4 minute period sampling at once per 1 seconds. Add up 240 samples then divide by 240.
    long dyn, bat, lt, fld, x = 0;
    long accdyn, accbat, acclt, accfld = 0;
    int EEpointer = 3;
    delay(2000);                            // 3 second startup delay to release button
    EEPROM.put(0, CurrentWeekday);          // 1 byte for start weekday
    EEPROM.put(1, CurrentHour);             // 1 byte for start hour
    EEPROM.put(2, CurrentMinute);           // 1 byte for start minute
    while(EEpointer < 2043) {
    	while (digitalRead(logpb) == LOW) {
	        digitalWrite(7, 0);
	        digitalWrite(6, 0);
            for (int x = 0; x < 240; x++) {      // take lots of readings and accumulate same
    	        digitalWrite(6, 0);
                dyn = (analogRead(10)-540)/8;    // 8-bits only for now, 39V FSD
                if (dyn < 1) dyn = 0;
                delay(200);
                bat = (analogRead(11)-540)/8;
                if (bat < 1) bat = 0;
                delay(200);
                lt = (analogRead(12)-540)/8;
                if (lt < 1) lt = 0;
                delay(200);
                accdyn = accdyn + dyn;
                accbat = accbat + bat;
                acclt = acclt + lt;
                digitalWrite(6, 1);             
                delay(400);
    // check to see if the record button has been pressed again
        	    if (digitalRead(logpb) == HIGH) {
        	        digitalWrite(7, 1);
        	        delay(2000);
                    return; 
                }
            }    
        } 
        EEPROM.put(EEpointer, accdyn/240);        // store data in EEPROM once every 10 minutes  
        EEPROM.put(EEpointer + 1, accbat/240);    // then reset accumulators to zero
        EEPROM.put(EEpointer + 2, acclt/240);  
        EEpointer = EEpointer + 3;
        accdyn = 0;
        accbat = 0;
        acclt = 0;
        accfld = 0;
        if (EEpointer > 2043) {                     // approx 680 readings @ 4 mins = 45hrs
            return;
        }
    }
}

This was followed by the backup RAM logging routine which is just the same apart from the location of the storage.

Why, when using previously working code, does it no longer work?
Also, why am I not allowed to use all 4k of the backup RAM for storage - I get compile overflow error messages if I try to use more than 3k of my Photon’s backup RAM.

I need to use EEPROM as there will be a period after logging when the Photon will be unpowered before the data can be downloaded (railway carriage voltage logging). I have a coin cell connected to Vbat to enable me to use the extra backup RAM. I should also say I removed the START coding statement and all references to the “readings” array, but still the problem persists. Zeros all through the EEPROM unless the Clear routine is called. Then 255’s everywhere. Call the record routine - instant zeros. Or that’s how it looked to me - however, on closer inspection, it was an integer (4 bytes) filling up EEPROM locations. I didn’t expect that. I also discovered a typo which stopped the code counting up correctly, so that is now fixed. I will try to mark this thread as fixed. Sorry for the inconvenience caused.

@gotopcs, which version of system firmware are you using? The latest 0.6.0-rc.1 and later reduces the retained SRAM to 3068 bytes. This would explain the errors you are seeing when you try to use 4K. I updated the docs but the changes haven’t been pushed yet.

1 Like

I’m using 0.5.3 - maybe I should upgrade ?!?

Now I’ve re-read the docs on EEPROM I can see why put() was confusing me!
All OK and working now.

1 Like