The ESP has no real EEPROM. Instead the EEPROM
class for ESPs uses the flash memory. There is a difference in the implementations of this class between Arduinos and ESPs:
On an ESP every data, that you write, will first be placed in a buffer in RAM. You need to call EEPROM.commit()
to actually write that data to the underlying flash memory.
So you need to add
EEPROM.commit();
to the end of your EEPROM_Write()
function, or the data will stay in RAM and thus be lost on power cycle or reset.
I think your problem (besides the commit) lies not in your EEPROM_Write()
function, but in the EEPROM_Read()
function. If it doesn't work correctly, you are just not able to see, if the data gets written correctly. In your code (which can be seen in the edit history), you have this function:
void EEPROM_Read(){
//load array pwmarray1 with stored values in EPROM
for (int x = 0; x < pwmarray1Size; x++){
EEPROM.get(EEPROMStartAdress + (x * sizeof pwmarray1[0]), pwmarray1[x]);
Serial.print("loading program from EPROM: ");
Serial.println(pwmarray1[x]);
}
}
While in EEPROM_Write()
you are calling EEPROM.begin(280)
every time, you don't EEPROM.end()
it there and you are also not begin the EEPROM in the EEPROM_Read()
function. I'm not sure about the underlying implementation, but it started to work for me, when I began and ended the EEPROM
object in both functions.
I've written a test code, that I composed party of your code and that for me correctly saves and reads an array via the EEPROM
object into the flash memory. It keeps the data over resets and power cycles just correctly. You can control it via Serial Monitor. Just type c
(for clearing the array to zero), r
(for reading the array from EEPROM/flash) or i
(for incrementing all array elements and save them to EEPROM/flash).
#include <EEPROM.h>
#define EEPROM_SIZE 280
int array[60] = {0};
const unsigned arraySize = sizeof array / sizeof array[0];
#define EEPROMStartAdress 0
void setup(){
Serial.begin(115200);
Serial.println("///////////////////////////////////////");
delay(2000);
}
void loop(){
if(Serial.available()){
char c = Serial.read();
switch(c){
case 'c':
for(uint8_t i=0;i<arraySize;i++) array[i] = 0;
EEPROM_Write();
Serial.println("EEPROM RESET");
break;
case 'r':
EEPROM_Read();
break;
case 'i':
for(uint8_t i=0;i<arraySize;i++) array[i] += 1;
EEPROM_Write();
break;
}
}
}
void EEPROM_Write(){
EEPROM.begin(EEPROM_SIZE);
// store values in EEPROM
for (int i = 0; i < arraySize; i++)
{
EEPROM.put( EEPROMStartAdress + (i * sizeof array[0]), array[i]);
}
EEPROM.commit();
EEPROM.end();
}
void EEPROM_Read(){
EEPROM.begin(EEPROM_SIZE);
for (int x = 0; x < arraySize; x++){
EEPROM.get(EEPROMStartAdress + (x * sizeof array[0]), array[x]);
Serial.print(" ");
Serial.print(array[x]);
}
Serial.println();
EEPROM.end();
}
Removing begin()
and end()
from EEPROM_Read()
and also end()
from EEPROM_Write()
gives me the same results, that you described as your problem.