Combining two programs

I think I am a little bit over my head. I have two programs I would like to run simultaneously.

“ONE” checks for a trigger on pin 2. Once triggered it will call or text my phone as noted in the program.

“TWO” checks for incoming SMS messages and activates relays accordingly.

I can get each of them to work individually. I even converted the first one to use hardware serial commands instead of software serial commands.

I then tried to merge the two into one program and only the program “ONE” works as it should. I get no response on program TWO.

Here they are: (sorry if they are a bit sloppy, I am waiting to clean up after I get it working as I like)
ONE: (call on trigger)

#include <SoftwareSerial.h>


const int buttonPin = 2; 
int buttonState = 0;
int buttonPrevState = 0;
const int ledPin =  13; 

void setup()
{
  powerUpOrDown();
  pinMode(buttonPin, INPUT);   // software powerUp so we dont have to press the button to turn on shield

  Serial.begin(19200);       // the GPRS baud rate 
  delay(500);
}
 
void loop()
{
   buttonState = digitalRead(buttonPin);
   if ((buttonState == HIGH) && (buttonPrevState == LOW))
   {        
    // SendTextMessage();            // send sms       |Temp blanked to only do calls.
    // digitalWrite(ledPin, LOW);   // turn LED on:    |
    // delay (5000);                                   |
     DialVoiceCall();  //Call me...
   } 
     else {digitalWrite(ledPin, HIGH);
   } 
   buttonPrevState = buttonState;  
   delay(100);
}

void SendTextMessage()
{
  Serial.println("AT+CMGF=1\r");                     //Because we want to send the SMS in text mode
  delay(100);
  Serial.println("AT + CMGS = \"+12404782215\""); //send sms message, be careful need to add a country code before the cellphone number
  delay(100);
  Serial.println("Gucci GET DOE");      //the content of the message
  delay(100);
  Serial.println((char)26);                     //the ASCII code of the ctrl+z is 26
  delay(100);
  Serial.println();
}

void DialVoiceCall()
{
  Serial.println("ATD + +12404782215;");//dial the number
  delay(100);
  Serial.println();
//  delay(1000);
//  Serial.println("ATH0;");
//  delay(100);
//  Serial.println();              Not working to hang up as desired.  To be removed.
////  Serial.println("ATH0;");
////  delay(100);
////  Serial.println();

}

void powerUpOrDown()
{
  pinMode(9, OUTPUT); 
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(9,HIGH);
  delay(2000);
  digitalWrite(9,LOW);
  delay(3000);
}

TWO: Relays based on text rec’d

#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;

// EN: Code PIN of the SIM card (if applied)
//String SIM_PIN_CODE = String( "XXXX" );
 
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(200);
}
 
void loop()
{
    char SerialInByte;
    if(Serial.available())
    {       
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
        
        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // -------------------------------------------------------------------
       // EN: If the message ends with <CR> then process the message
        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);
         }
     }   
}
// EN: Make action based on the content of the SMS. 
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
void ProcessSms( String sms ){
  
  if( sms.indexOf("Lock") >= 0 ){
    digitalWrite( relay_a, HIGH );
    delay(1000);
    digitalWrite( relay_a, LOW );
  }
   if( sms.indexOf("Unlock") >= 0 ){
    digitalWrite(  relay_b, HIGH );
    delay(1000);
    digitalWrite(  relay_b, LOW );
  }
   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 ){
   //  Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
   //  Serial.print( "SMS stored at " );
  //   Serial.println( SmsStorePos );     
     // EN: Ask to read the SMS store
     GprsReadSmsStore( SmsStorePos );
  }
  
  // EN: SMS store readed through UART (result of GprsReadSmsStore request)  
  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 ){
 //   Serial.println( "*** SMS MESSAGE CONTENT ***" );
 //   Serial.println( msg );
 //   Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
  }
  
  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0; 
}

COMBO (ONE part works, TWO part does NOT):

