Array Size on Ardiuno Mega 2560

I bought a [graphic LCD for my Arduino because I wanted something to experiment with before my parts arrive from China. I’ve gotten Conway’s Game of Life to work on a 16x16 LED matrix I have laying around, so I decided a simple exercise with this LCD would be to get a 84x48 board going.

I’m a little confused how to do this, as it doesn’t seem as straight forward as I hoped. Simply increasing the boundaries and switching up the display function only worked up to 48x48. Going much larger than that causes the screen to flash the test image from the LCD’s library test code. This image is the first thing that appears when the LCD is initialized, so it seems like the Arduino keeps resetting itself and reinitializing the LCD. The orange LED on the Arduino flashes as well.

I realize that my method for executing this program isn’t inefficient in terms of memory, but 2 arrays at 84x48 bytes doesn’t seem like it should be breaking my back on an Arduino Mega 2560. Any insight would be very appreciated.

[Link to code

[Adafruit_PCD8544.h

[Adafruit_GFX.h](/***********************************This is a our graphics core library, for a - Pastebin.com)](/*********************************************************************This is - Pastebin.com)](#include <Adafruit_GFX.h>#include <Adafruit_PCD8544.h>Adafruit_PCD8544 dis - Pastebin.com)](http://www.sparkfun.com/products/10168)

2 arrays at 84x48 bytes doesn’t seem like it should be breaking my back on an Arduino Mega 2560

Are you sure? Your program only leaves 120 bytes for operating overhead at those dimensions. I’m not familiar enough with the Arduino environment to know if that is enough. The good news is that switching to bitwise storage will cut your memory by a factor of 8.

I’m pretty sure that you won’t be able to make it run with that large of an array. As sethcim said, it is a monotone LCD, so you are best doing it bitwise. You could make it easy for yourself and do it like this:

unsigned long long array[2][84];

byte getPixel(byte x, byte y, byte whichArray){
  if(whichArray> 1){
    whichArray = 1;
  }
  if(x > 83){
    x = 83;
  }
  if(y > 47){
    y = 47;
  }
  unsigned long long temp = (1ULL << y);
  if(array[whichArray][x] & temp){
     return 1; //the yth bit was set, so return 1.
  } else {
     return 0; //the yth bit was clear, so return 0;
  }
}

void setPixel(byte x, byte y, byte whichArray, byte pixel){
  if(whichArray> 1){
    whichArray = 1;
  }
  if(x > 83){
    x = 83;
  }
  if(y > 47){
    y = 47;
  }
  unsigned long long temp = (1ULL << y);
  if (pixel){
    array[whichArray][x] |= temp;
  } else {
    array[whichArray][x] &= ~temp;
  }
}

Basically, there is one array. The first index ([2]) is whether it is the current or next board. And the second index is the x coordinate. The y coordinates are each bit in the unsigned long long variable.

So say your array ‘a’ would be index[0], so to get a pixel you would call:

pixelValue = getPixel(x, y, 0);

and to set one it would be:

setPixel(x, y, 0, pixelValue);

Then your array ‘b’ would be index[1], so to get a pixel you would call:

pixelValue = getPixel(x, y, 1);

and to set one it would be:

setPixel(x, y, 1, pixelValue);

Also, in your code, you can make if statements shorter, e.g. :

if (a[e-1][f-1]==1 && e!=0 && f!=0)

Can be written as:

if (a[e-1][f-1] && !e && !f)

and with the memory saving method above would become:

if (getPixel(e-1,f-1,0) && !e && !f)

Thanks for the replies. I should have realized that there wasn’t enough SRAM. I did some reading on bit packing since I last checked here.

[Revised code

It even works on an Uno. Still, the code is sloppy and I need to add borders. Definitely much to be learned. Hopefully, some more finagling might increase the frame rate. Right now I’m guessing it’s around 8 frames per second.](https://pastebin.galador.org/show/s0QKz77p87T68q1Gbeam/)