interrupts with conditions

PaulS:
Of course there is. If the current state of the switch doesn't match the previous state of the switch, a transition occurred. The transition was either a RISING edge, if the current state is HIGH, or a FALLING edge, if the current state is LOW. So, all you need to do is keep track of the previous state, by adding a new variable and assigning it a value at the end of loop. That value is the current state of the pin.

Today I tried to modified the code using your logic, I assigned a variable called "doorlock"

In the initial setup I stated declared a variablevolatile int doorlock;

In the void setup() I have declared

 pinMode(2, INPUT); //Door sensor monitor
 digitalWrite (2, LOW);  // internal pull-up resistor
  //When the Input goes from High to Low it will trigger a function called "doormonitor"
  doorlock = 0;

Finally in the main loop I added that the doorlock=1 after the relay_b is on

   if( sms.indexOf("onb") >= 0 ){
    digitalWrite(  relay_b, HIGH );
    doorlock=1;  
  }

I have also added a code that says...

if(digitalRead(2)==HIGH && doorlock==1)
        {digitalWrite(13,HIGH);
        delay (1000);
        doorlock=0;
        }
        else
        digitalWrite(13, LOW);

OUTPUT
After sending the "onb" command pin 13 glows only once & then goes off. Nothing happen if I short pin 2 with GND or VCC.

Expected Output
After the doorlock relay is high, the pin 2 will sense state change ONLY ONCE until the relay is activated again.

I guess I am missing something in my logic. Will help me find the error in the logic of the code.?
The full code I am working on is this...

#include <SoftwareSerial.h>
 

// EN: String buffer for the GPRS shield message
String msg = String("");
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
int SmsContentFlag = 0;
//control pins of relay.
int relay_a=4;
int relay_b=5;
int relay_c=6;
int relay_d=7;
int led = 13; // Interrupt monitor
//int door = 12; // Door Status monitor
int button=2; // Door Sensor monitor
volatile int doorlock;
 // Interrupt Service Routine (ISR)

void setup()
{
  Serial.begin(19200);                 // the GPRS baud rate
  // Initialize  PINs
  pinMode( 4, OUTPUT ); 
  pinMode( 5, OUTPUT ); 
  pinMode( 6, OUTPUT ); 
  pinMode( 7, OUTPUT ); 
  digitalWrite( 4, LOW ); 
  digitalWrite( 5, LOW ); 
  digitalWrite( 6, LOW );
  digitalWrite( 7, LOW );
  Serial.println( "AT+CMGF=1" ); 
 delay(2000);
 Serial.print( "AT+CMGDA=" ); 
Serial.write(34);
Serial.print("DEL ALL");
Serial.write(34);
Serial.write(13);
Serial.write(10);
delay(6000);
Serial.println( "Let's Test..." ); 
 pinMode(13, OUTPUT); // Interrupt monitor
//pinMode(12, INPUT); // Door status monitor
 pinMode(2, INPUT); //Door sensor monitor
 digitalWrite (2, LOW);  // internal pull-up resistor
  //When the Input goes from High to Low it will trigger a function called "doormonitor"
  doorlock = 0;
}
 
