Need help with receiving sms

hello everyone,

i' ve connected a gsm shield to an arduino mega and i want to send sms from my phone and control the relay which is connected to mega. The arduino receives correctly the first sms and can control the relay, but when i send another sms id doesn't work.

this is the code i am using:

 char inchar;               

 int relay_1 = A9;
 int relay_2 = A10;


 void setup()
 {

 pinMode(relay_1, OUTPUT);
 pinMode(relay_2, OUTPUT);

 
 digitalWrite(relay_1, LOW);
 digitalWrite(relay_2, LOW);

  
 Serial1.begin(19200);
 delay(3000); 
 Serial1.println("AT+CMGF=1\r");
 delay(200);
 Serial1.println("AT+CSMS=1\r"); 
 delay(200);
 
 Serial1.println("AT+CNMI=2,2,0,0,0");

 delay(200);
 }

 void loop() 
 {


 if(Serial1.available() >0)
 {  
    delay(10);
    inchar=Serial1.read(); 
      if (inchar=='0')   
 {     

       inchar=Serial1.read(); 
       if (inchar=='#')
       {
             inchar=Serial1.read(); 
             if (inchar=='0')
             {
                 digitalWrite(relay_1, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(relay_1, HIGH);
             }
             
             
       delay(10);
             inchar=Serial1.read(); 
             if (inchar=='0')
             {
                 digitalWrite(relay_2, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(relay_2, HIGH);
             }
             
            
       }            
       
 } 

 Serial.println("AT+CMGD=1,4"); 
 
 } 

   //Serial.println("AT+CMGR=1\r");
   //delay(4000);
 }

Is there a problem in the loop which i can not see?. The format of the messages are supposed to be like 0#xx, where the two last digits (xx) are 0 for close and 1 for open the first and the second relay.

Thank you in advance.

I'd suggest you might put some Serial.print() on another serial port - so you can see what the modem is doing.
The +CMT: message returns a lot more content than just your 0/1 character.

There are many examples here and other places - but I'm glad to see that you avoided the String class !!