PIR Wireless Alarm & GSM Shield Code Merge Issue

Hello Everyone

I am currently working on a PIR alarm system that senses motion and relays that info with a 433MHZ transmitter to a receiver that has a buzzer and LED. I have managed to get that working but I recently bought a GSM shield and want to combine a code that I found online but I seem to have run into some trouble. I want to receive a SMS when motion is detected as well as send the information wirelessly to my RX circuit. I keep getting error messages and I have searched the forums and google for help but to no avail. If someone can point me to the right direction or possibly help me merge the codes that would be amazing. Thank you :slight_smile:

// Include VirtualWire library
#include <VirtualWire.h>

int led_pin = 13;
int transmit_pin = 12;
int pir_pin = 2;
int val = 0; 
int pir_state = LOW;

void setup()
{
   Serial.begin(9600);
   vw_set_tx_pin(transmit_pin);
   vw_setup(4000); // Transmission rate
   pinMode(led_pin, OUTPUT);
   pinMode(pir_pin,INPUT);
}
 
void loop()
{
  char msg[1] = {'0'};
  // Get sensor value
  val = digitalRead(pir_pin);
  // Change message if motion is detected
  if (val == 1)
  {
      msg[0] = '1';
      digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
      vw_send((uint8_t *)msg, 1);
      vw_wait_tx(); // Wait until the whole message is gone
      if (pir_state == LOW) 
      {
      Serial.println("Motion detected!");
      pir_state = HIGH;
      }
   }
 else
 {
   msg[0] = '0';
   digitalWrite(led_pin, LOW);
   vw_send((uint8_t *)msg, 1);
   vw_wait_tx(); // Wait until the whole message is gone
   if (pir_state == HIGH)
   {
      Serial.println("Motion ended!");
      pir_state = LOW;
   }
  }
}
// Include VirtualWire library
#include <VirtualWire.h>

int led_pin = 13;
int transmit_pin = 12;
int pir_pin = 2;
int val = 0; 
int pir_state = LOW;

void setup()
{
   Serial.begin(9600);
   vw_set_tx_pin(transmit_pin);
   vw_setup(4000); // Transmission rate
   pinMode(led_pin, OUTPUT);
   pinMode(pir_pin,INPUT);
}
 
void loop()
{
  char msg[1] = {'0'};
  // Get sensor value
  val = digitalRead(pir_pin);
  // Change message if motion is detected
  if (val == 1)
  {
      msg[0] = '1';
      digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
      vw_send((uint8_t *)msg, 1);
      vw_wait_tx(); // Wait until the whole message is gone
      if (pir_state == LOW) 
      {
      Serial.println("Motion detected!");
      pir_state = HIGH;
      }
   }
 else
 {
   msg[0] = '0';
   digitalWrite(led_pin, LOW);
   vw_send((uint8_t *)msg, 1);
   vw_wait_tx(); // Wait until the whole message is gone
   if (pir_state == HIGH)
   {
      Serial.println("Motion ended!");
      pir_state = LOW;
   }
  }
}

I keep getting error messages

But you are not going to share them?

Sorry I am going to post the error messages soon. I accidentally posted the same code twice. Here is the GSM code I'm trying to merge with the above code

/*-------------GSM---------------------*/
#include <GSM.h>
#include <VirtualWire.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 = 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)
int transmit_pin = 12;
/*----------------------------------*/
/*------------- 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");  
}