Writing and loading a 2D Array to EEPROM

I’m making a patch preset saving device.

They’re saved in a 2D array, where the first dimension are patch numbers, and the second is the values for each patch.

I need to write these to EEPROM on a button press, and load them on power up.

However, I’m not sure how to preserve the structure of this. If I use the following it will just save a meaningless load of bytes: (20 patches of 50 bytes of data each - although approx 30 of these will be zero because of a weird data structure I’m using. I’d like to ask about this too, but I don’t want to swamp you gurus with q’s).

  for (int i = 0; i < 20) {
    for (int j = 0; j < 50) {
   EEPROM.write (i+j, patches[j] );

Perhaps I could count to 20 on loading the data, and after this, increment i, and set j to 0?
Not quite sure how I could do this.

Assistance appreciated.

   EEPROM.write (i+j, patches[j] );

Several combinations of i and j will result in writing to the same location in EEPROM. This is NOT what you want to do.

You really need to write the whole (sparse) array to EEPROM, and read the whole thing back. Unless the array is really sparse, in which case you could write i, j, and the value for each element of the array that is not 0. But, it the array is more than 1/3 full, you'd need more memory to save it.

although approx 30 of these will be zero because of a weird data structure

Perhaps you should explain more about this weird data structure.

Perhaps you should explain more about this weird data structure.

Don’t say I didn’t warm you… haha.

Maybe it will help with finding a good solution though, and perhaps someone could show me how to optimise it.

I don’t know what you know about the MIDI protocol, but it’s 3 byte messages. We’ll ignore the first one, as it’s not relevant here, but after this, essentially they are “Parameter number”, followed by “value”.

A 2D array saves the “patch” - that is, arrays of all the “values”, the 3rd bytes.
This is indexed by another array, 20 or so long, holding the CC numbers.

So to load a patch, you cycle through an array of all the parameter numbers, looking at the patches array:
1st dimension = the patch number
2nd dimension = indexed by the value of the parameters array.

EG: Sending cutoff frequency (parameter number 45) for patch 3 would look like this:

SendSysEx(1, ccNum[14], patches[3][ccNum[14]]);

14? Because 45 is at position 14 in the array, although this is arbitrary - I will never need to send just one parameter, only all of them.

Here's the parameter numbers, if you'd find that relevant:

byte ccNum[] = {11 , 42, 5,40, 43, 46, 48, 47, 49, 50, 51, 52, 53, 44, 45};

The problem is that the patches array is 20 (number of patches) x 50 - because parameter numbers range from 1 - 50, even though there is only 20 or so of them, and I’m using them to index each patch. This is obviously a little pointless, as if I work out how to write them to EEPROM i’ll be writing a bunch of zeros or NULLs or whatever.

void loadPatch() {
  for (int i = 0; i < sizeof(ccNum); i++) {
    if (ccNum[i] != 0) {
      SendSysEx(1, ccNum[i], patches[currentPatch][ccNum[i]]);
      //  SendSysEx(1, ccNum[i],100);


void SendSysEx(int Chan, byte Par, byte Value) {
  Serial1.write(176);         // for example Cutoff Synt
  Serial1.write(Par);         // for example Cutoff Synt
  Serial1.write(Value);         // for example Cutoff Synt

Above is the basic function used to load patches. I hope I’ve made it clear in the way I’ve gone about this!

Thanks and best wishes!

Any comments, folks? :grin: