Problems with arrays from a serial input.

I just started writing a program today that reads an sms (seeed studio gprs shield) using software serial, then saves it to an array, and the contents of that array eventually will define variables, so I can change variables using a cellphone.

for example if I sent a SMS to the arduino that said “PROG=var=cheese” I would assign var the value “cheese”.

I plan on all the SMS being the same length so that I could write a program that says something like this:

if(array[0] == 'P' && array[1] == 'R' && array[2] == 'O' && array[3] == 'G')
{    
    if(array[5] == 'v' && array[5] == 'a' && array[5] == 'r'){
        var= (the rest of the text message);
}

Today is my first day messing with arrays, but when I run the code I included below, it seems like the array is picking a new starting point every time… why?

Here is the code I started:

#include <SoftwareSerial.h>
#include <string.h>
char command = 0; //Conditional Programming Variable changes with SMS content.
char incoming_char = 0;
SoftwareSerial cellOld(7, 8); //my shield uses these pins for software serial.
HardwareSerial & cell = Serial2;
//These variables are for timing the Receive SMS command.
unsigned long currentMillis = 0; 
unsigned long interval = 30000; //Amount of time to wait between Receive SMS commands
char inChar[255];
char array[29] = {
  '\0'};
char var[6];
String var2;
int i = 0;

void setup()
{
  Serial.begin(19200);
  Serial.print("Starting modem communication...");
  cell.begin(19200);
  delay(12000);




  Serial.print("OK\nIntroduce your AT commands:\n"); 

  currentMillis = millis();

  cell.print('AT+CMGF=1');

  delay(1000);


  cell.println("AT+CMGR=1,0");
  cell.print("\r");

}

void loop()
{ 
  
  //CHECK FOR SMS
  if((millis() - currentMillis) > interval)
  { 
    //Serial.println(array);
    cell.println("AT+CMGR=1,0");
    cell.print("\r");
    currentMillis = millis();
  }

  if(cell.available() > 28)
  { 
    incoming_char=cell.read();

    if((incoming_char >= ' ') && (incoming_char <= 'z') ){

      for (int i=0; i < 28; i++)
      {   

        array[i] = cell.read();

      }
      cell.flush();  
    }
    Serial.println("---------");

    Serial.println(array);

    Serial.println("---------");

    Serial.print(array[22]);
    Serial.print(array[23]);
    Serial.print(array[24]);
    Serial.print(array[25]);
    Serial.println(array[26]);
    Serial.println("---------");


  }
  /*if(array[22] == 'H' && array[23] == 'e' && array[24] == 'l' && array[25] == 'l' && array[26] == 'o'){
   Serial.println("...........");
   
   var[0] = array[22];
   var[1] = array[23];
   var[2] = array[24];
   var[3] = array[25];
   var[4] = array[26];
   
   
   //Serial.println("...........");
   
   
   
   Serial.println("var: ");
   Serial.println(var);
   
   
   
   }   
   */


  //To use serial monitor for AT commands
  if(Serial.available() > 0)
  {
    incoming_char = Serial.read();
    cell.print(incoming_char);
  }

}

Here is an example of my serial monitor to help you debug:

Starting modem communication…OK
Introduce your AT commands:

5665AT+CMGR=1,0

+CMGR: "

MGR:


EC READ","+18055565847","","

“,”",


3/09/26,16:30:00-28"
Hello


Hello


3/09/26,16:30:00-28"
Hello


Hello


3/09/26,16:30:00-28"
Hello


Hello


3/09/26,16:30:00-28"
Hello


Hello


K
AT+CMGR=1,0

+CMGR: "R

GR: "


C READ","+18055565847","","1

,"","


/09AT+CMGR=1,0

+CMGR: "R

GR: "


C READ","+18055565847","","1

,"","


/09/26,16:30:00-28"
Hello


ello



/09/26,16:30:00-28"
Hello


ello



/09/26,16:30:00-28"
Hello


ello



K
AT+CMGR=1,0

+CMGR: "R

GR: "


C READ","+18055565847","","1

,"","


/09/AT+CMGR=1,0

+CMGR: "

MGR:


EC READ","+18055565847","","

“,”",


3/09/26,16:30:00-28"
Hello


Hello

Make sure the char array that you are using to hold the incoming message is null terminated. Then you can use functions such as strcmp() to check for an exact match, strtok() to separate a string into parts, strstr() to test whether one string contains another string.

I plan on all the SMS being the same length

Bad idea. Plan, instead, on using a end-of-packet marker so that you can tell when the complete packet has arrived. If you don't, you'll get all done, only to discover that you left out a function that won't fit in the packet size you have defined, and you'll have to rewrite a lot of stuff, and reprogram yourself to send new message length stuff. You'll forget sometime, and send the old format, and lock the Arduino up.

Thanks as always guys, Arduino should pay you guys for helping so much.

That or you should put a bitcoin wallet address in your footer. ;)