This last part is too large to use nice formatting, Here is a raw code version…

#include <SoftwareSerial.h>

 

const int buttonPin = 2;        // @
int buttonState = 0;            // @
int buttonPrevState = 0;        // @
const int ledPin =  13;         // @

// 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;


 
void setup()
{
  powerUpOrDown();             // @
  pinMode(buttonPin, INPUT);   // @  software powerUp so we dont have to press the button to turn on shield

  
  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(200);
 

}    //   End Void Setup                                                                                   
 
void loop()
{
    char SerialInByte;
    if(Serial.available())
    {       
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
        
        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // -------------------------------------------------------------------
       // EN: If the message ends with <CR> then process the message
        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);
         }
     }   //End "if Serial available"
     
   buttonState = digitalRead(buttonPin);                                              //
   if ((buttonState == HIGH) && (buttonPrevState == LOW))                                //
   {                                                                                     //  
    // SendTextMessage();            // send sms       |Temp blanked to only do calls.   //
    // digitalWrite(ledPin, LOW);   // turn LED on:    |                                 //
    // delay (5000);                                   |                                 //
     DialVoiceCall();  //Call me...                                                      //
   }                                                                                     //
     else {digitalWrite(ledPin, HIGH);                                                   //
   }                                                                                     //
   buttonPrevState = buttonState;                                                        //
   delay(100);                                                                           //
     
}
// EN: Make action based on the content of the SMS. 
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
void ProcessSms( String sms ){
  
  if( sms.indexOf("Lock") >= 0 ){
    digitalWrite( relay_a, HIGH );
    delay(1000);
    digitalWrite( relay_a, LOW );
  }
   if( sms.indexOf("Unlock") >= 0 ){
    digitalWrite(  relay_b, HIGH );
    delay(1000);
    digitalWrite(  relay_b, LOW );
  }
   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 ){
   //  Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
   //  Serial.print( "SMS stored at " );
  //   Serial.println( SmsStorePos );     
     // EN: Ask to read the SMS store
     GprsReadSmsStore( SmsStorePos );
  }
  
  // EN: SMS store readed through UART (result of GprsReadSmsStore request)  
  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 ){
 //   Serial.println( "*** SMS MESSAGE CONTENT ***" );
 //   Serial.println( msg );
 //   Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
  }
  
  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0; 
}

void DialVoiceCall()
{
  Serial.println("ATD + +12404782215;");//dial the number
  delay(100);
  Serial.println();
//  delay(1000);
//  Serial.println("ATH0;");
//  delay(100);
//  Serial.println();              Not working to hang up as desired.  To be removed.
////  Serial.println("ATH0;");
////  delay(100);
////  Serial.println();

}
void SendTextMessage()
{
  Serial.println("AT+CMGF=1\r");                     //Because we want to send the SMS in text mode
  delay(100);
  Serial.println("AT + CMGS = \"+12404782215\""); //send sms message, be careful need to add a country code before the cellphone number
  delay(100);
  Serial.println("Gucci GET DOE");      //the content of the message
  delay(100);
  Serial.println((char)26);                     //the ASCII code of the ctrl+z is 26
  delay(100);
  Serial.println();
}

void powerUpOrDown()
{
  pinMode(9, OUTPUT); 
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(9,HIGH);
  delay(2000);
  digitalWrite(9,LOW);
  delay(3000);
}

I'm curious. You managed to format your code properly in your third post but not in the first two?

A simple way to merge two programs is to treat the stuff in loop() in each of them as a function in the combined program. Something roughly like this (for the combined program). No doubt you can think of more meaningful names.

void loop() {
   stuffFromProgramA();
   stuffFromProgramB();
}

void stuffFromProgramA() {
   // the code from loop() in program A
}

void stuffFromProgramB() {
   // the code from loop() in program B
}

Then in the part before setup() you need all the stuff that is before setup() in both programs
And within setup() you need all the code that is within setup in both programs.

