Help with reducing global variable memory

Hi All,
I am still relatively new to programming but have been able to get enough under my belt to start a rather ambition project.
I have an arduino UNO i am using with an LCD display and 4X4 key pad to take user input and format it into a 51 digit code to send to another micro-controller for controlling various motors.
I have gotten my project working quite well despite my probable inefficient coding styles.
I am wanting to add micro SD card module to my project to save setting to a text file, and and possibly a DS3231 RTC.

The issue i am having is I don't have enough dynamic memory to add the code for the micro SD card board.

My current code stats (code attached):

Sketch uses 12300 bytes (38%) of program storage space. Maximum is 32256 bytes.
Global variables use 1399 bytes (68%) of dynamic memory, leaving 649 bytes for local variables. Maximum is 2048 bytes.

When i verify a basic sketch (code attached) for the SD card board it uses 1010 bytes of dynamic storage which wont fit with my current main project code.

My question is: is there any way i can reduce my memory usage in my main project enough to add the sd card code? and possibly the RTC as well? or is that a pipe dream on my UNO board?

Let me know

Thanks

Oliver

MainCode.ino (18.8 KB)

sd_card_test.ino (671 Bytes)

Try using the F macro with all of your print statements. It will put the strings in PROGMEM rather than RAM.

Like this:

    lcd.print(F("----- Motor "));
2 Likes

Look at these cases. Only a few things are different each time. You should make a function that takes the different things as parameters, and call it instead of repeating essentially the same inline code over and over again:

        switch (PSTEP) {
          case 1:
            lcd.setCursor(0, 1);
            lcd.print("Speed :");
            lcd.setCursor(7, 1);
            checkTemp(MTR, 0);
            editCode(key);
            lcd.print(tempCode[0]);
            lcd.print(tempCode[1]);
            lcd.print(tempCode[2]);
            lcd.print(tempCode[3]);
            if (key == '#') {
              setCode(MTR, 0);
              clearTemp();
              SET = 0;
              PSTEP = 2;
            }
            break;
          case 2:
            lcd.setCursor(0, 1);
            lcd.print("Angle :");
            lcd.setCursor(7, 1);
            checkTemp(MTR, 1);
            editCode(key);
            lcd.print(tempCode[0]);
            lcd.print(tempCode[1]);
            lcd.print(tempCode[2]);
            lcd.print(tempCode[3]);
            if (key == '#') {
              setCode(MTR, 1);
              clearTemp();
              SET = 0;
              PSTEP = 3;
            }
            break;

This is just one example. I think if you comb through your sketch you will see many more opportunities to factor code into functions. It will probably cut your code size to less than half. Also you have a char array that you can place in PROGMEM.

@ToddL1962 Thanks for your suggestion, Using the F macro shaved off 450 bytes! Sweet!

@aarg I will do that next, I am coming from languages where I never had to worry about memory management and so i'm still trying to figure out what everything does.
Can i use PROGMEM with a variable char array or constant only? (i'm trying to see which arrays i can switch from SRAM to FLASH)

Let me know, and thanks for the awesome suggestions.

Oliver