ESP32 - Preferences.h not writing to EEPROM



我有以下代码:(我知道我可以创建一个for循环,我删除它只是为了调试。(

#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;
}
}

我只发布了相关代码。数组中有值,如果我将写变量设置为ture,则应该将这些值写入eeprom。我预计,如果我在初始化后拔下电源并插回,数组应该与上次写入eeprom时的值相同。不幸的是,事实并非如此。整个数组初始化为0。

我的代码出了什么问题?

代码是正确的。问题是木板坏了。一个新的董事会用同样的代码运作得很好。

相关内容

  • 没有找到相关文章

最新更新