Go Down

Topic: reducing the amount of code in ram to free up space (Read 2 times) previous topic - next topic

countrypaul

Which version of the IDE is in use?

Some simple things such as replacing

 
Code: [Select]
lcd.setCursor(0,0); 
  lcd.print("Saving.     ");
  delay(500);
  lcd.setCursor(0,0);
  lcd.print("Saving..    ");
  delay(500);
  lcd.setCursor(0,0);
  lcd.print("Saving...   ");
  delay(500);
  lcd.setCursor(0,0);
  lcd.print("Saving....  ");
  delay(500);
  lcd.setCursor(0,0);
  lcd.print("Saving..... ");
  delay(500);
  lcd.setCursor(0,0);
  lcd.print("Saving......");
  lcd.setCursor(0,0);
 

with a loop that adds the requisite numbre of dots/spaces will mean you only need 3 string ("Saving","."and," ") so using 1/6 of the storage you currently use might help.

SukkoPera

You can also use the F() macro to avoid copying strings to SRAM when you are print()'ing them, i.e.:

Code: [Select]
lcd.print (F("Saving"));

paulsvang


You know you can attach a file to a forum posting? Click on "Additional Options" below the post and use the Attach option.


I saw it after I copied/pasted the code in so I just left it.

Hopefully someone has an insight progmem with menubackend.

GoForSmoke

Here are pieces of code I used to put menu into PROGMEM. I also put error messages, etc, there.

You need some #includes to use PROGMEM:
Code: [Select]

#include <avr/io.h>
#include <avr/pgmspace.h>


Here I have a 7 line menu (80 characters per line) stored to PROGMEM, and a special pointer to get the data with:
Code: [Select]

const char PROGMEM usageMsg[][80] = { // all this text stored in flash
  "          Piezo touch sensor tuner.",
  "Adjust vertical; enter W to subtract or X to add, and a number 0-255",
  "Adjust timescaler; enter A to subtract or D to add, and 0-16535",
  "to add 250 to vertical enter W250 in Serial Monitor and press enter",
  "seperate multiple commands with spaces; ex: W25 D240",
  "    ** this message stored in flash ram **",
  "    ** and printed with a 1 byte buffer ** :-P"
};

PGM_P Msg; // pointer into flash memory


The full program stores -every- const char string (not C++ String!) in PROGMEM. It has serial user I/O error messages, etc, as well as the menu.

I made this function to print the stored strings one char at a time:
Code: [Select]

void  printMsg( PGM_P FM )
{
  do
  {
    B = pgm_read_byte( FM++ );    // FM++ is pointer math; look Ma, no indexes!
    if ( B )  Serial.print( B );
  }
  while ( B ); // when it reaches the terminating zero, it exits
}


And this function just to print the whole menu:
Code: [Select]

void printHelp(void)
{
  for ( byte i = 0; i < HELPLINES; i++ )
  {
    printMsg( usageMsg[ i ]);
    Serial.println();
  }
  Serial.println();
}
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

paulsvang


You can also use the F() macro to avoid copying strings to SRAM when you are print()'ing them, i.e.:

Code: [Select]
lcd.print (F("Saving"));



I'm going to try this out.   Thanks SukkoPera.

Go Up