Hi! I'm trying to write a code for my arduino project. it's an SMS-based Led matrix display. I seem to have problems with SRAM memory because of the size of the code. can you help me optimize it?

#include "cmap.h"
#include <SIM900.h>
#include <sms.h>
#include <SoftwareSerial.h>

#define INTEN 6500      //speed the higher the value, the slower the scroll

//-------------------GSM VARIABLES-----------------------

boolean started = false;
char regnum[] = "639339182267";
char passRx[] = "default#";           //holds password, must be 8 char only! and ends with #
char smsdata[160];         //sms only accept 160 char, remaining for header
char numberRx[20];         //holds sender number
char LED_DATA[] = " Hello! ";

//-----------------LED MATRIX VARIABLES------------------

const char CLK = 5; 
const char DAT = 6; 
const char STR = 7; 
const char K0 = A0; 
const char K1 = A1; 
const char K2 = A2; 
const char K3 = A3; 
const char K4 = 8; 
const char K5 = 9; 
const char K6 = 10; 
const char K7 = 11;

char Display_Ram[38];
char count; 
unsigned char char_pointer = 0x00;
unsigned char mask;
unsigned char shift_counter;
int message_no;
int pins[] = { CLK, DAT, STR, K0, K1, K2, K3, K4, K5, K6, K7 };
int mask_data[] = {0x00, 0x00, 0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
unsigned int d;

void setup() 
{ 
  gsmsetup();
  pinsetup(); 
}

void loop()
{
//  checkSMS();
  Display();
  shift(); 
}


void gsmsetup(void)
// ---------- serial setup ----------
{
  Serial.begin(9600);
  gsm.begin(4800);  
}

void pinsetup()
{
  for (int x=0;x<=10;x++){  
    pinMode(pins[x], OUTPUT);     
  } 

  for (int y=3;y<=10;y++){
    digitalWrite(pins[y],HIGH);  
  }  

  for(d=0;d<32;d++){
    clock();
  }
  strobe();
  d = 0;
}

void checkSMS()
// ---------- check if there is new message ----------
{
  int i;
  gsm.readSMS(smsdata, 160, numberRx, 20);
  delay(1000);
  if (strncmp (numberRx, regnum, 11) == 0)
    {
      if (strncmp (smsdata, passRx, 8) == 0)
      {
        while (smsdata[i] != '~')
        {
          smsdata[i] = smsdata[i+9];     
          i++; 
        }
        i = 0;
 //       Serial.print("\nParsed SMS: ");

        while (smsdata[i] != '~')
        {
//          Serial.print(smsdata[i]);
          i++; 
        }
 //       Serial.println("");
      }
    }
}

void Display(void)
{
  mask = 0x01;
  scan();
  digitalWrite(K7,LOW);
  for(d=0;d<INTEN;d++){}
  digitalWrite(K7,HIGH);

  mask = 0x02;
  scan();
  digitalWrite(K6,LOW);
  for(d=0;d<INTEN;d++){}
  digitalWrite(K6,HIGH);

  mask = 0x04;
  scan();
  digitalWrite(K5,LOW);
  for(d=0;d<INTEN;d++){}
  digitalWrite(K5,HIGH);

  mask = 0x08;
  scan();
  digitalWrite(K4,LOW);
  for(d=0;d<INTEN;d++){}
  digitalWrite(K4,HIGH);

  mask = 0x10;
  scan();
  digitalWrite(K3,LOW);
  for(d=0;d<INTEN;d++){}
  digitalWrite(K3,HIGH);

  mask = 0x20;
  scan();
  digitalWrite(K2,LOW);
  for(d=0;d<INTEN;d++){}
  digitalWrite(K2,HIGH);

  mask = 0x40;
  scan();
  digitalWrite(K1,LOW);
  for(d=0;d<INTEN;d++){}
  digitalWrite(K1,HIGH);

  mask = 0x80;
  scan();
  digitalWrite(K0,LOW);
  for(d=0;d<INTEN;d++){}
  digitalWrite(K0,HIGH);
}

void scan(void){
  for (count=32;count>(-1);count--){
    if ((Display_Ram[count] & mask) == mask)
      digitalWrite(DAT,LOW);
    else
      digitalWrite(DAT,HIGH);
    clock();
  }
  strobe();
}

void clock(void){
  digitalWrite(CLK,HIGH);           // clock hi
  digitalWrite(CLK,LOW);                // clock low
}

void strobe(void){
  digitalWrite(STR,HIGH);           // strobe hi
  digitalWrite(STR,LOW);            // strobe low
}

void shift(void) 
{
  for (int val=0; val<37; val++)
  {
    Display_Ram[val] = Display_Ram[val+1];
  }

  shift_counter++;
  if(shift_counter == 6){
    shift_counter = 0;
    load();
  }
}

void load(void)
{
  char counter1;
  char *LED_DATA= announcement(message_no);                   
  Serial.println(smsdata);

  if(LED_DATA[char_pointer]  == '~' || LED_DATA[char_pointer]  == '\0')
  {
    char_pointer = 0;
    message_no++;   
    goto exit;
  }

  for(counter1 = 0; counter1 < 5; counter1++)
  {
    Display_Ram[32 + counter1] = character_data[(LED_DATA[char_pointer] - 0x20)][counter1];
  }

  Display_Ram[37] = 00; 
  char_pointer++;
  exit:
  counter1++;
}

and here's CMAP.H

const unsigned char character_data[][6] =
{
    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },       //20-
    { 0x00, 0x00, 0x00, 0xF2, 0x00, 0x00 },       //21-!
    { 0x00, 0x00, 0xE0, 0x00, 0xE0, 0x00 },       //22-"
    { 0x28, 0xFE, 0x28, 0xFE, 0x28, 0x00 },       //23-#
    { 0x24, 0x54, 0x7E, 0x54, 0x48, 0x00 },       //24-$
    { 0xC4, 0xC4, 0x10, 0x26, 0x46, 0x00 },       //25-%
    { 0x6C, 0x92, 0xAA, 0x44, 0x1A, 0x00 },       //26-&
    { 0x00, 0x00, 0xA0, 0xC0, 0x00, 0x00 },       //27-'
    { 0x00, 0x00, 0x38, 0x44, 0x82, 0x00 },       //28-(
    { 0x00, 0x82, 0x44, 0x38, 0x00, 0x00 },       //29-)
    { 0x28, 0x10, 0x6C, 0x10, 0x28, 0x00 },       //2A-*
    { 0x10, 0x10, 0x6C, 0x10, 0x10, 0x00 },       //2B-+
    { 0x00, 0x00, 0x0A, 0x0C, 0x00, 0x00 },       //2C-,
    { 0x10, 0x10, 0x10, 0x10, 0x10, 0x00 },       //2D--
    { 0x00, 0x00, 0x06, 0x06, 0x00, 0x00 },       //2E-.
    { 0x04, 0x08, 0x10, 0x20, 0x40, 0x00 },       //2F-/
    { 0x7C, 0x82, 0x82, 0x82, 0x7C, 0x00 },       //30
    { 0x00, 0x42, 0xFE, 0x02, 0x00, 0x00 },       //31
    { 0x42, 0x86, 0x8A, 0x92, 0x62, 0x00 },       //32
    { 0x84, 0x82, 0xA2, 0xD2, 0x8C, 0x00 },       //33
    { 0x18, 0x28, 0x48, 0xFE, 0x08, 0x00 },       //34
    { 0xE4, 0xA2, 0xA2, 0xA2, 0x9C, 0x00 },       //35
    { 0x3C, 0x52, 0x92, 0x92, 0x0C, 0x00 },       //36
    { 0x80, 0x8E, 0x90, 0xA0, 0xC0, 0x00 },       //37
    { 0x6C, 0x92, 0x92, 0x92, 0x6C, 0x00 },       //38
    { 0x60, 0x92, 0x92, 0x94, 0x78, 0x00 },       //39
    { 0x00, 0x00, 0x6C, 0x6C, 0x00, 0x00 },       //3A-:
    { 0x00, 0x00, 0x6A, 0x6C, 0x00, 0x00 },       //3B-//
    { 0x00, 0x10, 0x28, 0x44, 0x82, 0x00 },       //3C-<
    { 0x28, 0x28, 0x28, 0x28, 0x28, 0x00 },       //3D-=
    { 0x00, 0x82, 0x44, 0x28, 0x10, 0x00 },       //3E->
    { 0x40, 0x80, 0x8A, 0x90, 0x60, 0x00 },       //3F-?
    { 0x7C, 0x82, 0xBA, 0xAA, 0xBA, 0x70 },       //40-@
    { 0x7E, 0x88, 0x88, 0x88, 0x7E, 0x00 },       //41-A
    { 0xFE, 0x92, 0x92, 0x92, 0x6C, 0x00 },       //42-B
    { 0x7C, 0x82, 0x82, 0x82, 0x44, 0x00 },       //43-C
    { 0xFE, 0x82, 0x82, 0x44, 0x38, 0x00 },       //44-D
    { 0xFE, 0x92, 0x92, 0x92, 0x82, 0x00 },       //45-E
    { 0xFE, 0x90, 0x90, 0x90, 0x80, 0x00 },       //46-F
    { 0x7C, 0x82, 0x92, 0x92, 0x5E, 0x00 },       //47-G
    { 0xFE, 0x10, 0x10, 0x10, 0xFE, 0x00 },       //48-H
    { 0x00, 0x82, 0xFE, 0x82, 0x00, 0x00 },       //49-I
    { 0x04, 0x02, 0x82, 0xFC, 0x80, 0x00 },       //4A-J
    { 0xFE, 0x10, 0x28, 0x44, 0x82, 0x00 },       //4B-K
    { 0xFE, 0x02, 0x02, 0x02, 0x02, 0x00 },       //4C-L
    { 0xFE, 0x40, 0x30, 0x40, 0xFE, 0x00 },       //4D-M
    { 0xFE, 0x20, 0x10, 0x08, 0xFE, 0x00 },       //4E-N
    { 0x7C, 0x82, 0x82, 0x82, 0x7C, 0x00 },       //4F-O
    { 0xFE, 0x90, 0x90, 0x90, 0x60, 0x00 },       //50-P
    { 0x7C, 0x82, 0x8A, 0x84, 0x7A, 0x00 },       //51-Q
    { 0xFE, 0x90, 0x98, 0x94, 0x62, 0x00 },       //52-R
    { 0x62, 0x92, 0x92, 0x92, 0x8C, 0x00 },       //53-S
    { 0x80, 0x80, 0xFE, 0x80, 0x80, 0x00 },       //54-T
    { 0xFC, 0x02, 0x02, 0x02, 0xFC, 0x00 },       //55-U
    { 0xF8, 0x04, 0x02, 0x04, 0xF8, 0x00 },       //56-V
    { 0xFC, 0x02, 0x1C, 0x02, 0xFC, 0x00 },       //57-W
    { 0xC6, 0x28, 0x10, 0x28, 0xC6, 0x00 },       //58-X
    { 0xE0, 0x10, 0x0E, 0x10, 0xE0, 0x00 },       //59-Y
    { 0x86, 0x8A, 0x92, 0xA2, 0xC2, 0x00 },       //5A-Z
    { 0x00, 0x00, 0xFE, 0x82, 0x82, 0x00 },       //5B-[
    { 0x40, 0x20, 0x10, 0x08, 0x04, 0x00 },       //5C-\

    { 0x00, 0x82, 0x82, 0xFE, 0x00, 0x00 },       //5D-]
    { 0x20, 0x40, 0xFE, 0x40, 0x20, 0x00 },       //5E-^
    { 0x01, 0x01, 0x01, 0x01, 0x01, 0x00 },       //5F-_
    { 0x80, 0x40, 0x20, 0x00, 0x00, 0x00 },       //60-`
    { 0x04, 0x2A, 0x2A, 0x2A, 0x1E, 0x00 },       //61-a
    { 0xFE, 0x12, 0x22, 0x22, 0x1C, 0x00 },       //62-b
    { 0x1C, 0x22, 0x22, 0x22, 0x04, 0x00 },       //63-c
    { 0x1C, 0x22, 0x22, 0x12, 0xFE, 0x00 },       //64-d
    { 0x1C, 0x2A, 0x2A, 0x2A, 0x18, 0x00 },       //65-e
    { 0x08, 0x3F, 0x48, 0x40, 0x20, 0x00 },       //66-f
    { 0x18, 0x25, 0x25, 0x25, 0x3E, 0x00 },       //67-g
    { 0xFE, 0x10, 0x20, 0x20, 0x1E, 0x00 },       //68-h
    { 0x00, 0x22, 0xBE, 0x02, 0x00, 0x00 },       //69-i
    { 0x02, 0x01, 0x21, 0xBE, 0x00, 0x00 },       //6A-j
    { 0x00, 0xFE, 0x08, 0x14, 0x22, 0x00 },       //6B-k
    { 0x00, 0x82, 0xFE, 0x02, 0x00, 0x00 },       //6C-l
    { 0x3E, 0x20, 0x18, 0x20, 0x1E, 0x00 },       //6D-m
    { 0x3E, 0x10, 0x20, 0x20, 0x1E, 0x00 },       //6E-n
    { 0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00 },       //6F-o
    { 0x3F, 0x24, 0x24, 0x24, 0x18, 0x00 },       //70-p
    { 0x18, 0x24, 0x24, 0x14, 0x3F, 0x00 },       //71-q
    { 0x3E, 0x10, 0x20, 0x20, 0x10, 0x00 },       //72-r
    { 0x12, 0x2A, 0x2A, 0x2A, 0x04, 0x00 },       //73-s
    { 0x20, 0xFC, 0x22, 0x02, 0x04, 0x00 },       //74-t
    { 0x3C, 0x02, 0x02, 0x04, 0x3E, 0x00 },       //75-u
    { 0x38, 0x04, 0x02, 0x04, 0x38, 0x00 },       //76-v
    { 0x3C, 0x02, 0x0C, 0x02, 0x3C, 0x00 },       //77-w
    { 0x22, 0x14, 0x08, 0x14, 0x22, 0x00 },       //78-x
    { 0x38, 0x05, 0x05, 0x05, 0x3E, 0x00 },       //79-y
    { 0x22, 0x26, 0x2A, 0x32, 0x22, 0x00 },       //7A-z
    { 0x00, 0x10, 0x6C, 0x82, 0x00, 0x00 },       //7B-{
    { 0x00 },                                     //7C-|-BLANK BAR
    { 0x00, 0x82, 0x6C, 0x10, 0x00, 0x00 },       //7D-}
    { 0x18, 0x24, 0x7E, 0x24, 0x08, 0x00 },
    { 0 }                                         //7F-DO NOT USE

};

char *announcement(int message_no1)
{
  char *LED_message[]={"LCoES Announcements         "};

  return LED_message[message_no1];
}

would appreciate your help! :) thanks!

To be honest I am very surprised that, this code cannot be saved to your arduino board. But to optimize it, can you state how many columns of LEDS you have and how many rows. Based on that I think it is possible to optimize your code.

You might also want to consider shift registers connected between your arduino board and LED Matrix. Then clocking the values out.

Edited 4 Years Ago by Software guy

Can you help me sir on how to upload all this code in my arduino board. Should i merge the two codes and upload simultaneously?

This article has been dead for over six months. Start a new discussion instead.