1

I have a ESP32, programmed using Arduino IDE. In my code I have 2 variables: sName (string) and nScore (double). This pair of variables, needs to be written to the EEPROM into the higscore namespace. So far it would be easy, open the namespace, and write the values..

but here comes the tricky part: The namespace highscore has 20 values: 10 names and 10 scores. I need to write the value to the namespace only if it is higher than those already present, and to add it to the correct spot in the high score table, shifting all the other values.

How should I do this? Currently I am thinking of loading all the values on startup and storing it in 2 arrays. Then whenever something is changed in the array, write it down.

But I am not sure that this is the proper way of doing this.

sharkyenergy
  • 3,842
  • 10
  • 46
  • 97
  • Just a heads up OP, on the ESP32 you have SPIFFS and the resources something like ArduinoJson. SPIFFS lets you treat part of the Flash as a file system so you could just do `SPIFFS.open("highscore.json")`, and ArduinoJson lets you represent your data in a format that's easy for you to understand. Json for saving basic types on an embedded processor's flash is usually a waste, but it sounds like this isn't data that changes every millisecond, and most ESP32 projects have *tons* of unused resources left over when it comes to RAM. – Selali Adobor Jul 09 '21 at 17:00
  • 1
    (At the very least I recommend SPIFFS/LittleFS over EEPROM, it's using the same flash on the ESP32 but with an easier interface) – Selali Adobor Jul 09 '21 at 17:03

1 Answers1

2

Assuming that you want to minimize the number of writes to EEPROM, the best way would be to assign 10 addresses of EEPROM, one for each highscore holder, and then have a separate variable in EEPROM to denote the order of the highscores.

eg: 
ADD1: NameA
ADD2: NameB
ADD3: NameC
....

and then an

int ord = 231

Which would mean 1.NameC 2.NameA 3.NameB....

This way if someone new comes into the Scoreboard, rewrite only the address of the player with least score (eg: order 3 -> NameB) and rearrange the ord variable.

Since you have have 10 entries, your ord variable could be something like 7562931048, where 0 would be the highest scorer.

In any case you'll surely have to load all the scores(maybe just the numbers) into ram while startup(or at a later time) to compare.

Isa Haroon
  • 140
  • 3
  • 1
    thank you! I do not really care about how much I write it, because there will be under 100 rewrites the first week, and then go down to 0 within a month. so in total i expect around 350 writes.. but I like your idea.. i will consider it. thank you! – sharkyenergy Jun 28 '21 at 10:39