*SOLVED* Writing different fonts on an graphic LCD (memcpy) Video included

I got my Nokia 5110 screen up and running and I have made a count-down timer for the game Alias. The program has a menu where you can change parametes and a main screen which consists of a big custom font that shows the remaining time.

The problem is that I can´t figure out how to change the big font.

Each big font is stored in a two dimensional array, for example:

static const byte fontti1[][48] =
{
  {0xC0, 0xF0, 0xFC, 0x3E, 0x0E, 0x07, 0x03, 0x03, 0x03, 0x07, 0x0E, 0x3E, 0xFC, 0xF0, 0xC0,0x00, //nolla
  0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,0x00, 
  0x01, 0x07, 0x1F, 0x3E, 0x38, 0x70, 0x60, 0x60, 0x60, 0x70, 0x38, 0x3E, 0x1F, 0x07, 0x01,0x00 
  },
{0x00, ..... and so on

My function for plotting the character works really well as long as I only use one font. When I want to be able to change the font (meaning using a different 2 dimensional, same size array) the whole program crashes from the beginning and I get only gibberish on the screen. This happends allso in the menu where only the basic 5x8 ASCII characters are used. Why is this happening??

The function that draws a big number on the screen is below. It is the perfectly working version, but it can only use "fontti2"

// this function goes to the demanded XY position and draws a big character from a 2 dimensional array
int i = 0;
  byte j = 16;          // määrittää kuinka monta hexaa käsitellään /rivi
  for(byte k = 1; k < 4;k++)    //kirjottaa joka riville oikeen tiedon
  {
    gotoXY(paikka, k);
    for(i; i < j; i++)
     {
      Lahetys(DCd, fontti2[character][i]);       //this function send a byte of data to the screen
      }
    j = j+16;
  }

If I add the following code anywhere in the program, the whole Arduino just goes wild! It either will draw something random or just leave a blank screen no matter which buttons I push (one is an interrupt which goes to the menu).

// fonttiNum tells which font to use, fonttiBuffer = byte fonttiBuffer[11][48], character = a number between 0-10 to tell which character wants to be copied.
if(fonttiNum == 1){memcpy(fonttiBuffer, fontti1[character], sizeof(byte)*50);}
 if(fonttiNum == 2){memcpy(fonttiBuffer, fontti2[character], sizeof(byte)*50);}

Then again, the memcpy(...) WORKS, IF I remove the fonttinum == 2 statement.

This happens no matter how I put my code. I have tried doing the font change in the main menu directly after changing "fonttiNum" and in the function which draws the big character on the screen. The result is still the same.

I tried allso to do the copying manually by saying, in a for sentence : fonttiBuffer[k][j] = fontti1[k][j]. The same error happends.

Please help me with the problem! I have no idea what I could do.

You may be running out of RAM - use PROGMEM for the fonts?

Doesn´t anyone have an idea how to solve the problem?

Then again, the memcpy(...) WORKS, IF I remove the fonttinum == 2 statement.

If you don't reference some out of bounds memory, you mean.

Doesn´t anyone have an idea how to solve the problem?

MarkT did. Too bad you didn't like it.

MarkT:
You may be running out of RAM - use PROGMEM for the fonts?

Wow thanks! This was the answer!

I will write some working code and upload a video ones I get it working correctly :slight_smile:

AAAnnd done!!

Thank you so much for the RAM tip! It´s quite obvious that it was the reason, now that somone said it.

A really nice site explainig PROGMEM: ClicketyClick!
A really nice site about using the screen: Nokia 5110 example

And the video I promised:

Some code how I used PROGMEM:

// One font to progmem

static byte fontti1[11][48] PROGMEM =          
{
  {0xC0, 0xF0, 0xFC, 0x3E, 0x0E, 0x07, 0x03, 0x03, 0x03, 0x07, 0x0E, 0x3E, 0xFC, 0xF0, 0xC0,0x00, //nolla
  0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,0x00, 
  0x01, 0x07, 0x1F, 0x3E, 0x38, 0x70, 0x60, 0x60, 0x60, 0x70, 0x38, 0x3E, 0x1F, 0x07, 0x01,0x00 
  },
  {0x00, 0x00, 0x00, 0x30, 0x30, 0x30, 0x3.......
// The function that selects the font. fonttiNum is changed in the menu (see video)

void fonttiValinta(void)
{
  if(fonttiNum == 1){
  for(byte j = 0; j < 11; j++){
    for(byte i = 0; i < 48; i++)
    {
      fonttiBuffer[j][i] = pgm_read_byte(&(fontti1[j][i]));
    }}
  }
  if(fonttiNum == 2){
    for(byte j = 0; j < 11; j++){
    for(byte i = 0; i < 48; i++)
    {
      fonttiBuffer[j][i] = pgm_read_byte(&(fontti2[j][i]));
    }}
  }
}
//This function draws a big character on the screen. It takes in the wanted character and the place on the screen to be written

void isoNumero(char character, byte paikka){
int i = 0;
  byte j = 16;          // määrittää kuinka monta hexaa käsitellään /rivi
  for(byte k = 1; k < 4;k++)    //kirjottaa joka riville oikeen tiedon
  {
    gotoXY(paikka, k);
    for(i; i < j; i++)
     {
      Lahetys(DCd, fonttiBuffer[character][i]);
      }
    j = j+16;
  }
}