I am new to C++ and Arduino coding and I am struggling to figure out why one version of my code works but the other doesn't
I am trying to do simple test of writing a defined array of custom structure to EEPROM then reading it back again. (This is part of game I am writing but I have split this out to debug)
In this code version of the code I apply the EEPROM.put and EEPROM.get all in the setup function and it works as expected.
#include <EEPROM.h>
#define DEBUG 1;
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"}
};
int initFlag = 0;
int eeAddressFlag = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
EEPROM.get(eeAddressFlag, initFlag);
#ifdef DEBUG
Serial.print(" flag = ");
Serial.println(initFlag);
#endif
if (initFlag != 5) {
EEPROM.put(eeAddressFlag, 5);
// Setup Default High Scores
Serial.println("Setup Default HighScores");
EEPROM.put(10, dftHighScores);
}
//Load High Scoores
EEPROM.get(10, highScores);
#ifdef DEBUG
for(int i=0; i<5; i++) {
HighScoreLog(highScores[i]);
Serial.println("======================");
}
#endif
}
void HighScoreLog(struct highscore temp)
{
Serial.println();
Serial.print(" Name = ");
Serial.println(temp.name);
Serial.print(" Attempts = ");
Serial.println(temp.attempts);
}
// }
void loop() {
/* Empty loop */
}
But if I change the code so that the EEPROM.put is in its own function then my read seems to get bad data and I cannot figure out why
#include <EEPROM.h>
#define DEBUG 1;
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"}
};
int initFlag = 0;
int eeAddressFlag = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
EEPROM.get(eeAddressFlag, initFlag);
#ifdef DEBUG
Serial.print(" flag = ");
Serial.println(initFlag);
#endif
if (initFlag != 5) {
EEPROM.put(eeAddressFlag, 5);
// Setup Default High Scores
Serial.println("Setup Default HighScores");
writeHighScore(dftHighScores);
}
//Load High Scoores
EEPROM.get(10, highScores);
#ifdef DEBUG
for(int i=0; i<5; i++) {
HighScoreLog(highScores[i]);
Serial.println("======================");
}
#endif
}
void writeHighScore(highscore writeScore[5])
{
#ifdef DEBUG
for(int i=0; i<5; i++) {
HighScoreLog(writeScore[i]);
Serial.println("+++++++++++++++++++++++");
}
#endif
EEPROM.put(10, writeScore);
delay(250);
}
void HighScoreLog(struct highscore temp)
{
Serial.println();
Serial.print(" Name = ");
Serial.println(temp.name);
Serial.print(" Attempts = ");
Serial.println(temp.attempts);
}
void loop() {
/* Empty loop */
}
Output is now as follows:
flag = -1
Setup Default HighScores
Name = AAA
Attempts = 10
+++++++++++++++++++++++
Name = BBB
Attempts = 20
+++++++++++++++++++++++
Name = CCC
Attempts = 30
+++++++++++++++++++++++
Name = DDD
Attempts = 40
+++++++++++++++++++++++
Name = EEE
Attempts = 50
+++++++++++++++++++++++
Name = ÿÿÿ
Attempts = 0
======================
Name = ÿÿÿÿ
Attempts = 255
======================
Name = ÿÿÿÿ
Attempts = 255
======================
Name = ÿÿÿÿ
Attempts = 255
======================
Name = ÿÿÿÿ
Attempts = 255
======================
Any help would be appreciated I am sure it is something simple I am missing.