Of course there may be conflicting variable names and some duplication (for example Serial.begin()) that will need to be adjusted.

...R

  1. Sorry. I figured the "Copy for Forum" option in the Arduino IDE would be optimal. In the first 2 I did not exceed the 9500 character limit. The Combo far exceeded so I resorted to another method, Code. I fixed both the former ones.

  2. I think I see what you did there. I do have some programming experience (C++ and Js/Html) Intro college courses. Its been about a year since my last actual programming attempt (besides this).

So the void loop () {} is the general way to call all the functions (which need to be ongoing) in a program?

I have never really done a function in a function as layered as this. I think I'll need some time to play around with that option.

Question: If I do set it up as:

void loop() {
   stuffFromProgramA();
   stuffFromProgramB();
}

Then will both programs run simultaneously?

This would be a great benefit since I only need one to operate (check for input) not as often as the other, and the way I have it would put any delay I make on both necessarily.

One more thing... I did some trouble shooting and the original code I had does no longer work from what I can tell on my setup. I changed nothing physically, just resent the original code that DID work previously.

My research has led me to believe that I may have used up all of the Arduino or GPRS buffer or memory is full or something of that nature. Is this a possibility? I essentially copied the code from the inter-webs and altered it using what actually do know. Someone who knows more than me could hopefully say, "yes after 30 or so text messages, it will be useless, look into this function _____ to clear the messages and implement it somewhere in the code..."

I did this web page about merging code:-
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

However you said:-

I have two programs I would like to run simultaneously.

You can’t do that, you can only get them to run one after the other.

If you are sneaky you split the tasks so it does a little bit of each task and then swaps to the other.
This is known as a state machine, and you can code it yourself, I wrote this about the technique:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

Gslenk:
Then will both programs run simultaneously?

Strictly speaking the Arduino can only do things consecutively. But it can do things so fast that they appear to be simultaneous.

The extent to which the process I suggested will appear simultaneous for you depends on how the code from each program is written. If there is widespread use of the delay() function they won't appear simultaneous.

This demo several things at a time shows how the appearance of simultaneous operation can be achieved.

...R

Here is my latest iteration.

The problem remains that only the first portion of the code works. I define works in the following ways:

—Program one works: I close a physical switch between +5v and Pin 2 and then the Arduino/GPRS calls my phone

—Program two works: I text the following messages: Lock, Unlock, Start and then relays 4, 3, 2 respectively close the NO leads for one second.

When I send a text, the serial monitor indicates that it was received but the relays do not engage. The serial monitor reads: AT+CMGR=19 where 19 is incremented on every consecutive text received by the arduino/gprs.

I assume there is some sort of interference going on, is it obvious to anyone what is happening?

If I comment out the following section it works fine for the lock/unlock relay part:

 void setup(){
   setupLock();
   //setupAlarm();
  }
  
  void loop(){
    loopLock();
    //loopAlarm();
  }

If I un-comment and use the code as below, only the alarm portion is successful.

//COMBINATION of Lock/Unlock/Start and Alarm alert

  void setup(){
   setupLock();
   setupAlarm();
  }
  
  void loop(){
    loopLock();
    loopAlarm();
  }
  
  #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;

// EN: Code PIN of the SIM card (if applied)
//String SIM_PIN_CODE = String( "XXXX" );
 
void setupLock()
{
  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(200);
}
 