void loop()
{
  
 { if(digitalRead(2)==HIGH && doorlock==1)
        {digitalWrite(13,HIGH);
        delay (1000);
        doorlock=0;
        }
        else
        digitalWrite(13, LOW);
        
    
  char SerialInByte;
    if(Serial.available())
    {       
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
        
        if( SerialInByte == 13 ){
          // EN: Store the char into the message buffer
          ProcessGprsMsg();
         }
         if( SerialInByte == 10 ){
            // EN: Skip Line feed
         }
         else {
           // EN: store the current character in the message string buffer
           msg += String(SerialInByte);
         }
     }   
}

void ProcessSms( String sms ){
  
  if( sms.indexOf("ona") >= 0 ){
    digitalWrite( relay_a, HIGH );
  }
   if( sms.indexOf("onb") >= 0 ){
    digitalWrite(  relay_b, HIGH );
    doorlock=1;  
  }
   if( sms.indexOf("onc") >= 0 ){
    digitalWrite(  relay_c, HIGH );
  }
  if( sms.indexOf("ond") >= 0 ){
    digitalWrite(  relay_d, HIGH );
  }
  if( sms.indexOf("offa") >= 0 ){
    digitalWrite(  relay_a, LOW );
  }
  if( sms.indexOf("offb") >= 0 ){
    digitalWrite(  relay_b, LOW );
  }
  if( sms.indexOf("offc") >= 0 ){
    digitalWrite(  relay_c, LOW );
  }
  if( sms.indexOf("offd") >= 0 ){
    digitalWrite(  relay_d, LOW );
  }
}
// EN: Request Text Mode for SMS messaging
void GprsTextModeSMS(){
  Serial.println( "AT+CMGF=1" );
}

void GprsReadSmsStore( String SmsStorePos ){
  Serial.print( "AT+CMGR=" );
  Serial.println( SmsStorePos );
}

// EN: Clear the GPRS shield message buffer
void ClearGprsMsg(){
  msg = "";
}

// EN: interpret the GPRS shield message and act appropiately
void ProcessGprsMsg() {
  if( msg.indexOf( "Call Ready" ) >= 0 ){
   //  Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
     GprsTextModeSMS();
  }
  
  // EN: unsolicited message received when getting a SMS message
  if( msg.indexOf( "+CMTI" ) >= 0 ){

     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
     GprsReadSmsStore( SmsStorePos );
  }
  
 if( msg.indexOf( "+CMGR:" ) >= 0 ){
    // EN: Next message will contains the BODY of SMS
    SmsContentFlag = 1;
    // EN: Following lines are essentiel to not clear the flag!
    ClearGprsMsg();
    return;
  }
  
  // EN: +CMGR message just before indicate that the following GRPS Shield message 
  //     (this message) will contains the SMS body 
  if( SmsContentFlag == 1 ){
    ProcessSms( msg );
  }
  
  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0; 
}

Any suggestions?

Now you are starting to cook, no need to use 1 pin to check on another.

 pinMode(2, INPUT); //Door sensor monitor
 digitalWrite (2, LOW);  // internal pull-up resistor
  //When the Input goes from High to Low it will trigger a function called "doormonitor"
  doorlock = 0;

If you set the { } out clearly they will be easier to see and balance closes to opens

  if( sms.indexOf("onb") >= 0 )
  {
    digitalWrite(  relay_b, HIGH );
    doorlock=1;  
  }

That delay(1000) stops your machine from keeping track of the world or doing anything about it. Spend time with the BlinkWithoutDelay example that comes with your IDE and you will learn how to not lose that time which you may want when you add features later.

if(digitalRead(2)==HIGH && doorlock==1)
{
  digitalWrite(13,HIGH);
  delay (1000);
  doorlock=0;
}
else  digitalWrite(13, LOW);

OUTPUT
After sending the "onb" command pin 13 glows only once & then goes off. Nothing happen if I short pin 2 with GND or VCC.

Expected Output
After the doorlock relay is high, the pin 2 will sense state change ONLY ONCE until the relay is activated again.

Pin 2 is the button. Your code should track what the button is doing as a separate thing from what the code will do about the state of the button. For one thing, that will allow you to debounce the button, ie make sure about the intention of the button pusher.

In your code you use C++ String object(s). They are fitting to computers with lots of RAM.
I suggest you learn and use C char array strings and string.h functions and then PROGMEM.

Thanks for all the suggestions GFS.

Just now, got an idea...

   while (doorlock==1)
     {
      if (digitalRead(2)==HIGH));
        {
          digitalWrite(13,HIGH);
              delay (1000);
                digitalWrite(13, LOW);
                  doorlock=0;
                    }
        else {}
     }

According to the main program when I send "onb", doorlock flag will be 1.
in the while loop if the button pin sees high, arduino will lit an led / send sms
after execution of the led or sms code the flag will set to 0. so Until the door lock relay is on again, the led/ sms sending part will not happen.

I am probably doing wrong in the syntax may be... any suggestions.? the void loop() function is...

void loop()
{
  
   while (doorlock==1)
     {
      if (digitalRead(2)==HIGH));
        {
          digitalWrite(13,HIGH);
              delay (1000);
                digitalWrite(13, LOW);
                  doorlock=0;
                    }
        else {}
     }
         
  char SerialInByte;
    if(Serial.available())
    {       
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
        
        if( SerialInByte == 13 ){
          // EN: Store the char into the message buffer
          ProcessGprsMsg();
         }
         if( SerialInByte == 10 ){
            // EN: Skip Line feed
         }
         else {
           // EN: store the current character in the message string buffer
           msg += String(SerialInByte);
         }
     }   
}

