Memory used too much.

#include <GSM.h>
                        
#define PINNUMBER ""

                                   
#include "Arduino.h"
void setup();
void loop();
GSM gsmAccess;
GSM_SMS sms;


                                                   
char senderNumber[20];

void setup()
{
                                                                
  Serial.begin(9600);
  while (!Serial) {
    ;                                                             
  }

  Serial.println("SMS Messages Receiver");

                     
  boolean notConnected = true;

                         
  while (notConnected)
  {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

void loop()
{
 
  char c[5];
  int cust,cur;
  cur=001;
  char s1[200];
  char qnum[10][20];
                                      
  if (sms.available())
  {
    Serial.println("Message received from:");

                        
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);

                                     
                                                       
    if (sms.peek() == '#')
    {
      Serial.println("Discarded SMS");
      sms.flush();
    }

                                                
               for(int i=0;i<5;i++)
               {
               c[i] = sms.read();
               }
               Serial.print(c);
                                                        
               cust=atoi(c);
               Serial.print("\ncustomer number = ");
               Serial.print(cust);
                                 if(cust<=10&&cust>=01)
                                 {
                                   if(cust>cur&&cust-cur<=5)
                                   {
                                                         
                                        sms.beginSMS(senderNumber);
                                        sprintf(s1,  "Welcome to UPSI\nCurrent Number : %d\nPlease return to the service counter for your service. Thank you. " , cur); 
                                        sms.print(s1);
                                        sms.endSMS();
                                   }
                                   else if(cust-cur>5)
                                   {
                                        sms.beginSMS(senderNumber);
                                        sprintf(s1,  "Welcome to UPSI\nCurrent Number : %d\nQueue notification activated, sms will be send when your ticket is near. Thank you. " , cur);
                                        strcpy(qnum[cust-1], senderNumber);
                                        Serial.println(qnum[cust-1]);
                                        sms.print(s1);
                                        sms.endSMS();
                                   }
                                   else if(cust<cur)
                                   {
                                        sms.beginSMS(senderNumber);
                                        sprintf(s1,  "Welcome to UPSI\nCurrent Number : %d\nSorry, this ticket is expired. Please request a new ticket. Thank you. " , cur);
                                        sms.print(s1);
                                        sms.endSMS();
                                   }
                                 }
                                 else
                                 {
                                        sms.beginSMS(senderNumber);
                                        sprintf(s1,  "Welcome to UPSI\nCurrent Number : %d\nSorry, this ticket is invalid. Please request a new ticket. Thank you. " , cur);
                                        sms.print(s1);
                                        sms.endSMS();
                                 }
            
                                              
            Serial.println("\nEND OF MESSAGE");

                                       
    sms.flush();
    Serial.println("MESSAGE DELETED");
  }

  delay(1000);

}

Hi,

This is the code i am doing, anyone know how to simplified this program or optimize the memory usage?

Regard and thanks.

Need to create a serial read to read input all the time(to increase the current number by input N) while this program is running but i have no idea too,anyone can give me some idea?

anyone know how to simplified this program or optimize the memory usage?

Yes.

Serial.println(“SMS Messages Receiver”);
should be:
Serial.println(F(“SMS Messages Receiver”));

  char s1[200];
  char qnum[10][20];

Why are you allocating space for 400 characters?

    if (sms.peek() == '#')
    {
      Serial.println("Discarded SMS");
      sms.flush();
    }

                                                
               for(int i=0;i<5;i++)

You should be ashamed of yourself for this awful indenting. If you really can’t be bothered to indent properly as you go, you should at least use Tools + Auto Format before posting code.

               for(int i=0;i<5;i++)
               {
               c[i] = sms.read();
               }

You know that there is some data to read. You have no idea how much. Yet, you proceed to read 5 bytes. That is just plain wrong.

               Serial.print(c);
                                                        
               cust=atoi(c);

Then, you assume that the 5 characters are a NULL terminated array of chars and call functions that expect strings. They are NOT, so this code is just plain wrong.

It only makes sense to optimize memory based on correctly functioning code. Yours isn’t, so optimization is premature.

PaulS:
Yes.

Serial.println(“SMS Messages Receiver”);
should be:
Serial.println(F(“SMS Messages Receiver”));

  char s1[200];

char qnum[10][20];



Why are you allocating space for 400 characters?

So i should allocating for the maximum i will used?



if (sms.peek() == ‘#’)
    {
      Serial.println(“Discarded SMS”);
      sms.flush();
    }

for(int i=0;i<5;i++)



You should be ashamed of yourself for this awful indenting. If you really can't be bothered to indent properly as you go, you should at least use Tools + Auto Format before posting code.

Sorry i still a beginner.



for(int i=0;i<5;i++)
              {
              c[i] = sms.read();
              }



You know that there is some data to read. You have no idea how much. Yet, you proceed to read 5 bytes. That is just plain wrong.

It is planed to receive only 1 to 4 character only.



Serial.print(c);
                                                       
              cust=atoi(c);



Then, you assume that the 5 characters are a NULL terminated array of chars and call functions that expect strings. They are NOT, so this code is just plain wrong.

It only makes sense to optimize memory based on correctly functioning code. Yours isn't, so optimization is premature.

Hmmm, i can't understand this.i convert the string to integer for comparison purpose.

Hi, PaulS

Thanks for replying, i appreciate it so much.I will edit again and show it here again later.

Thanks.