void loopLock()
{
    char SerialInByte;
    if(Serial.available())
    {       
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
        
        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // -------------------------------------------------------------------
       // EN: If the message ends with <CR> then process the message
        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);
         }
     }   
}
// EN: Make action based on the content of the SMS. 
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
void ProcessSms( String sms ){
  
  if( sms.indexOf("Lock") >= 0 ){
    digitalWrite( relay_a, HIGH );
    delay(1000);
    digitalWrite( relay_a, LOW );
  }
   if( sms.indexOf("Unlock") >= 0 ){
    digitalWrite(  relay_b, HIGH );
    delay(1000);
    digitalWrite(  relay_b, LOW );
  }
   if( sms.indexOf("Start") >= 0 ){
    digitalWrite(  relay_c, HIGH );
    delay(1000);
    digitalWrite(  relay_c, LOW );
  }
  if( sms.indexOf("ond") >= 0 ){
    digitalWrite(  relay_d, HIGH );
  }
  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 ){
   //  Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
   //  Serial.print( "SMS stored at " );
  //   Serial.println( SmsStorePos );     
     // EN: Ask to read the SMS store
     GprsReadSmsStore( SmsStorePos );
  }
  
  // EN: SMS store readed through UART (result of GprsReadSmsStore request)  
  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 ){
  //  Serial.println( "*** SMS MESSAGE CONTENT ***" );
  //  Serial.println( msg );
  //  Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
  }
  
  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0; 
}

/////////////////////////////////////////////////////////////////////

#include <SoftwareSerial.h>
#include <String.h>
 
SoftwareSerial mySerial(7, 8);
const int buttonPin = 2; 
int buttonState = 0;
int buttonPrevState = 0;
const int ledPin =  13; 

void setupAlarm()
{
  powerUpOrDown();
  pinMode(buttonPin, INPUT);   // software powerUp so we dont have to press the button to turn on shield
  Serial.begin(19200);       // the GPRS baud rate 
  delay(500);
}
 
void loopAlarm()
{
   buttonState = digitalRead(buttonPin);
   if ((buttonState == HIGH) && (buttonPrevState == LOW))
   {        
    // SendTextMessage();            // send sms       |Temp blanked to only do calls.
    // digitalWrite(ledPin, LOW);   // turn LED on:    |
    // delay (5000);                                   |
     DialVoiceCall();  //Call me...
   } 
     else {digitalWrite(ledPin, HIGH);
   } 
   buttonPrevState = buttonState;  
   delay(100);
}

void SendTextMessage()
{
  Serial.print("AT+CMGF=1\r");                     //Because we want to send the SMS in text mode
  delay(100);
  Serial.println("AT + CMGS = \"+12404782215\""); //send sms message, be careful need to add a country code before the cellphone number
  delay(100);
  Serial.println("Gucci GET DOE");      //the content of the message
  delay(100);
  Serial.println((char)26);                     //the ASCII code of the ctrl+z is 26
  delay(100);
  Serial.println();
}

void DialVoiceCall()
{
  Serial.println("ATD + +12404782215;");//dial the number
  delay(100);
  Serial.println();


}

void powerUpOrDown()
{
  pinMode(9, OUTPUT); 
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(9,HIGH);
  delay(2000);
  digitalWrite(9,LOW);
  delay(3000);
}
 
void ShowSerialData()
{
  while(Serial.available()!=0)
    Serial.write(Serial.read());
}

Made a sketch combining the button example on pin 11 instead of 2. Combined it with the Lock/unlock program and they work together nicely... they can even happen at the "same time". Still doesn't like being with the Alarm phone call program.

I have reorganized your sketch to match the instructions as I thought I had given them. Sorry if they were not clear enough.

You will see that I have all the definitions and variable from both sketches at the top of the program and they are followed by all the code for setup() from both programs. In fact all the setup code can go into a single setup() function.

The point about organizing like this is that it makes it easy to see if there are conflicts or duplications, or things that could be shared.

Moving the code around like that shouldn’t make any difference to the working of your code, but it may suggest improvements.

I notice the delay() function is used very frequently. They may be interfering with the simultaneous running of the two parts. You should probably replace the all with the technique from the Blink Without Delay example sketch. I wrote an extended demo of BWoD in several things at a time.

//COMBINATION of Lock/Unlock/Start and Alarm alert


// =======all the definitions and global variables together at the top

  #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;

// EN: Code PIN of the SIM card (if applied)
//String SIM_PIN_CODE = String( "XXXX" );

