Integration of project components

Guys , may i request help on combining my project code ?
The project is base on a home security system.

I want to add in a accelerometer in the transmitter board working together with the PIR sensor with OR then the signal will be receive at the receiver board causing the led and buzzer to activate.

(I need help to combine the accelerometer with the transmitter board , any trigger in the accelerometer/PIR sensor will cause the led and buzzer to activate at the receiver board.)

The first and 2nd code consist of transmitter board with PIR sensor and receiver code with LED and buzzer .

This is the Transmitter board which i want to add the accelerometer with:
(The Led help to check whether the PIR sensor is working)

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

int led_pin = 7;
int transmit_pin = 12;
int pir_pin = 10;
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("Hey Stanley 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("Bye Stanley Motion ended!");
     pir_state = LOW;
  }
 }
}

This is the receiver board which will receiver signal from the transmitter board when the accelerometer/PIR sensor detect anything:

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

// Pins definition
const int led_pin = 13;
const int receive_pin = 12;
int pinSpeaker = 10; 

void setup()
{
  Serial.begin(9600); // Debugging only
  // Initialise the IO and ISR
  vw_set_rx_pin(receive_pin);
  vw_setup(4000); // Transmission rate
  // Start the receiver PLL
  vw_rx_start();
  // Set LED pin and Buzzer
  pinMode(led_pin, OUTPUT);
  pinMode(pinSpeaker, OUTPUT);
}

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  // Check if a message was received
   if (vw_get_message(buf, &buflen)) 
   {
     if(buf[0]=='1') 
     {
     Serial.println("Motion detected!");
     digitalWrite(led_pin,1);
     playTone(300, 160);
     delay(150);
     }  
    if(buf[0]=='0')
    {
    Serial.println("Motion ended!");
    digitalWrite(led_pin,0);
    playTone(0, 0);
    delay(300); 
    }
  }
}

// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) 
{
   duration *= 1000;
   int period = (1.0 / freq) * 1000000;
   long elapsed_time = 0;
   while (elapsed_time < duration) 
   {
   digitalWrite(pinSpeaker,HIGH);
   delayMicroseconds(period / 2);
   digitalWrite(pinSpeaker, LOW);
   delayMicroseconds(period / 2);
   elapsed_time += (period);
  }
}

This is the Accelerometer which i need help to combine with the transmitter board , any trigger in the accelerometer/PIR sensor will cause the led and buzzer to activate at the receiver board:

#include <ADXL345.h>
#include <a_adxl345.h>



// adxl345demo.ino

// This demo make use of non standard libraries
// - i2c
// - delays

// Connect up adxl345 appropriately using i2c
// After uploading to the MCU, Run the Serial Monitor set to 57600 bps.
// It will show the x, y and z acceleration at 0.5s intervals.

#include "Wire.h"
#include "a_delays.h"
#include "a_i2c.h"
#include "a_adxl345.h"

#define ADXL345_ADDR 0x53
#define FULL_RES 1 // 1 for 0.004g per bit, 0 depends on range
#define RANGE    16 // 2, 4, 8, 16g

static DELAYMS_T adly;
boolean accelInitOK;
int buzzerPin =9;
// ----------------------------------------------------------
void taskAccel_init( void ) {
 uint8_t id;
 if ((id = adxl345Init(ADXL345_ADDR, FULL_RES, RANGE)) == 0) {
   Serial.println("Error initializing adxl345");
   accelInitOK = false;
   return;
 }
 else {
   adly.dlySet(500);
   Serial.print("ADXL345 device ID = ");
   Serial.println(id, HEX);
   accelInitOK = true;
   Serial.println("G in x y z directions:");
 }
}

void taskAccel ( void ) {
 char buf[10];
 String temp;
 float trigger;
 accel_result_t r;
 int count = 0;
 
 if (accelInitOK == false)
 {
   return;
 }
 if (adly.dlyExpired())
 {
   adly.dlySetSinceOld(500); // 0.5s interval
   if (adxl345Read( &r ) == true) 
   {
     Serial.println("");
     dtostrf(r.x, 5, 2, buf);
     temp = String(buf);
     trigger = temp.toFloat();
     if((trigger>0.1)||(trigger<0.005))
     {
       tone(buzzerPin, 100);
     }
     else
     {
       noTone(buzzerPin);
     }
     Serial.print(buf);
     Serial.print(' ');
     
     dtostrf(r.y, 5, 2, buf);
     temp = String(buf);
     trigger = temp.toFloat();
     if((trigger>0.1)||(trigger<0.005))
     {
       tone(buzzerPin, 31);
     }
     else
     {
       noTone(buzzerPin);
     }
     Serial.print(buf);
     Serial.print(' ');
     
     dtostrf(r.z, 5, 2, buf);
     temp = String(buf);
     trigger = temp.toFloat();
     if((trigger>1.5)||(trigger<0.01))
     {
       tone(buzzerPin, 31);
     }
     else
     {
       noTone(buzzerPin);
     }
     Serial.print(buf);
   }
 }
}

void setup( void ) {
 i2cInit();
 Serial.begin(57600);
 Serial.println("adxl345 demo program");
 taskAccel_init();
}

void loop( void ) {
 taskAccel();
}

You really should have read the How to use this forum - please read post at the top of the index page and How to use this forum before posting.

ie Your code and any error messages should always be placed between code tags. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

It's still not too late to edit your post and do this. You'll make potential helpers much happier. :slight_smile:

Read Mikes link:
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

I can't imagine what an accelerometer is going to contribute to your home security system. What, exactly, is the accelerometer measuring the rate of change of velocity of?