EEPROM - saving int arrays

Hi there,

I would like to save the following array into EEPROM.

int saveSounds[850] = {50, 100, 31, 4000, 10, .......5};

Do I use EEPROM Put? How do I do this?

Thank you in advance.

On what board? On an UNO it's just plain not gonna happen. Not enough room.

Hello,

I have the UNO. Do you mean the array I want to save will exceed 1k byte?

Flash 32k bytes (of which .5k is used for the bootloader)
SRAM 2k bytes
EEPROM 1k byte

You have 850 ints.

An int is 2 bytes.

2 * 850 is 1700.

1700 is larger than 1K.

So yes, your array is too big to fit in EEPROM

mirandamg:
Hi there,

I would like to save the following array into EEPROM.

int saveSounds[850] = {50, 100, 31, 4000, 10, .......5};

Do I use EEPROM Put? How do I do this?

Thank you in advance.

Check the Reference Page

The supported micro-controllers on the various Arduino and Genuino boards have different amounts of EEPROM: 1024 bytes on the ATmega328, 512 bytes on the ATmega168 and ATmega8, 4 KB (4096 bytes) on the ATmega1280 and ATmega2560. The Arduino and Genuino 101 boards have an emulated EEPROM space of 1024 bytes.

at two bytes per int, you cannot fit your data into EEPROM if for example you are working with an UNO.

Would it help to store it in PROGMEM Flash instead of EEPROM?

If it is const.

You aren't really giving us much to work with. http://xyproblem.info/

Why don't you start by describing what you are trying to do instead of how you think you might go about it.

I am working on an electronic piano that has 8 tactile switch keys. The saveSounds array changes according to what the person plays on the keyboard. Does this mean I cannot put it into PROGMEM because the array is not constant?

mirandamg:
I am working on an electronic piano that has 8 tactile switch keys. The saveSounds array changes according to what the person plays on the keyboard. Does this mean I cannot put it into PROGMEM because the array is not constant?

Yes, that is exactly what it means. You can't modify the stuff you put in PROGMEM at runtime. It is only good for const data.

I can change

int saveSounds[850] = {50, 100, 31, 4000, 10, .......5}; into

int saveSounds[500] = {50, 100, 31, 4000, 10, .......5};

This means I am using 500*2 = 1000 bytes.

Do I use EEPROM Put? How do I do this?

Thank you in advance.

Yes, use put. See the examples in the reference for how to do it. Or check with Google, there are tons of examples and tutorials on using EEPROM.

Or, since you'll be basically filling the whole thing, use the EEPROM syntax to just deal with the array in EEPROM directly just like it EEPROM were the name of the array.

Do you mean replacing:

  1. saveSounds[saveNumber] = tone_key; with EEPROM[saveNumber] = tone_key;?
  2. if(saveSounds != 0) with if(EEPROM != 0)
    3) tone(speaker,saveSounds_); with tone(speaker,EEPROM*);
    Basically all mention of saveSounds with EEPROM?
    Would I exceed the limit of 100,000 write/clear cycles if I implement this in my code? I have a loop that loops 500 times.
    Here is my code:
    void loop() {_

    if (digitalRead(record_button) == HIGH) {
    digitalWrite(record_led, HIGH);
    _ for(int i = 0; i < 500 ; i++) {
    octave = map(analogRead(inPot),0,1023,1,7);
    if (digitalRead(buttons[0]) == HIGH) { _

    tone_out = 0;
    _ silence = false;
    }
    else if (digitalRead(buttons[1]) == HIGH) {_

    tone_out = 1;
    _ silence = false;
    }
    else if (digitalRead(buttons[2]) == HIGH) {_

    tone_out = 2;
    _ silence = false;
    }
    else if (digitalRead(buttons[3]) == HIGH) {_

    tone_out = 3;
    _ silence = false;
    }
    else if (digitalRead(buttons[4]) == HIGH) {_

    tone_out = 4;
    _ silence = false;
    }
    else if (digitalRead(buttons[5]) == HIGH) {_

    tone_out = 5;
    _ silence = false;
    }
    else if (digitalRead(buttons[6]) == HIGH) {_

    tone_out = 6;
    _ silence = false;
    }
    else if (digitalRead(buttons[7]) == HIGH) {_

    tone_out = 7;
    _ silence = false;
    }
    else {
    silence = true;_

    tone_key = 0;
    _ }
    if (silence == false)
    {
    switch(octave)
    {
    case 1:_

    tone_key = melody1[tone_out]; break;
    _ case 2:_
    tone_key = melody2[tone_out]; break;
    _ case 3:_
    tone_key = melody3[tone_out]; break;
    _ case 4:_
    tone_key = melody4[tone_out]; break;
    _ case 5:_
    tone_key = melody5[tone_out]; break;
    _ case 6:_
    tone_key = melody6[tone_out]; break;
    _ case 7:_
    tone_key = melody7[tone_out]; break;
    _ }_
    tone(speaker,tone_key);
    _ delay(time);
    }
    else {
    noTone(speaker);_

    tone_key = 0;
    _ delay (time); }*_

* saveSounds[saveNumber] = tone_key;
_ saveNumber++;_
if (digitalRead(play_button) == HIGH){
_ break;}
}
//EEPROM*

digitalWrite(record_led, LOW);
tone(speaker, 700);
delay(200);
noTone(speaker);
check++;
}
if(check >= 1) {
* while (0 == 0){
freePlay;_

if(digitalRead(play_button) == HIGH)
_ {_
digitalWrite(play_led, HIGH);
_ for(int i = 0; i < maxNumber; i++)
{
if(saveSounds != 0)
{
tone(speaker,saveSounds);
delay(25);
}
else*

* {
noTone(speaker);
delay(time);
}
}
noTone(speaker);_

digitalWrite(play_led,LOW);
_ }
}
}
} *_

