Hello, please I am trying to build a home security system for a project and I would like my Arduino Uno to transmit data to my Arduino Yun anytime the PIR sensor detects motion. The PIR sensor is attached to interrupt pin 2 but for some reason when the sensor detects motion and transmits the data it does not go back to its normal state , it keeps transmitting indefinitely until the reset button on the board is pressed. I have attached a sample of my Tx code below. Please any input will be well appreciated thank you.
// Include VirtualWire library
#include <VirtualWire.h>
int led_pin = 13;
int transmit_pin = 12;
volatile 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);
attachInterrupt(0, interruptRoutine, RISING);
}
void loop()
{
char msg[1] = {'0'};
/* while(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;
}
}*/
// 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;
}
}
}
void interruptRoutine()
{
val = 1;
}