arduinoeeprom

ESP32 - Preferences.h not writing to EEPROM


I have the following code: (i know that i can create a for loop, i removed it just for debugging..)

#include <Preferences.h>
Preferences preferences;

String highscoreNames[10];
double highscoreScores[10];
bool savescore;

void setup(void) {
preferences.begin("highscores", false);

highscoreNames[0] = preferences.getString("name0", "--");
highscoreScores[0] = preferences.getDouble("score0", 0);
highscoreNames[1] = preferences.getString("name1", "--");
highscoreScores[1] = preferences.getDouble("score1", 0);
highscoreNames[2] = preferences.getString("name2", "--");
highscoreScores[2] = preferences.getDouble("score2", 0);
highscoreNames[3] = preferences.getString("name3", "--");
highscoreScores[3] = preferences.getDouble("score3", 0);
highscoreNames[4] = preferences.getString("name4", "--");
highscoreScores[4] = preferences.getDouble("score4", 0);
highscoreNames[5] = preferences.getString("name5", "--");
highscoreScores[5] = preferences.getDouble("score5", 0);
highscoreNames[6] = preferences.getString("name6", "--");
highscoreScores[6] = preferences.getDouble("score6", 0);
highscoreNames[7] = preferences.getString("name7", "--");
highscoreScores[7] = preferences.getDouble("score7", 0);
highscoreNames[8] = preferences.getString("name8", "--");
highscoreScores[8] = preferences.getDouble("score8", 0);
highscoreNames[9] = preferences.getString("name9", "--");
highscoreScores[9] = preferences.getDouble("score9", 0);
preferences.end();
}


void loop(void) {

 if (savescore== true)
  {

    Serial.println("highscore changed");
    Serial.println(highscoreScores[0]);
    preferences.begin("highscores", false);
    
      preferences.putString("name0", highscoreNames[0]);
      preferences.putDouble("score0", highscoreScores[0]);
      preferences.putString("name1", highscoreNames[1]);
      preferences.putDouble("score1", highscoreScores[1]);
      preferences.putString("name2", highscoreNames[2]);
      preferences.putDouble("score2", highscoreScores[2]);
      preferences.putString("name3", highscoreNames[3]);
      preferences.putDouble("score3", highscoreScores[3]);
      preferences.putString("name4", highscoreNames[4]);
      preferences.putDouble("score4", highscoreScores[4]);
      preferences.putString("name5", highscoreNames[5]);
      preferences.putDouble("score5", highscoreScores[5]);
      preferences.putString("name6", highscoreNames[6]);
      preferences.putDouble("score6", highscoreScores[6]);
      preferences.putString("name7", highscoreNames[7]);
      preferences.putDouble("score7", highscoreScores[7]);
      preferences.putString("name8", highscoreNames[8]);
      preferences.putDouble("score8", highscoreScores[8]);
      preferences.putString("name9", highscoreNames[9]);
      preferences.putDouble("score9", highscoreScores[9]);
      preferences.end();
      savescore = false;
  }
}

I only posted the relevant code. the array has values in it, and if i set the write variable to ture, the values should be written to eeprom. I expect that if I unplug the power and plug it back after initalization the array should have the same values it had the last time it wrote to the eeprom. Unfortunately that is not the case. the whole array is initialized at 0.

what is wrong with my code?


Solution

  • The code was correct. the problem was that the board was broken. a new board worked just fine with the same code.