Ide shows the following errors

Res_3.ino: In function 'void loop()':
Res_3:54: error: expected primary-expression before ')' token
Res_3:54: error: expected `;' before ')' token
Res_3:61: error: 'else' without a previous 'if'

the compiler point to this line for error.

      if (digitalRead(2)==HIGH));

Kindly mention the error in this syntax.

Thank you.

Figured out my fault... I was doing wrong in the "if" statement... extra ")" made the issue. Thanks to google.

modified the code once again... this time there are 2 issues.

  1. The code works for only once. After sending the "onb" sms relay got acivated BUT when I send "offb" the system does not respond. relay is always activated, I suppose the void loop got stuck in the while(doorlock==1) loop.

  2. The internal PULL-Down resistor of pin2 is not working. I have made the setup in breadboard. whenever the pin2 sense non zero, it lits up the led 13. in this case floating value=high, I mean if I connect pin2 with nothing (no VCC, no GND) in the breadboard it sees the floating value as HIGH & triggers the LED.

will you kindly help me figure out the fault in the code?

#include <SoftwareSerial.h>
 

// EN: String buffer for the GPRS shield message
String msg = String("");
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
int SmsContentFlag = 0;
//control pins of relay.
int relay_a=4;
int relay_b=5;
int relay_c=6;
int relay_d=7;
int led = 13; // Interrupt monitor
//int door = 12; // Door Status monitor
int button=2; // Door Sensor monitor
volatile int doorlock;
 // Interrupt Service Routine (ISR)

void setup()
{
  Serial.begin(19200);                 // the GPRS baud rate
  // Initialize  PINs
  pinMode( 4, OUTPUT ); 
  pinMode( 5, OUTPUT ); 
  pinMode( 6, OUTPUT ); 
  pinMode( 7, OUTPUT ); 
  digitalWrite( 4, LOW ); 
  digitalWrite( 5, LOW ); 
  digitalWrite( 6, LOW );
  digitalWrite( 7, LOW );
  Serial.println( "AT+CMGF=1" ); 
 delay(2000);
 Serial.print( "AT+CMGDA=" ); 
Serial.write(34);
Serial.print("DEL ALL");
Serial.write(34);
Serial.write(13);
Serial.write(10);
delay(6000);
Serial.println( "Let's Test..." ); 
 pinMode(13, OUTPUT); // Interrupt monitor
//pinMode(12, INPUT); // Door status monitor
 pinMode(2, INPUT); //Door sensor monitor
 digitalWrite (2, LOW);  // internal pull-up resistor
  doorlock = 0;
}
 
void loop()
{
  while (doorlock==1)
     {
      if (digitalRead(2)==HIGH)
        {
          digitalWrite(13,HIGH);      //  This part will be used for sending SMS
              delay (1000);               //
                digitalWrite(13, LOW); //
                  doorlock=0;
                    }
        else {
                break;
                    }
  }
  char SerialInByte;
    if(Serial.available())
    {       
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
        
        if( SerialInByte == 13 ){
          // EN: Store the char into the message buffer
          ProcessGprsMsg();
         }
         if( SerialInByte == 10 ){
            // EN: Skip Line feed
         }
         else {
           // EN: store the current character in the message string buffer
           msg += String(SerialInByte);
         }
     }   
}


void ProcessSms( String sms ){
  
  if( sms.indexOf("ona") >= 0 ){
    digitalWrite( relay_a, HIGH );
  }
   if( sms.indexOf("onb") >= 0 ){
    digitalWrite(  relay_b, HIGH );
    doorlock=1;  
  }
   if( sms.indexOf("onc") >= 0 ){
    digitalWrite(  relay_c, HIGH );
  }
  if( sms.indexOf("ond") >= 0 ){
    digitalWrite(  relay_d, HIGH );
  }
  if( sms.indexOf("offa") >= 0 ){
    digitalWrite(  relay_a, LOW );
  }
  if( sms.indexOf("offb") >= 0 ){
    digitalWrite(  relay_b, LOW );
    doorlock=0;
  }
  if( sms.indexOf("offc") >= 0 ){
    digitalWrite(  relay_c, LOW );
  }
  if( sms.indexOf("offd") >= 0 ){
    digitalWrite(  relay_d, LOW );
  }
}
// EN: Request Text Mode for SMS messaging
void GprsTextModeSMS(){
  Serial.println( "AT+CMGF=1" );
}

void GprsReadSmsStore( String SmsStorePos ){
  Serial.print( "AT+CMGR=" );
  Serial.println( SmsStorePos );
}

// EN: Clear the GPRS shield message buffer
void ClearGprsMsg(){
  msg = "";
}

// EN: interpret the GPRS shield message and act appropiately
void ProcessGprsMsg() {
  if( msg.indexOf( "Call Ready" ) >= 0 ){
   //  Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
     GprsTextModeSMS();
  }
  
  // EN: unsolicited message received when getting a SMS message
  if( msg.indexOf( "+CMTI" ) >= 0 ){

     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
     GprsReadSmsStore( SmsStorePos );
  }
  
 if( msg.indexOf( "+CMGR:" ) >= 0 ){
    // EN: Next message will contains the BODY of SMS
    SmsContentFlag = 1;
    // EN: Following lines are essentiel to not clear the flag!
    ClearGprsMsg();
    return;
  }
  
  // EN: +CMGR message just before indicate that the following GRPS Shield message 
  //     (this message) will contains the SMS body 
  if( SmsContentFlag == 1 ){
    ProcessSms( msg );
  }
  
  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0; 
}

This code blocks and has a delay(). Arduino does nothing else but wait for the button to be pressed while the door is locked. You can wire a logic gate to the lock system to do that, real cheap.

   while (doorlock==1)
     {
      if (digitalRead(2)==HIGH));
        {
          digitalWrite(13,HIGH);
              delay (1000);
                digitalWrite(13, LOW);
                  doorlock=0;
                    }
        else {}
     }

You are writing top-down code which is what they first teach on PC's that have Operating Systems.

Microcontrollers are geared towards real-time asynchronous event code, ie system code.

Each execution of loop() should take a little bit of time to do a small task so that all tasks, pin and timer checks are run responsively and then all you need to do is break your job down to small tasks and control which task runs by either time, sensor input, or state of a variable as in step 0, 1, 2, 3, etc.

But no task holds things up. If condition A is met but condition B is not, return to run loop() again.

Put the most critical tasks first and IMO Serial.available() last. Then you don't have to logic one part to fit it into another. You run part of the task in one execution of loop() and set a variable to run the next part on some trigger or next pass through loop() that gets to that sub-task. You don't care if some other higher priority sub-task gets run in between -- and you don't have to code whole layers of indented logic for that. Triggers and state variables replace if's and loop() replaces while's. It will simplify your code as long as you are careful to pre-define your tasks and break them down to run quickly. It's amazing what can be done in the time that top-down code spends doing nothing but waiting.

Doing something on time:
if ( millis() - start >= interval ) { time to do whatever };

Doing the next sub-task:
switch (state)
{
case : 0
{
// sub-task 0, usually waiting to start
}
break;
case : 1
{
// etc
}
break;
default :
{
// a good place to catch any errors
}
}

And good old serial input:
if (Serial.available())
{
// c strings don't copy themselves just to add a character.
// that doesn't just take RAM, it takes CYCLES.
// C++ String objects are wasteful and bloated.
// a c string is just an array of char variables that code text using ASCII values ending with a zero.
// learn the string.h functions and you can have faster text code that uses less RAM.
// learn PROGMEM and keep your constants in flash RAM and you will save more RAM.
}

Hahha... :slight_smile: I like your sense of humour GFS. nice Algo.
Thanks for all your support. Your explanation key of IF & WHILE loop is very useful to me. A little bit of googling & finally the code is working...
Till now the code is working just like (more or less) I wanted it to be. :slight_smile:

Well, I have replaced the led & delay part with the SMS sending code as I mentioned before. But there's still an issue. while communicating with the sim300 module there has to be some delay otherwise the GSM modem is sending weird characters in the sms. How to make it possible to avoid the necessary delay for the communication between arduino & GSM shield??? I have seen the Blinkwithoutdelay example & came across millis(). I hope it will help me develop the system.

The modified code is here & it works, The whole process can be done in a more simple way & I have to find out... "HOW"

#include <SoftwareSerial.h>

String msg = String("");// EN: String buffer for the GPRS shield message
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
int SmsContentFlag = 0;
//control pins of relay.
int relay_a=4;
int relay_b=5;
int relay_c=6;
int relay_d=7;
int button=2; // Door Sensor monitor
volatile int doorlock;

void setup()
{
Serial.begin(19200);                 // the GPRS baud rate
  // Initialize  PINs
pinMode( 4, OUTPUT ); 
pinMode( 5, OUTPUT ); 
pinMode( 6, OUTPUT ); 
pinMode( 7, OUTPUT ); 
digitalWrite( 4, LOW ); 
digitalWrite( 5, LOW ); 
digitalWrite( 6, LOW );
digitalWrite( 7, LOW );
Serial.println( "AT+CMGF=1" ); 
delay(2000);
Serial.print( "AT+CMGDA=" ); 
Serial.write(34);
Serial.print("DEL ALL");
Serial.write(34);
Serial.write(13);
Serial.write(10);
delay(6000);
Serial.println( "Let's Test..." ); 
pinMode(13, OUTPUT); // Interrupt monitor
//pinMode(12, INPUT); // Door status monitor
pinMode(2, INPUT); //Door sensor monitor
digitalWrite (2, LOW);  // internal pull-up resistor
doorlock = 0;
}
 
void loop()
{
  while (doorlock==1)
     {
      if (digitalRead(2)==HIGH)
        {
         Serial.write("AT+CMGS=");
         Serial.write(34); //ASCII of “
         Serial.write("+1234567890");
         Serial.write(34);
         Serial.write(13);
         Serial.write(10);
         delay(1000);
         Serial.println("Warning! Door has been opened"); //this is the message to be sent
         delay(500);
         Serial.write(26); //
         Serial.write(13);//
         Serial.write(10);//Ascii code of ctrl+z to send the message
         doorlock=0;
        }
        else if (digitalRead(2)==LOW)
                {
                  break;
                }
      }
  char SerialInByte;
    if(Serial.available())
    {       
     SerialInByte = (unsigned char)Serial.read();
     delay(5);
     if( SerialInByte == 13 )
         {
           ProcessGprsMsg(); // EN: Store the char into the message buffer
         }
            if( SerialInByte == 10 )
            {
               // EN: Skip Line feed
            }
            else 
            {
              msg += String(SerialInByte); // EN: store the current character in the message string buffer
            }
     }   
}

void ProcessSms( String sms )
{
  if( sms.indexOf("ona") >= 0 )
  {
    digitalWrite( relay_a, HIGH );
  }
   if( sms.indexOf("onb") >= 0 )
  {
     digitalWrite(  relay_b, HIGH );
     doorlock=1;  
  }
   if( sms.indexOf("onc") >= 0 )
  {
    digitalWrite(  relay_c, HIGH );
   }
  if( sms.indexOf("ond") >= 0 )
  {
    digitalWrite(  relay_d, HIGH );
  }
  if( sms.indexOf("offa") >= 0 )
  {
    digitalWrite(  relay_a, LOW );
  }
  if( sms.indexOf("offb") >= 0 )
  {
    digitalWrite(  relay_b, LOW );
     doorlock=0;
  }
  if( sms.indexOf("offc") >= 0 ) 
  {
    digitalWrite(  relay_c, LOW );
  }
  if( sms.indexOf("offd") >= 0 )
  {
    digitalWrite(  relay_d, LOW );
  }
}

void GprsTextModeSMS()
{ 
  Serial.println( "AT+CMGF=1" ); // EN: Request Text Mode for SMS messaging
}

    void GprsReadSmsStore( String SmsStorePos )
     {
        Serial.print( "AT+CMGR=" );
        Serial.println( SmsStorePos );
     }
       
void ClearGprsMsg() // EN: Clear the GPRS shield message buffer
    {
      msg = "";
    }


void ProcessGprsMsg() // EN: interpret the GPRS shield message and act appropiately
    {
      if( msg.indexOf( "Call Ready" ) >= 0 ){
      //  Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
       GprsTextModeSMS();
    }
  
 if( msg.indexOf( "+CMTI" ) >= 0 )   // EN: unsolicited message received when getting a SMS message
     {
          int iPos = msg.indexOf( "," );
          String SmsStorePos = msg.substring( iPos+1 );
          GprsReadSmsStore( SmsStorePos );
     }
  
 if( msg.indexOf( "+CMGR:" ) >= 0 )
    {
       SmsContentFlag = 1;     // EN: Next message will contains the BODY of SMS
       ClearGprsMsg();     // EN: Following lines are essentiel to not clear the flag!
       return;
    }
  
  
 if( SmsContentFlag == 1 )  // EN: +CMGR message just before indicate that the following GRPS Shield message
     {
       ProcessSms( msg );   //     (this message) will contains the SMS body 
     }
  
 ClearGprsMsg();
  SmsContentFlag = 0;    // EN: Always clear the flag
}

Really its a great experience learning arduino. Actually I am not a computer guy & know very very litttle about coding. I have recently get into the field of C & embedded systems. And yes, I love Tinkering.
:smiley:

void setup()
{
  Serial.begin(19200);                 // the GPRS baud rate
  // Initialize  PINs
    pinMode( 4, OUTPUT ); 
     pinMode( 5, OUTPUT ); 
      pinMode( 6, OUTPUT ); 
       pinMode( 7, OUTPUT ); 
        digitalWrite( 4, LOW ); 
         digitalWrite( 5, LOW ); 
          digitalWrite( 6, LOW );
           digitalWrite( 7, LOW );
            Serial.println( "AT+CMGF=1" ); 
             delay(2000);

Unusual indentation style. :stuck_out_tongue:

corrected the pattern...
:slight_smile:

the compiler point to this line for error.
Code:

if (digitalRead(2)==HIGH));

Kindly mention the error in this syntax.

You have the extra ')' and ALSO that semicolon should probably not be there.

nightcrawler218:
corrected the pattern...
:slight_smile:

In the IDE Tools tab there is Auto Format that arranges indents or tells why it could not.

Good idea
otherwise your code
becomes unnecessarily
hard to read.

I want to add a small feature that, If I want I can RESET the system by sending SMS. Found 2 different codes in this forum for RESET.

void (softReset){
asm volatile ("  jmp 0");
}

and

void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile ("  jmp 0");  
}

While using the first code it shows an error but the second one works perfectly fine, why is it so???

My Full code now looks like this...

#include <SoftwareSerial.h>
 

// EN: String buffer for the GPRS shield message
String msg = String("");
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
int SmsContentFlag = 0;
//control pins of relay.
int relay_a=4;
int relay_b=5;
int relay_c=6;
int relay_d=7;
int button=2; // Door Sensor monitor
volatile int doorlock;

void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile ("  jmp 0");  
} 

void setup()
{
Serial.begin(19200);                 // the GPRS baud rate
  // Initialize  PINs
pinMode( 4, OUTPUT ); 
pinMode( 5, OUTPUT ); 
pinMode( 6, OUTPUT ); 
pinMode( 7, OUTPUT ); 
digitalWrite( 4, LOW ); 
digitalWrite( 5, LOW ); 
digitalWrite( 6, LOW );
digitalWrite( 7, LOW );
Serial.println( "AT+CMGF=1" ); 
delay(2000);
Serial.print( "AT+CMGDA=" ); 
Serial.write(34);
Serial.print("DEL ALL");
Serial.write(34);
Serial.write(13);
Serial.write(10);
delay(6000);
Serial.println( "Let's Test..." ); 
pinMode(2, INPUT); //Door sensor monitor
digitalWrite (2, HIGH);  // internal pull-up resistor
doorlock = 0;
}
 
void loop()
{
  while (doorlock==1)
     {
      if (digitalRead(2)==HIGH)
        {
         Serial.write("AT+CMGS=");
         Serial.write(34); //ASCII of “
         Serial.write("+1234567890");
         Serial.write(34);
         Serial.write(13);
         Serial.write(10);
         delay(1000);
         Serial.println("Yahoo, You are Successful...!"); //this is the message to be sent
         delay(500);
         Serial.write(26); //
         Serial.write(13);//
         Serial.write(10);//Ascii code of ctrl+z to send the message
         doorlock=0;
        }
        else if (digitalRead(2)==LOW)
                {
                  break;
                }
      }
  char SerialInByte;
    if(Serial.available())
    {       
     SerialInByte = (unsigned char)Serial.read();
     delay(5);
     if( SerialInByte == 13 )
         {
           ProcessGprsMsg(); // EN: Store the char into the message buffer
         }
            if( SerialInByte == 10 )
            {
               // EN: Skip Line feed
            }
            else 
            {
              msg += String(SerialInByte); // EN: store the current character in the message string buffer
            }
     }   
}

void ProcessSms( String sms )
{
  if( sms.indexOf("ona") >= 0 )
  {
    software_Reset();
  }
   if( sms.indexOf("onb") >= 0 )
  {
     digitalWrite(  relay_b, HIGH );
     doorlock=1;  
  }
   if( sms.indexOf("onc") >= 0 )
  {
    digitalWrite(  relay_c, HIGH );
   }
  if( sms.indexOf("ond") >= 0 )
  {
    digitalWrite(  relay_d, HIGH );
  }
  if( sms.indexOf("offa") >= 0 )
  {
    digitalWrite(  relay_a, LOW );
  }
  if( sms.indexOf("offb") >= 0 )
  {
    digitalWrite(  relay_b, LOW );
     doorlock=0;
  }
  if( sms.indexOf("offc") >= 0 ) 
  {
    digitalWrite(  relay_c, LOW );
  }
  if( sms.indexOf("offd") >= 0 )
  {
    digitalWrite(  relay_d, LOW );
  }
}

void GprsTextModeSMS()
{ 
  Serial.println( "AT+CMGF=1" ); // EN: Request Text Mode for SMS messaging
}

    void GprsReadSmsStore( String SmsStorePos )
     {
        Serial.print( "AT+CMGR=" );
        Serial.println( SmsStorePos );
     }
       
void ClearGprsMsg() // EN: Clear the GPRS shield message buffer
    {
      msg = "";
    }


void ProcessGprsMsg() // EN: interpret the GPRS shield message and act appropiately
    {
      if( msg.indexOf( "Call Ready" ) >= 0 ){
      //  Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
       GprsTextModeSMS();
    }
  
 if( msg.indexOf( "+CMTI" ) >= 0 )   // EN: unsolicited message received when getting a SMS message
     {
          int iPos = msg.indexOf( "," );
          String SmsStorePos = msg.substring( iPos+1 );
          GprsReadSmsStore( SmsStorePos );
     }
  
 if( msg.indexOf( "+CMGR:" ) >= 0 )
    {
       SmsContentFlag = 1;     // EN: Next message will contains the BODY of SMS
       ClearGprsMsg();     // EN: Following lines are essentiel to not clear the flag!
       return;
    }
  
  
 if( SmsContentFlag == 1 )  // EN: +CMGR message just before indicate that the following GRPS Shield message
     {
       ProcessSms( msg );   //     (this message) will contains the SMS body 
     }
  
 ClearGprsMsg();
  SmsContentFlag = 0;    // EN: Always clear the flag
}

Since the Arduino+GSM communication part is completed more or less, my next step is to communicate Arduino+GPS.
I have this GPS module. ( http://robokits.co.in/shop/index.php?main_page=product_info&cPath=69&products_id=289 )
It comes with an USB port, any clue how to use it with Arduino??? because it does not have Tx & Rx pin-outs.

While using the first code it shows an error but the second one works perfectly fine, why is it so???

The second one is a function. The first one isn't
Why the need to reset ?

Because the sim300 has only 25 SMS memory. When I send commands via sms to access the system, the sms memory is getting filled one by one. So I added the DELETE SMS commands in the void setup() & by sending "ona" via sms I run the code from beginning so that the sms memory can be cleared. I know it is a limitation of the code that when I send RESET instruction, the system will take time to re-communicate with the real world. Avoiding timings & delays is the most critical part. I am a noob, any suggestions to run the following code without wasting any time delay (Time needed for RESET/ Delete sms memory)..?

Serial.print( "AT+CMGDA=" ); 
Serial.write(34);
Serial.print("DEL ALL");
Serial.write(34);
Serial.write(13);
Serial.write(10);
delay(6000);
Serial.println( "Let's Test..." );

Will you kindly help me distinguish between the previous two differenr reset codes, how are they different from each other.?

UKHeliBob:

While using the first code it shows an error but the second one works perfectly fine, why is it so???

The second one is a function. The first one isn't
Why the need to reset ?

Are you FinlessBOB from freaktab.com ?

No, neither am I Finless from HeliFreak

String msg = String("");

Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software

Thanks Mr. nick for letting me aware of the issue.

I note that even with the fixed free, C++ Strings still copy themselves to change length.

C++ Strings are wasteful of both RAM and cycles. They were made for PC's and people who would rather save on typing and learning than to think.