The forum software mangled your post and turned half of it into itallics. It's time to go read the forum rules and learn to use code tags (something you should have done before your first post) so that it will make sense.

Hello,

Sorry for the bother again. I have the following array:
int saveSounds[500] = {50, 100, …3};

Would the following work for placing this array into EEPROM?

int eeAddress = 0;

for (i = 0; i<500; i++) {
EEPROM.put(eeAddress,saveSounds*)*

  • eeAddress = eeAddress + 2;*
    }

Again, the code tags thing has got you. Please have enough respect for
the folks offering to help you for free to actually go read the rules thread and fix that. I think what you had written was right, but what shows on the screen now obviously isn't.

Hello,

Sorry for the bother again. I have the following array:
int saveSounds[500] = {50, 100, …3};

Would the following work for placing this array into EEPROM?

int eeAddress = 0;

for (i = 0; i<500; i++) {
  EEPROM.put(eeAddress,saveSounds)
  eeAddress = eeAddress + 2;
}

No. you need to index that array. You need

saveSounds[i]

So sorry about that. Thank you for your reply. I am getting the following errors:

PianoAllFunctions_latest.ino: In function ‘void loop()’:
PianoAllFunctions_latest:243: error: ‘class EEPROMClass’ has no member named ‘put’
PianoAllFunctions_latest:264: error: ‘class EEPROMClass’ has no member named ‘get’

Have I used EEPROM PUT and GET correctly? Thanks so much in advance.

Here is my code:

void loop() {

if (digitalRead(record_button) == HIGH) {
  digitalWrite(record_led, HIGH);
  for(int i = 0; i < maxNumber ; i++) {
  octave = map(analogRead(inPot),0,1023,1,7);                                           
  if (digitalRead(buttons[0]) == HIGH) {      
    tone_out = 0;                             
    silence = false;                          
  } 
  else if (digitalRead(buttons[1]) == HIGH) {
    tone_out = 1;  
    silence = false;
  } 
  else if (digitalRead(buttons[2]) == HIGH) {
    tone_out = 2; 
    silence = false;
  } 
  else if (digitalRead(buttons[3]) == HIGH) {
    tone_out = 3;  
    silence = false;
  } 
  else if (digitalRead(buttons[4]) == HIGH) {
    tone_out = 4;  
    silence = false;
  } 
  else if (digitalRead(buttons[5]) == HIGH) {
    tone_out = 5; 
    silence = false;
  } 
  else if (digitalRead(buttons[6]) == HIGH) {
    tone_out = 6; 
    silence = false;
  } 
  else if (digitalRead(buttons[7]) == HIGH) {
    tone_out = 7;
    silence = false;
  } 
  else {
    silence = true;
    tone_key = 0;
  }

  if (silence == false)
  {
    switch(octave) 
    {
    case 1: 
      tone_key = melody1[tone_out]; break;
    case 2: 
      tone_key = melody2[tone_out]; break;
    case 3: 
      tone_key = melody3[tone_out]; break;
    case 4: 
      tone_key = melody4[tone_out]; break;
    case 5: 
      tone_key = melody5[tone_out]; break;
    case 6: 
      tone_key = melody6[tone_out]; break;
    case 7: 
      tone_key = melody7[tone_out]; break;
    }
    tone(speaker,tone_key); 
    delay(time);
  }                         
  else {
   noTone(speaker);
   tone_key = 0; 
   delay (time); } 
   
    saveSounds[saveNumber] = tone_key;
    saveNumber++;
    if (digitalRead(play_button) == HIGH){
    break;}
}
//EEPROM
for (int i = 0; i<500; i++) {
  EEPROM.put(eeAddress,saveSounds[i]);
  eeAddress = eeAddress + 2;
}

digitalWrite(record_led, LOW);
tone(speaker, 700);
delay(200);
noTone(speaker);
check++;
}

if(check >= 1) {
  while (0 == 0){
    freePlay;
    if(digitalRead(play_button) == HIGH)
    {
      digitalWrite(play_led, HIGH);
      for(int i = 0; i < maxNumber; i++)
      {
        if(saveSounds[i] != 0)
        {
          tone(speaker,EEPROM.get(eeAddress,saveSounds[i]));
          eeAddress = eeAddress + 2;
          delay(25);
        }
        else
        {
          noTone(speaker);
          delay(time);
        }
      }
      noTone(speaker);
      digitalWrite(play_led,LOW);
    }
  }
 }
}

What version of Arduino?