Not enough memory

I’ve been trying to make the Arduino check a lot of IF statements. So many that it says that I’m using 529% of the SRAM memory. I’ve already put all variables I can into PROGMEM but that didn’t help either… And I can’t use any memory extension shield for the Arduino because i’m alreasy using a shield for a UTFT display…
Is there any other way to move entire IF statements into flash memory or ay other way of more efficient coding?
I’m using an Arduino Mega for this project
The code I’m showing is only a tiny tiny piece of all the IF statements (in total about 29’000 lines of code…)

for (byte i = 0; i <= Flightplanlength - 1; i++) {
      Navpoint = Flightplan[i];
      if (Navpoint == "ARBOL") {
        coordis[0] = ARBOL[0];
        coordis[1] = ARBOL[1];
      }
      else if (Navpoint == "ARDAG") {
        coordis[0] = ARDAG[0];
        coordis[1] = ARDAG[1];
      }
      else if (Navpoint == "ARDOK") {
        coordis[0] = ARDOK[0];
        coordis[1] = ARDOK[1];
      }
      else if (Navpoint == "ARGES") {
        coordis[0] = ARGES[0];
        coordis[1] = ARGES[1];
      }
      else if (Navpoint == "ARGUT") {
        coordis[0] = ARGUT[0];
        coordis[1] = ARGUT[1];
      }
      else if (Navpoint == "ARIES") {
        coordis[0] = ARIES[0];
        coordis[1] = ARIES[1];
      }
      else if (Navpoint == "ARISA") {
        coordis[0] = ARISA[0];
        coordis[1] = ARISA[1];
      }
      else if (Navpoint == "ARKAD") {
        coordis[0] = ARKAD[0];
        coordis[1] = ARKAD[1];
      }

and so on and so forth…

That is clearly not a whole sketch so it makes it problematic for people wanting to help you.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Can you take AR out of the waypoint names?
Or break them into a section where if the waypoint starts with AR then drop down and look for next 3 letters.
Could add into into savings if there are enough involved.

Depending on the IO, maybe try a Atmega1284P with 16K SRAM.
Or jump to a processor with more SRAM. Easy to add it to the IDE and try it.

norbyx:
I've been trying to make the Arduino check a lot of IF statements. So many that it says that I'm using 529% of the SRAM memory.

Wow, wow, wow :wink: Why did you not stop when you got the warnings? You have some serious problems.

Note:

      if (Navpoint == "ARBOL") {

"ARBOL" is not in PROGMEM, same applies for all the others.

Except for that, it looks like you're making heavy use of String (capital S) objects. Even if you manage to get it at a reasonable 75%, you will probably run into unexpected runtime behaviour (read: crashing) if you also make heavy use of String concatenation.

Post your full sketch (you will probably have to attach it).

You should be able to store an array in PROGMEM that contains the text for each IF statement, as well as the values being assigned in each IF statement, then use a FOR loop to go through the array until a match if found. Without seeing more of your code, its hard to give a specific example.

I can't use any memory extension shield for the Arduino because i'm alreasy using a shield for a UTFT display...

Why are the two mutually exclusive ?
Designed properly shields can be stacked or can use an interface such as SPI, i2C etc and be connected with just a couple of wires so no actual shield is required

ballscrewbob:
That is clearly not a whole sketch so it makes it problematic for people wanting to help you.

Here’s the entire code
I’ve already tried some things to move the waypoint names into PROGMEM without any luck
And sorry that those are 15 sepparate files, for some reason I can’t upload them together in a folder…

00_Navpoints.ino (815 KB)

arial_normal.c (16.1 KB)

Cockpit_Arduino_MCDU.ino (5.48 KB)

F_PLN.ino (2.46 KB)

Flightplan.ino (659 Bytes)

General.ino (8.05 KB)

hallfetica_normal.c (16.1 KB)

I2C.ino (2.19 KB)

IFFS.ino (795 KB)

INIT_1.ino (15.4 KB)

Matrix.ino (26.1 KB)

MENU.ino (4.27 KB)

Receiver.ino (607 Bytes)

various_symbols.c (16.1 KB)

sterretje:
Wow, wow, wow :wink: Why did you not stop when you got the warnings? You have some serious problems.

I've used Microsoft Excel and Word to create all the functions at once...
So I added all the 29'000 lines at once and that was my first warning xD

sterretje:
Note:"ARBOL" is not in PROGMEM, same applies for all the others.

That's true. I only put the coordinates into PROGMEM because if I try to put a String into PROGMEM it fills up the dynamic memory instead of the program storage space for some reason..

I can’t think of a way to put Strings in PROGMEM.
Another good reason not to use them.

TheMemberFormerlyKnownAsAWOL:
I can't think of a way to put Strings in PROGMEM.
Another good reason not to use them.

So what data type should I use for text?

string (char array)

The Navpoints file is storing the data in PROGMEN, but instead of using separate arrays for each navpoint's location, make a struc that has a pointer to the text, as well as the associated coordinates, then make an array of the struc, so you can search through the array looking for the matching text. Use strcmp_P() to do the comparison.

A struct combines associated information. Your navigation points and coordinates are associated. For my own practice, I created the below code. I’m not a PROGMEM ‘expert’, so others can point out improvements or mistakes (though it seems to work correctly).

// structure to store navigation point
struct NAVPOINT
{
  char name[10];
  float coordis[2];
};

// array of navigation points in progmem
const NAVPOINT Navpoints[] PROGMEM =
{
  {"ARBOL", {43.516945, 70.854446}},
  {"ARDAG", {51.284443, 20.69389}},
  {"ARDOK", {43.404999, 34.105}},
  {"ARGES", {44.082222, 26.826666}},
  {"ARGUT", {43.433334, 43.933334}},

  {"ARIES", {46.384548, 23.449244}},
  {"ARISA", {51.490002, 50.548332}},
  {"ARKAD", {52.866665, 44.099998}},
  {"ARKAM", {47.193054, 64.539169}},
  {"ARKAN", {54.666668, 94.98333}},
};

void setup()
{
  Serial.begin(57600);

  // navigation point to look for
  char Navpoint[10] = "ARIES";

  // temporary copy
  NAVPOINT tmp;
  // flag
  bool found = false;

  for (uint16_t navCnt = 0; navCnt < sizeof(Navpoints) / sizeof(Navpoints[0]); navCnt++)
  {
    // get navpoint from progmem
    memcpy_P(&tmp, &(Navpoints[navCnt]), sizeof(NAVPOINT));

    // compare
    if (strcmp(tmp.name, Navpoint) == 0)
    {
      found = true;
      break;
    }
  }

  if (found == false)
  {
    Serial.println("Not found");
  }
  else
  {
    Serial.print("Found ");
    Serial.println(tmp.name);
    Serial.println(tmp.coordis[0], 6);
    Serial.println(tmp.coordis[1], 6);
  }
}

void loop()
{
}

If a navigation point is found, tmp will contain the relevant data. You need to use the flag to determine if the navigation point was found.

Stats:

5 nav points
Sketch uses 3546 bytes (1%) of program storage space
Global variables use 226 bytes (2%) of dynamic memory

10 nav points
Sketch uses 3636 bytes (1%) of program storage space
Global variables use 226 bytes (2%) of dynamic memory

As can be seen, increasing the number of navigation points by 5 increases program memory usage by 5x18 bytes (the size of NAVPOINT) and does not affect SRAM.

If you use C strings, you’ll be able to use strcmp_P() for comparisons..
.

westfw:
If you use C strings, you’ll be able to use strcmp_P() for comparisons..
.

I've tried that option but i'm still using 211% of program storage space, so I don't know if the struct version would be any better in terms of saving space...

One other thing I've thought about was to store all of that information (that now is in PROGMEM) in an SD card an access it from there. Does anyone know if that would save any space or does the library and the function use up a lot?

norbyx:
I've tried that option but i'm still using 211% of program storage space, so I don't know if the struct version would be any better in terms of saving space...

211% of program memory, not dynamic memory (ram) ?

Can you post the code you are using to store the data? If you really are using 211% of the program memory in a mega then you have added a substantial amount more data than was previously posted.

You have two floats (2x4=8 bytes) and a 5 character string to store.
Am I correct?
And the 5 character string is always uppercase A-Z. No digits, symbols or lowercase?

Sorry, but I cannot download and view your attachments on my mobile device to check.

How many of these items do you have to store?

This information will give you an upper/lower bound for how much PROGMEM you need to store your data and determine if it is even possible.

Also some compression may be possible. For example if only A-Z you may be able to reduce each character to 5 bits.

Before considering external EEPROM I’d be looking at parts or devices with more PROGMEM.

An SD card will help; although the library is quite big, I'm sure that you will reduce your code size to acceptable levels. For the library, it does not matter if you have 1 navigation point plus to floats in a file or 6000.

There are 6568 navigation points. As @pcbbc says, calculate. 6 bytes for the name (including termintor) and 2x4 bytes for the position (floats), you will need 91,952 bytes to store that information. I'm not sure if you have other associated information.

Add about 10k for fontdata types.

Notes
1)
I used 10 bytes for the 'name' element; you can reduce it to 6 if all names are 5 characters.
2):
If you post your new code, please do so as a ZIP file; easier to copy over. Give the zip the name of your main ino file.

The mega has 256K of flash memory, so the data should fit, but there are size limitations for an array so storing the text and coordinates in a single array will not work.

david_2018:
211% of program memory, not dynamic memory (ram) ?

Can you post the code you are using to store the data? If you really are using 211% of the program memory in a mega then you have added a substantial amount more data than was previously posted.

The thing is, I actually have less data in the IF statements xD

Cockpit_Arduino_MCDU.zip (224 KB)