It might seem weird but i am trying to make a theft alarm system using arduino which can be turned on and off using an ir receiver. Unfortunately a problem has come as you could have expected. I want to continue the theft search of pir sensor without requiring to put an input through ir remote. The issue is that since i am using ir in a conditional statement i need to put an input at the end of every loop. So it stops the pir sensor. Is there any way i can let my pir sensor work and stop it at any time i want. I have put the code below. Do not point out syntaxt errors please i have already uploaded and runned it please just help with the logic
2ndpart.ino (2.7 KB)
Just posting your code so that people don't have to download it:
#include <IRremote.h>
int RECV_PIN = 11;
#define led 7
IRrecv irrecv(RECV_PIN);
decode_results results;
#include <SoftwareSerial.h>
SoftwareSerial gsm(2,3);
String outMessage = "intruder alert";
String destinationNumber = "+919835175453";
int ledPin = 13; // choose the pin for the LED
int inputPin = 4; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
//Set up a speaker on a PWM pin (digital 9, 10, or 11)
void setup() {
gsm.begin(4800);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
Serial.println("Enabling IRin");
irrecv.enableIRIn(); // Start the receiver
Serial.println("Enabled IRin");
pinMode(led,OUTPUT);
}
void loop(){
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
if (results.value == 0xFF30CF){
digitalWrite(led, HIGH);
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
gsm.print("AT+CMGF=1\r");
delay(1000);
gsm.println("AT + CMGS = \"" + destinationNumber +"\"");
delay(1000);
// gsm.println("AT + CMGS = \"" + destinationNumber1 +"\"");
// delay(1000);
gsm.print(outMessage);
delay(1000);
gsm.write((char)26); //ctrl+z
delay(1000);
gsm.println("AT+CLTS=1");
gsm.println("AT+CCLK?");
delay(150);
Serial.println("Motion detected!");
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
delay(300);
if (pirState == HIGH){
// we have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
if(results.value == 0xFF18E7){
digitalWrite(led, LOW);
Serial.println("off");
}
// irrecv.resume(); // Receive the next value
}
delay(100);
}