PIR-GSM Code problem

Hi, I am testing the use of a PIR sensor with a GSM SIMCOM SIM900 shield.
I plugged the shield onto an Arduino UNO and pluged the PIR directly on the GSM shield at 5V, GND and D2.

The simple idea is to check if there is motion detected through PIR. When motion is detected the LED turns on and Arduino sends SMS.
Then turns LED off and restart (through loop).

What I did is just merge the PIR code and the SendSMS code and the result is the following:

/*-------------GSM---------------------*/
#include <GSM.h>

#define PINNUMBER "0000"

// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;

// char array of the telephone number to send SMS
// change the number 1-212-555-1212 to a number
// you have access to
char remoteNumber[20]= "1-212-555-1212";  

// char array of the message
char txtMsg[200]="Love You *ARDUINO*";

/*-------------GSM END ---------------------*/

/*-------------PIR ---------------------*/
int led = 13;                // the pin that the LED is atteched to
int sensor = 2;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)

/*----------------------------------*/
/*------------- SETUP --------------*/
/*----------------------------------*/

void setup() {
  // initialize serial communications
  Serial.begin(9600);

  /*-------------GSM SETUP---------------------*/
  Serial.println("SMS Messages Sender");

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("GSM initialized");
  /*-------------PIR SETUP ---------------------*/
  
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
}

/*----------------------------------*/
/*------------- LOOP --------------*/
/*----------------------------------*/

void loop() {
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds

    if (state == LOW) {
      Serial.println("Motion detected!");
      state = HIGH;       // update variable state to HIGH
      delay(100);
      sendSMS();          /*------------- SEND THE SMS --------------*/
      delay(1000); 
    }
  }
  else {
    digitalWrite(led, LOW); // turn LED OFF
    delay(200);             // delay 200 milliseconds

    if (state == HIGH) {
      Serial.println("Motion stopped!");
      state = LOW;       // update variable state to LOW
    }
  }
}

/*-----------------------------------------*/
/*------------- SMS FUNCTION --------------*/
/*-----------------------------------------*/

void sendSMS(){

  Serial.print("Message to mobile number: ");
  Serial.println(remoteNumber);

  // sms text
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNumber);
  sms.print(txtMsg);
  sms.endSMS(); 
  Serial.println("\nCOMPLETE!\n");  
}

For some reason it takes too long to get connected and if/when it gets connected it sends 1 SMS turns the LED on and stacks there with LED ON! (I get the message "COMPLETE" from SMS and it is sent).

I don't know where is the problem...
I have extra power supply 12V 1,5A on Arduino for the Shield.
Thanks.

with a GSM SIMCOM SIM900 shield.

A link, please.

#include <GSM.h>

A link, please.

What I'm wondering is which pins the shield is using, and whether the PIR sensor is using the same pin.

GSM Shield used is hete: GSM Shield
The library is in same page you can see if you scroll down.

I think the info must be here : http://linksprite.com/wiki/index.php5?title=SIM900_GPRS/GSM_Shield

PIR uses D2 and I think isn't used by the shield as I have seen above in the link.

int state = LOW;             // by default, no motion detected

Then, why use a dumb name like state? There is either motion happenning, or there isn't. You don't need a type that can hold 65000+ values to hold true or false. You need a boolean.

   if (state == LOW) {
      Serial.println("Motion detected!");

There is nothing about the name of the variable OR the value being tested for that leads ME to believe the state equaling LOW means that movement was detected.

Since you have not read anything from a pin that I can see has a motion sensor connected to it, and any such reading was not stored in state, I find the conclusion that state equal to LOW means that motion was detected.

For some reason it takes too long to get connected

How long does it take? In what part of the code?

I can't see the purpose of all the delay()s in the code.

I don't see enough Serial.print() statements in the code to see how you have determined that the code has stuck, or where.

I mean the connection of GSM. It takes about 2-5 minutes to get connected (no problem in signal strength).

The code is in fact the merge of 2 examples, each one works OK but when merged there is the problem.
One code found in a website and one in IDE examples:

SendSMS code:

#include <GSM.h>

#define PINNUMBER "0000"

// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;

// char array of the telephone number to send SMS
// change the number 1-212-555-1212 to a number
// you have access to
char remoteNumber[20]= "6974414235";  

// char array of the message
char txtMsg[200]="Love You *ARDUINO*";

void setup()
{
  // initialize serial communications
  Serial.begin(9600);

  Serial.println("SMS Messages Sender");

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("GSM initialized");
  sendSMS();
}

void loop()
{
// nothing to see here
}

void sendSMS(){

  Serial.print("Message to mobile number: ");
  Serial.println(remoteNumber);

  // sms text
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNumber);
  sms.print(txtMsg);
  sms.endSMS(); 
  Serial.println("\nCOMPLETE!\n");  
}

PIR code from this website.

/*  
    Arduino with PIR motion sensor
    For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
    Modified by Rui Santos based on PIR sensor by Limor Fried
*/
 
int led = 13;                // the pin that the LED is atteched to
int sensor = 2;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  Serial.begin(9600);        // initialize serial
}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 
      
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  }
}

I changed PIN from D2 to D6 for the PIR sensor and works with no problem. Even the GSM works OK.

/*-------------GSM---------------------*/
#include <GSM.h>

#define PINNUMBER "0000" // My GSM Card's pin is 0000. Change it or leave blank if you haven't got a pin number

// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;

// char array of the telephone number to send SMS
// change the number 1-212-555-1212 to a number
// you have access to
char remoteNumber[20]= "1-212-555-1212";  

// char array of the message
char txtMsg[200]="Love You *ARDUINO*";

/*-------------GSM END ---------------------*/

/*-------------PIR ---------------------*/
int led = 13;                // the pin that the LED is atteched to
int sensor = 6;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)

/*----------------------------------*/
/*------------- SETUP --------------*/
/*----------------------------------*/

void setup() {
  // initialize serial communications
  Serial.begin(9600);

  /*-------------GSM SETUP---------------------*/
  Serial.println("SMS Messages Sender");

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(500);
    }
  }
  Serial.println("GSM initialized");
  /*-------------PIR SETUP ---------------------*/
  
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
}

/*----------------------------------*/
/*------------- LOOP --------------*/
/*----------------------------------*/

void loop() {
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(20);                // delay 100 milliseconds

    if (state == LOW) {
      Serial.println("Motion detected!");
      state = HIGH;       // update variable state to HIGH
      delay(20);
      sendSMS();          /*------------- SEND THE SMS --------------*/
      Serial.println("Send SMS");
    }
  }
  else {
    digitalWrite(led, LOW); // turn LED OFF
    delay(20);             // delay 200 milliseconds

    if (state == HIGH) {
      Serial.println("Motion stopped!");
      state = LOW;       // update variable state to LOW
    }
  }
}

/*-----------------------------------------*/
/*------------- SMS FUNCTION --------------*/
/*-----------------------------------------*/

void sendSMS(){

  Serial.print("Message to mobile number: ");
  Serial.println(remoteNumber);

  // sms text
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNumber);
  sms.print(txtMsg);
  sms.endSMS(); 
  Serial.println("\nCOMPLETE!\n");  
}