i suppose, bc they written right values in EEPROM with working sketch and then bad sketch just reads right values from EEPROM, so seems everything works.
Here is an example using .put() and .get() with the names of the struct arrays as proposed.
EDIT: This is not correct. It does not appear to read the data if the write is commented out of the sketch and it is reloaded.
#include <EEPROM.h>
struct highscore {
byte attempts;
char name[4];
};
struct highscore highScores[5];
struct highscore dftHighScores[5] = {
{10, "AAA"},
{20, "BBB"},
{30, "CCC"},
{40, "DDD"},
{50, "EEE"}
};
//this will get filled by the data from .get()
struct highscore read_dftHighScores[5] = {
{},
{},
{},
{},
{}
};
void setup() {
Serial.begin(9600);
delay(2000);
// Setup Default High Scores
Serial.println("Setup Default HighScores");
writeHighScore(dftHighScores);
//Read BackHigh Scoores
Serial.println("ReadBack Default HighScores");
readHighScore(read_dftHighScores);
}
void loop() {}
void readHighScore(highscore read_dftHighScores[])
{
EEPROM.get(50, read_dftHighScores);
delay(250);
for (int i = 0; i < 5; i++)
{
HighScoreLog(read_dftHighScores[i]);
Serial.println("+++++++++++++++++++++++");
}
}
void writeHighScore(highscore writeScore[])
{
for (int i = 0; i < 5; i++)
{
HighScoreLog(writeScore[i]);
Serial.println("+++++++++++++++++++++++");
}
EEPROM.put(50, writeScore);
delay(250);
}
void HighScoreLog(struct highscore temp)
{
Serial.println();
Serial.print(" Name = ");
Serial.println(temp.name);
Serial.print(" Attempts = ");
Serial.println(temp.attempts);
}
Output:
16:14:59.047 -> Setup Default HighScores
16:14:59.047 ->
16:14:59.047 -> Name = AAA
16:14:59.093 -> Attempts = 10
16:14:59.093 -> +++++++++++++++++++++++
16:14:59.139 ->
16:14:59.139 -> Name = BBB
16:14:59.139 -> Attempts = 20
16:14:59.139 -> +++++++++++++++++++++++
16:14:59.186 ->
16:14:59.186 -> Name = CCC
16:14:59.186 -> Attempts = 30
16:14:59.233 -> +++++++++++++++++++++++
16:14:59.233 ->
16:14:59.233 -> Name = DDD
16:14:59.233 -> Attempts = 40
16:14:59.279 -> +++++++++++++++++++++++
16:14:59.279 ->
16:14:59.279 -> Name = EEE
16:14:59.320 -> Attempts = 50
16:14:59.320 -> +++++++++++++++++++++++
16:14:59.552 -> ReadBack Default HighScores
16:14:59.784 ->
16:14:59.784 -> Name = AAA
16:14:59.830 -> Attempts = 10
16:14:59.830 -> +++++++++++++++++++++++
16:14:59.876 ->
16:14:59.876 -> Name = BBB
16:14:59.876 -> Attempts = 20
16:14:59.876 -> +++++++++++++++++++++++
16:14:59.922 ->
16:14:59.922 -> Name = CCC
16:14:59.922 -> Attempts = 30
16:14:59.922 -> +++++++++++++++++++++++
16:14:59.967 ->
16:14:59.967 -> Name = DDD
16:14:59.967 -> Attempts = 40
16:15:00.014 -> +++++++++++++++++++++++
16:15:00.014 ->
16:15:00.014 -> Name = EEE
16:15:00.014 -> Attempts = 50
16:15:00.060 -> +++++++++++++++++++++++
output is fine, but still not working. read_dftHighScores still empty
#include <EEPROM.h>
struct highscore {
byte attempts;
char name[4];
};
// highscore highScores[5];
highscore dftHighScores[5] = {
{10, "AAA"},
{20, "BBB"},
{30, "CCC"},
{40, "DDD"},
{50, "EEE"}
};
//this will get filled by the data from .get()
highscore read_dftHighScores[5] = { {}, {}, {}, {}, {}};
void setup() {
Serial.begin(9600);
writeHighScore(dftHighScores); // Setup Default High Scores
//Read BackHigh Scoores
Serial.println("ReadBack Default HighScores");
readHighScore(read_dftHighScores);
for (int i = 0; i < 5; i++) HighScoreLog(read_dftHighScores[i]);
}
void loop() {}
void readHighScore(highscore read_dftHighScores[])
{
EEPROM.get(50, read_dftHighScores);
delay(250);
}
void writeHighScore(highscore writeScore[])
{
Serial.println("++++++++++++");
EEPROM.put(50, writeScore);
delay(250);
}
void HighScoreLog( highscore temp)
{
Serial.print(" Name = ");
Serial.print(temp.name);
Serial.print(" Attempts = ");
Serial.println(temp.attempts);
}
BUT ! it works when
void readHighScore(highscore read_d[]) // read_d disappear after return
{
EEPROM.get(50, read_d);
delay(250);
for (int i = 0; i < 5; i++)
{
HighScoreLog(read_dftHighScores[i]=read_d[i]); // << ---------- so we write temporary to global and print out
Serial.println("******************");
}
}
I think you are right when I was testing on my hardware I only tested the bad never the good. So for others, the second run of the bad only read never wrote cause the flag was set and good data was already in eeprom.
That solves that mystery now too!
Nice detective work.