Writing and reading multiple arrays from EEPROM

Hi there i am trying to store a set of boolean arrays to EEPROM so the state is saved after switching off , at the moment i have set it to write to EEPROM with the long click of the resButton i have. it’s working with the preset1 array but not reading anything back from preset2 array after turning off so i have not taken it any further. Any help would really be appreciated. Thankyou!!! :slight_smile: Board is the Mega 2560 by the way.

#include <EEPROM.h>
#include <PinButton.h>

PinButton buttons[12] = {27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49};
const byte leds[12] = {24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46};
bool ledOn[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset1[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset2[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset3[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset4[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset5[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset6[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset7[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset8[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset9[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset10[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset11[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool preset12[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool clearLeds[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};



PinButton resButton = 23;
const byte resLed = 53;
const byte presetButton = 25;
const byte presetLed = 22;

void setup() {

   Serial.begin(9600);
  // put your setup code here, to run once:
 for (byte i = 0; i < 12; i++)  {
    pinMode(leds[i], OUTPUT);
}
    pinMode(resLed, OUTPUT);

    for (byte i = 0; i < 12; i++)
    preset1[i] = EEPROM.read(i);

    for (byte i = 0; i < 12; i++)
     for (byte j = 12; j < 24; j++)
    preset2[i] = EEPROM.read(j);

}

void loop() {
  // put your main code here, to run repeatedly:
  for (byte i = 0; i < 12; i++)
  buttons[i].update();
  resButton.update();
  myButtons();
  

  
}

void myButtons(){

  if(resButton.isSingleClick()) {
   for (byte i = 0; i < 12; i++)
   digitalWrite(leds[i], LOW);
   for (byte i = 0; i < 12; i++)
   ledOn[i] = clearLeds[i];
   
  }

  if (resButton.isLongClick()){
  for (byte i = 0; i < 12; i++)
  EEPROM.write(i, preset1[i]);
   for (byte i = 0; i < 12; i++)
   for (byte j = 12; j < 24; j++)
  EEPROM.write(j, preset2[i]);
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
    
    }
  
  for (byte i = 0; i < 12; i++)
  if (buttons[i].isSingleClick()) {
  
  ledOn[i] = !ledOn[i];
 
   digitalWrite(leds[i], ledOn[i]);
   
  }
  for (byte i = 0; i < 12; i++)
  if (buttons[11].isLongClick()) {
  preset1[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
}

  if (buttons[11].isDoubleClick()) {
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset1[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset1[i];
}

  for (byte i = 0; i < 12; i++)
  if (buttons[4].isLongClick()) {
  preset2[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
 
}

  if (buttons[4].isDoubleClick()) {
    
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset2[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset2[i];
}

  for (byte i = 0; i < 12; i++)
  if (buttons[10].isLongClick()) {
  preset3[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
 
}

  if (buttons[10].isDoubleClick()) {
    
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset3[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset3[i];
  }
  
  for (byte i = 0; i < 12; i++)
  if (buttons[3].isLongClick()) {
  preset4[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
 
}

  if (buttons[3].isDoubleClick()) {
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset4[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset4[i];
  }
  
  for (byte i = 0; i < 12; i++)
  if (buttons[9].isLongClick()) {
  preset5[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
 
}

  if (buttons[9].isDoubleClick()) {
    
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset5[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset5[i];
  }

  for (byte i = 0; i < 12; i++)
  if (buttons[8].isLongClick()) {
  preset6[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
 
}

  if (buttons[8].isDoubleClick()) {
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset6[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset6[i];
  }
  
  for (byte i = 0; i < 12; i++)
  if (buttons[2].isLongClick()) {
  preset7[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
 
}

  if (buttons[2].isDoubleClick()) {
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset7[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset7[i];
  }

  for (byte i = 0; i < 12; i++)
  if (buttons[7].isLongClick()) {
  preset8[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
 
}

  if (buttons[7].isDoubleClick()) {
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset8[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset8[i];
  }

  for (byte i = 0; i < 12; i++)
  if (buttons[1].isLongClick()) {
  preset9[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
 
}

  if (buttons[1].isDoubleClick()) {
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset9[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset9[i];
  }

  for (byte i = 0; i < 12; i++)
  if (buttons[6].isLongClick()) {
  preset10[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
 
}

  if (buttons[6].isDoubleClick()) {
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset10[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset10[i];
  }

  for (byte i = 0; i < 12; i++)
  if (buttons[0].isLongClick()) {
  preset11[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
 
}

  if (buttons[0].isDoubleClick()) {
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset11[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset11[i];
  }
  
  for (byte i = 0; i < 12; i++)
  if (buttons[5].isLongClick()) {
  preset12[i] = ledOn[i];
  digitalWrite(resLed, HIGH);
  delay(10);
  digitalWrite(resLed, LOW);
 
}

  if (buttons[5].isDoubleClick()) {
    
  for (byte i = 0; i < 12; i++)
  digitalWrite(leds[i], preset12[i]);
  for (byte i = 0; i < 12; i++)
  ledOn[i] = preset12[i];
  }
  }
    for (byte i = 0; i < 12; i++)
      for (byte j = 12; j < 24; j++)
        EEPROM.write(j, preset2[i]);

Are these 2 for loops intended to be nested within each other ?

UKHeliBob:

    for (byte i = 0; i < 12; i++)

for (byte j = 12; j < 24; j++)
        EEPROM.write(j, preset2[i]);



Are these 2 for loops intended to be nested within each other ?

yes

EEPROM.put() and EEPROM.get() can help you handle your arrays. You can work with a 2D array to structure all your presets in a more efficient manner. Address management is important, and removal of “magic numbers” to make the code generic would be an improvement from here.

#include <EEPROM.h>

void setup() {
  Serial.begin(115200);
  boolean preset1[12] = {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0};
  boolean preset2[12] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1};
  boolean readPreset1[12] = {};
  boolean readPreset2[12] = {};
  boolean pre_set[2][12] =
  {
    {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},
    {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}
  };
  boolean read_pre_set[2][12] = 
  {
    {},
    {}
  };
  
  byte eeaddress = 50;//start storing data in address 50

  EEPROM.put(eeaddress, preset1);
  EEPROM.put(eeaddress + 12, preset2);
  EEPROM.get(eeaddress, readPreset1);
  EEPROM.get(eeaddress + 12 , readPreset2);
  EEPROM.put(eeaddress+24, pre_set);
  EEPROM.get(eeaddress+24, read_pre_set);
  


  for (byte j = 0; j < 12; j++)
  {
    Serial.print(readPreset1[j]);
    Serial.print(" ");
  }
  Serial.println();

  for (byte j = 0; j < 12; j++)
  {
    Serial.print(readPreset2[j]);
    Serial.print(" ");
  }
  Serial.println();

  for (byte i = 0; i < 2; i++)
  {
    for (byte j = 0; j < 12; j++)
    {
      Serial.print(read_pre_set[i][j]);
      Serial.print(" ");
    }
    Serial.println();
  }

}


void loop() {}
   for (byte i = 0; i < 12; i++)
      for (byte j = 12; j < 24; j++)
        EEPROM.write(j, preset2[i]);

Better written as

for (byte i = 0; i < 12; i++) //do the outer loop 12 times
{
  for (byte j = 12; j < 24; j++)  //do the inner loop 12 times
  {
    EEPROM.write(j, preset2[i]);    //how many writes in total ?
  }
}

cattledog:
EEPROM.put() and EEPROM.get() can help you handle your arrays. You can work with a 2D array to structure all your presets in a more efficient manner. Address management is important, and removal of “magic numbers” to make the code generic would be an improvement from here.

#include <EEPROM.h>

void setup() {
  Serial.begin(115200);
  boolean preset1[12] = {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0};
  boolean preset2[12] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1};
  boolean readPreset1[12] = {};
  boolean readPreset2[12] = {};
  boolean pre_set[2][12] =
  {
    {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},
    {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}
  };
  boolean read_pre_set[2][12] =
  {
    {},
    {}
  };
 
  byte eeaddress = 50;//start storing data in address 50

EEPROM.put(eeaddress, preset1);
  EEPROM.put(eeaddress + 12, preset2);
  EEPROM.get(eeaddress, readPreset1);
  EEPROM.get(eeaddress + 12 , readPreset2);
  EEPROM.put(eeaddress+24, pre_set);
  EEPROM.get(eeaddress+24, read_pre_set);

for (byte j = 0; j < 12; j++)
  {
    Serial.print(readPreset1[j]);
    Serial.print(" ");
  }
  Serial.println();

for (byte j = 0; j < 12; j++)
  {
    Serial.print(readPreset2[j]);
    Serial.print(" ");
  }
  Serial.println();

for (byte i = 0; i < 2; i++)
  {
    for (byte j = 0; j < 12; j++)
    {
      Serial.print(read_pre_set[i][j]);
      Serial.print(" ");
    }
    Serial.println();
  }

}

void loop() {}

Thank you very much this is very helpful, is there a reason you started storing EEPROM from address 50?

is there a reason you started storing EEPROM from address 50?

Only to move around where I place things when I do these tests. I have a well used UNO for a lot of my forum code work and it can't hurt to wear level the EEPROM use. It's probably totally unnecessary with what I do, but always starting at address 0 gets boring :wink: