optimizing loop for saving RAM

void SETLEDSFROMWEBUI_TO_EEPROM(){
  EEPROM.write(13, 0x92); //position 14 to 589 //  LEDS 96 * 6LEDS
  int i=1;
  int y=14;
        for (y = 14  ; y <= (LEDVALUES+13); y++ ){
          EEPROM.write(y, LED1[i]);
          EEPROM.write(y+(LEDVALUES), LED2[i]);
          EEPROM.write(y+(LEDVALUES*2), LED3[i]);
          EEPROM.write(y+(LEDVALUES*3), LED4[i]);
          EEPROM.write(y+(LEDVALUES*4), LED5[i]);
          EEPROM.write(y+(LEDVALUES*5), LED6[i]);
          i++;
          wdt_reset();  
      }
 
}

i need this code in order to take all the values from 6 byte arrays of 96 values each , and set them into the epprom ,
after the execution of this code , i read my ram with

int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

and is 700k less
how can i optimize it ?

What Arduino are you using? Mine has 2k of RAM so I would be surprised if I had 700k less after doing something.

You can't. That piece of code is allocating 2 int's (i.e., 4 bytes) - y and i - on the stack. The 700 bytes probably comes from the LED arrays. If they are byte arrays they total 576 bytes. If they're int arrays then you're wasting an extra 576 bytes.

That function returns the amount of free RAM between the top of the heap and the bottom of the stack. It doesn't take into account any free space within the heap itself.

i am using arduino mega

my bad ,i am so sorry
this is the part of code , that making the mess of RAM
the getWEBLED2values gets this string

http://192.168.2.61:90/?SXL=1&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=50&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&color=%23ffffff

and concatenated , in order to save it into the epprom

void getWEBLED2values(String data){
    char a ='#';
    String value ;
    int FinalValue;
    data.replace("SXL","*");
    data.replace("PWM","#");
    data.replace("&","");
    data.replace("color=%23","#");

    for (int i = 1 ; i <=LEDVALUES; i++){
      value=CONCATENATE( data ,a,i) ;
        if (value != "*=1"){
                int check = value.indexOf('=');
                if(check  != -1){
                      FinalValue = (value.substring(check+1)).toInt();
                      wdt_reset();
                }
//WebSelectedLEDvalues is byte array[96]
           WebSelectedLEDvalues[i]= FinalValue;
        }
      } 
      value=CONCATENATE( data ,a,97);
      char charbuf[8];
      value.toCharArray(charbuf,8);
      fillHexValues(charbuf);
}

 void fillHexValues(char charbuf[8])
 {
         byte dum[3];
         long int rgb=strtol(charbuf,0,16); 
         dum[0]=(byte) (rgb>>16) ;
         dum[1]=(byte) (rgb>>8);
         dum[2]=(byte) (rgb);
              
              switch (selectedLED) {
              case 1 :
                  LED1COLOR[0] =dum[0];LED1COLOR[1] =dum[1];LED1COLOR[2] =dum[2];
                  break;
              case 2 :
                  LED2COLOR[0] =dum[0];LED2COLOR[1] =dum[1];LED2COLOR[2] =dum[2];
                  break;
              case 3 :
                  LED3COLOR[0] =dum[0];LED3COLOR[1] =dum[1];LED3COLOR[2] =dum[2];
                  break;
              case 4 :
                  LED4COLOR[0] =dum[0];LED4COLOR[1] =dum[1];LED4COLOR[2] =dum[2];
                  break;
              case 5 :
                  LED5COLOR[0] =dum[0];LED5COLOR[1] =dum[1];LED5COLOR[2] =dum[2];
                  break;
              case 6 :
                  LED6COLOR[0] =dum[0];LED6COLOR[1] =dum[1];LED6COLOR[2] =dum[2];
                  break;
              }
              wdt_reset();
              setLEDColorsToEEPROM();
              wdt_reset();
              SETLEDColorFROMEEPROM();
 }

void setLEDColorsToEEPROM(){
   wdt_reset();
   EEPROM.write(2000, 0x92);
   EEPROM.write(2001, LED1COLOR[0]);   EEPROM.write(2002, LED1COLOR[1]);   EEPROM.write(2003, LED1COLOR[2]);
   EEPROM.write(2004, LED2COLOR[0]);   EEPROM.write(2005, LED2COLOR[1]);   EEPROM.write(2006, LED2COLOR[2]);
   EEPROM.write(2007, LED3COLOR[0]);   EEPROM.write(2008, LED3COLOR[1]);   EEPROM.write(2009, LED3COLOR[2]); 
   EEPROM.write(2010, LED4COLOR[0]);   EEPROM.write(2011, LED4COLOR[1]);   EEPROM.write(2012, LED4COLOR[2]);
   EEPROM.write(2013, LED5COLOR[0]);   EEPROM.write(2014, LED5COLOR[1]);   EEPROM.write(2015, LED5COLOR[2]);
   EEPROM.write(2016, LED6COLOR[0]);   EEPROM.write(2017, LED6COLOR[1]);   EEPROM.write(2018, LED6COLOR[2]);
    wdt_reset();  
 }

 String CONCATENATE(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        wdt_reset();
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

as you can see i play with String all the time ...

i store into a temp array the values , and then i send them into the selected led array , after that i store them into the EPPROM

      getWELED2values(parm);
                                             for (int i = 1 ; i <=LEDVALUES; i++)
                                          {
                                            wdt_reset();
                                             SETLedValue(selectedLED, i, WebSelectedLEDvalues[i]);
                                          }
                                       SETLEDSFROMWEBUI_TO_EEPROM();

Ok, we're starting to get to the heart of it now.

  1. Don't use String.
  2. Parse the data as it comes in - don't store it in an intermediate array.

You might also be better only sending values that have been changed rather than the whole lot, or breaking the data down into separate chunks and sending them as separate requests. If you identify the variables with numbers instead of just a generic "PWM=..." (i.e., use "PWM1=...&PWM2=..." etc) then you can tie that to an array slice. Also, keeping your parameter names short would save space - use P1 instead of PWM1 for example. HEX encoding can also reduce space in the data stream, since FF is one character less than 255 (tip: strtol() can parse HEX values).

the getWEBLED2values gets this string

http://192.168.2.61:90/?SXL=1&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=255&PWM=50&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&PWM=0&color=%23ffffff

That 719 byte string?


Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software