Save/read score to eeprom

Hi all,

I'd like to store highscore values in eeprom.
I got the idea to save values up to 2.549.999 using only 3 bytes: (1) 254 (2) 99 (3) 99
So 1st byte would store 10.000 - 2.540.000
2nd byte would be 100 - 9.900
3rd byte would be 0 - 99

I'd like to use one "global" variable to keep track of the score, and split that up somehow when saving/loading to/from eeprom.
The score needs to be displayed every now and then, and I'm already using "sprintf" in my code. (so no extra overhead there)

Questions:

  1. Is this a good idea, or is there a better way of doing this? (I probably have enough eeprom space, but I like to save where I can)
  2. Any tips on how to implement saving & using values >= 1.000.000?
  3. Should I drop this idea, and save as chars instead? (Got that down. Saving/loading player names already)

Any tips, comments or remarks are appreciated!

3 bytes is such an awkward data type. 3 bytes will hold 2^24=16777216 values. If you store your high score in a "unsigned long" (which is 32 bit or 4 bytes), then you can store it in EEPROM using EEPROM.put. If you insist of just storing 3 bytes, then you must do something like:

EEPROM.put(address+0, (byte)(highscore & 0xFF));
EEPROM.put(address+1, (byte)((highscore>>8) & 0xFF));
EEPROM.put(address+2, (byte)((highscore>>16) & 0xFF));

And reverse the operation when reading it.

Danois90:
3 bytes is such an awkward data type. 3 bytes will hold 2^24=16777216 values. If you store your high score in a "unsigned long" (which is 32 bit or 4 bytes), then you can store it in EEPROM using EEPROM.put.

Thanks for that Danois.
I was already planning to use a "long" to store the total value (as an "int" can't hold a large enough number)...
This is exactly what I wanted to know, as the one extra byte doesn't hurt when it can make things easier/less awkward.

On a side note; I'm not using Arduino's EEPROM lib. I just use avr/eeprom.h functions, no extra includes required.
But I expect "eeprom_update_block()" will do the same job just fine.

AvanOsch:
On a side note; I’m not using Arduino’s EEPROM lib. I just use avr/eeprom.h functions, no extra includes required.

The arduino EEPROM.h library allows you to store an object… like a struct:

example:

#include <EEPROM.h>

struct Player {
  uint32_t score;
  char name[24];
};

Player savedHighScore = {
  194590, "Joe Schmoe"
};

Player currentPlayer = {
  0, "Current Player"
};

void setup() {
  Serial.begin(9600);
  //EEPROM.put(0, savedHighScore);  // uncomment to store the default in EEPROM
  EEPROM.get(0, savedHighScore);
  Serial.print(savedHighScore.name);
  Serial.print("\t");
  Serial.println(savedHighScore.score);
  Serial.println();
  Serial.println(F("Enter Player Name"));
  bool gotName = false;
  char newPlayerName[24] = "";
  while (!gotName)
  {
    if (Serial.available())
    {
      Serial.readBytesUntil('\n', newPlayerName, sizeof(newPlayerName) - 1);
      gotName = true;
    }
  }
  //  Serial.println(newPlayerName);
  strcpy(currentPlayer.name, newPlayerName);
  Serial.print(currentPlayer.name);
  Serial.print("\t");
  Serial.print(currentPlayer.score);
}

void loop() {
  //pseudo code...
//  if (currentPlayer.score > savedHighScore.score)
//  {
//    EEPROM.put(0, currentPlayer);
//  }

}

BulldogLowell:
The arduino EEPROM.h library allows you to store an object.. like a struct:

Wow. That's pretty sweet! Thanks!
I don't actually need that for my current project (no linking players with the scores or anything - I'm making a pool/9 ball scoreboard), but I will definitely play around with that in the future!
Might even eventually adjust my code to use this.
-- EDIT --
Getting all kinds of ideas already to add functionality :wink:

Many thanks @BulldogLowell:
simple solution with short code: exactly what I was looking for!
karma added! :sunglasses: