Arcade Game Project - How to store player data on SD card

Hey everyone,

I have a question for an Arduino project that we are currently working on at home.
So the basic setup is the following:

  • we have an Arduino Mega on a breadboard
  • we have an Adafruit LED strip, consisting of 450 LEDs connected to it
  • we have an arcade joystick also connected to the Arduino Mega

The LEDs are arranged in a way that they form a 15x30 matrix, where we project simple arcade games like Snake.

Now we added a SD card adapter (something like this) with an SD card to the breadboard.

We want to use the SD card to log some player data, for instance in the case of snake to store the player name and the score.
When starting a game, a player can choose a name given on some list of pre-set names. (Subject to be changed into an interface, where one can write their name on the LED matrix.) The names will be three letters long.
So we basically have key value pairs, e.g. String name = "lmf" with int score = 62 (...).
We need to be able to

  1. update the score for a given player name
  2. sort the list of players by score, such that the highest scores are on top

This involves reading the whole file associated to a game on the SD card, which should be fine since we assume a player list of like 40-50 players.

Now the main questions is, how to efficiently store the data on the SD card such that, reading and writing remains "fast" but also the approach is open to updates. And additionally we can update the score for a given player name and then sort the data by score.

We have so far come up with two ideas:

  1. using ArduinoJson to define structs and thus store the player data according to this library on the SD card.
  2. Storing a key value pair as a comma separated line, e.g. lempf,62 (,...) and use string splitting methods to retrieve player name and score.

The first approach would leave the data structures more flexible to future adjustments, whereas the latter idea is probably faster and has less overhead.
Also in the latter case, updating at a specific index and sorting would involve transferring the data into some appropriate data structure (Vector probably) and re-transferring it, before writing back to the SD card.

Does anyone have experience with this kind of problem ? Is it worth using the ArduinoJson library or does it speed down the processes or consume too much memory?
Or does someone maybe have a better idea to store player data (name, score, ...) efficiently on a SD card (including updating and sorting)?

Thanks for the help in advance.

Best regards,
Lempf

ArduinoJson is fantastic and is a great benchmark. If would try it first. If it works and is fast enough then you're good to go, which it probably will given the simple nature of your game project.
If you find it lacking then I would consider other options.
Key: Value pairs alone are not enough to generate speed as accessing values for read or write takes O(n) time. You would need to create a lookup table to associate the keys to the values. A simple hash should do. Then heap the values at the end of the card and put the LUT at the beginning of the card. That should give you O(1) access for reads and writes. There is probably a library for this already but I cannot imagine you needing this kind of speed for game state data.

1 Like

Unless you plan to have the need for a multilevel json, json is overkill in my opinion. To be as fast as possible, I would try to

  1. use fixed width records so you don't have to read a data till you find a newline but can use seek
  2. store the records sorted by name (or hash which is probably faster) and use a binary search.

But if that is really relevant for yor game is the question.

1 Like

Hey guys,
thanks for your answers they definitely helped and both cover different future approaches.

Right now I just found out about the EEPROM capabilities of Arduinos (4KB in our case), which should be sufficient and quick to set up in our case.

@sterretje: your approach is probably then useful if we reach limits on the EEPROM
@er_name_not_found: this insight is great and maybe we will end up using ArduinoJson, when our project grows too big :smiley:

Thanks again and closed with this!