Random function is using too much memory?

The project I am working on now takes a random number, after a button press and then uses this random number display a message on an LCD Screen. There are a total of 28 messages.

Whenever I use MessageNumber = random(28), the Global variable memory jumps from 20% to 230% being used.
The strange thing is that the MessageNumber is declared locally and is not a global variable.

I understand it is something to do with the large number of switch cases, but i am unsure of how to implement the variables correctly.

void messages()
{
  //in this function messages will be displayed on screen

  int MessageNumber = random(28);
  

  switch (MessageNumber)
  {
    case 0:
      {
         tft_Message(); tft.println(" 1 of 28");

          //displaymessage

       } break; 

case 1:
      {
         tft_Message(); tft.println(" 2 of 28");

          //displaymessage

       } break; 

case 3:
      {
         tft_Message(); tft.println(" 3 of 28");

          //displaymessage

       } break; 

case 4:
      {
         tft_Message(); tft.println(" 4 of 28");

          //displaymessage

       } break; 

} // etc to case 27

delay(5000);

This is the part of the body of code which is causing problems.

It seems that every time the “int MessageNumber = random(NUMBER)” increases, and extraordinary amount of memory is being used for it.

Also I should say that random(seed) is being used to get the random number.

I know it is the fact that this number is being inside the switch which is why so much memory is being used, but don’t know why or have a workaround.

Help would be greatly appreciated.

Thanks.

It is probably the strings you are printing within your case statements that are consuming a lot of memory. Use the F() macro to store then in program memory instead.
e.g.

tft_Message(); tft.println(F(" 3 of 28"));

You could factor your code and save both ROM and RAM - the string " of 28" seems repeated.

blh64:
It is probably the strings you are printing within your case statements that are consuming a lot of memory. Use the F() macro to store then in program memory instead.
e.g.

tft_Message(); tft.println(F(" 3 of 28"));

You’re absolutely right.

Didn’y even occur to me that it just wouldn’t read through the other cases if the number was too low.
Thanks for this!

AWOL:
You could factor your code and save both ROM and RAM - the string " of 28" seems repeated.

Will be doing this aswell, cheers fellas.

(deleted)

spycatcher2k:

tft_Message()

tft.print(" “);
tft.print(1 + MessageNumber );
tft.print(F(”  of 28"));

Factoring. K++.