/////////////////////////////////////////////////////////////////////

#include <SoftwareSerial.h>
#include <String.h>
 
SoftwareSerial mySerial(7, 8);
const int buttonPin = 2; 
int buttonState = 0;
int buttonPrevState = 0;
const int ledPin =  13; 


//======  all the setup stuff together =======

  void setup(){
   setupLock();
   setupAlarm();
  }


void setupLock()
{
  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(200);
}

void setupAlarm()
{
  powerUpOrDown();
  pinMode(buttonPin, INPUT);   // software powerUp so we dont have to press the button to turn on shield
  Serial.begin(19200);       // the GPRS baud rate 
  delay(500);
}

//==================
  
  void loop(){
    loopLock();
    loopAlarm();
  }
  
 
void loopLock()
{
    char SerialInByte;
    if(Serial.available())
    {       
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
        
        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // -------------------------------------------------------------------
       // EN: If the message ends with <CR> then process the message
        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);
         }
     }   
}
// EN: Make action based on the content of the SMS. 
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
void ProcessSms( String sms ){
  
  if( sms.indexOf("Lock") >= 0 ){
    digitalWrite( relay_a, HIGH );
    delay(1000);
    digitalWrite( relay_a, LOW );
  }
   if( sms.indexOf("Unlock") >= 0 ){
    digitalWrite(  relay_b, HIGH );
    delay(1000);
    digitalWrite(  relay_b, LOW );
  }
   if( sms.indexOf("Start") >= 0 ){
    digitalWrite(  relay_c, HIGH );
    delay(1000);
    digitalWrite(  relay_c, LOW );
  }
  if( sms.indexOf("ond") >= 0 ){
    digitalWrite(  relay_d, HIGH );
  }
  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 ){
   //  Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
   //  Serial.print( "SMS stored at " );
  //   Serial.println( SmsStorePos );     
     // EN: Ask to read the SMS store
     GprsReadSmsStore( SmsStorePos );
  }
  
  // EN: SMS store readed through UART (result of GprsReadSmsStore request)  
  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 ){
  //  Serial.println( "*** SMS MESSAGE CONTENT ***" );
  //  Serial.println( msg );
  //  Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
  }
  
  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0; 
}


//===============
 
void loopAlarm()
{
   buttonState = digitalRead(buttonPin);
   if ((buttonState == HIGH) && (buttonPrevState == LOW))
   {        
    // SendTextMessage();            // send sms       |Temp blanked to only do calls.
    // digitalWrite(ledPin, LOW);   // turn LED on:    |
    // delay (5000);                                   |
     DialVoiceCall();  //Call me...
   } 
     else {digitalWrite(ledPin, HIGH);
   } 
   buttonPrevState = buttonState;  
   delay(100);
}

void SendTextMessage()
{
  Serial.print("AT+CMGF=1\r");                     //Because we want to send the SMS in text mode
  delay(100);
  Serial.println("AT + CMGS = \"+12404782215\""); //send sms message, be careful need to add a country code before the cellphone number
  delay(100);
  Serial.println("Gucci GET DOE");      //the content of the message
  delay(100);
  Serial.println((char)26);                     //the ASCII code of the ctrl+z is 26
  delay(100);
  Serial.println();
}

void DialVoiceCall()
{
  Serial.println("ATD + +12404782215;");//dial the number
  delay(100);
  Serial.println();


}

void powerUpOrDown()
{
  pinMode(9, OUTPUT); 
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(9,HIGH);
  delay(2000);
  digitalWrite(9,LOW);
  delay(3000);
}
 
void ShowSerialData()
{
  while(Serial.available()!=0)
    Serial.write(Serial.read());
}

…R

I got it!

Removing all of the delays and using scheduling allows both to run without the "delays" interfering with each other.

I would post the code, but it is so embarrassingly sloppy and disorganized. Thanks for all the pointers!