I want to make a portable pir (2x) sensor connected to arduino pro mini and than transmit some data ( via 433mhz transmitter ) to another arduino. To save power I would like to make arduino sleep and only be waken up by pir. The problem is I can't make the code to work with two pirs on two interrupts. I was able to make it work with one pir. So my question is if it is doable (2 pirs on 2 interrupts) or if not can I combine data-output from both pirs to one interrupt?
My not working code:
#include <VirtualWire.h>
#include <avr/sleep.h>
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 10;
int pirPin1 = 2; //the digital pin connected to the PIR sensor's output
int pirPin2 = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 13;
int transmit_pin = 4;
int t = 1;
int pow1 = 7; //pir 1 power / just temporary
int pow2 = 8; //pir 2 power / just temporary
int pow3 = 9; //trans power / just temporary
boolean unique = false;
boolean ready_to_sleep = false;
char msg[6] = {'s','1','A','C','T','V'};
int fired_by_sensor = 0;
void setup() {
pinMode(pow1, OUTPUT);
pinMode(pow2, OUTPUT);
pinMode(pow3, OUTPUT);
digitalWrite(pow1, HIGH);
digitalWrite(pow2, HIGH);
digitalWrite(pow3, HIGH);
pinMode(ledPin, OUTPUT);
pinMode(pirPin1, INPUT);
pinMode(pirPin2, INPUT);
digitalWrite(pirPin1, LOW);
digitalWrite(pirPin2, LOW);
//pinMode(transmit_pin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
// Initialise the IO and ISR
vw_set_tx_pin(transmit_pin);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
//give the sensor some time to calibrate
Serial.print("Calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" Calibration Done!");
vw_send((uint8_t *)msg, 6);
vw_wait_tx(); // Wait until the whole message is gone
delay(50);
sleepNow();
}
void loop() {
if(unique == true){
char msg[6] = {'x','s','0','t','m','#'};
msg[5] = fired_by_sensor;
msg[5] = t;
vw_send((uint8_t *)msg, 6);
vw_wait_tx(); // Wait until the whole message is gone
delay(50);
unique = false;
}
Serial.println(ready_to_sleep);
Serial.println(digitalRead(pirPin1));
if ( (ready_to_sleep == true) && (digitalRead(pirPin1) == LOW) ){
Serial.println("Sleep now fired from loop.");
sleepNow();
}
delay (1000);
Serial.println("Loop.");
}
void pir1() {
Serial.begin(9600);
Serial.println(" ");
Serial.print("Motion detected: Sensor 1x");
Serial.println(t);
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
t++;
unique = true;
fired_by_sensor = 1;
ready_to_sleep = true;
}
void pir2() {
Serial.begin(9600);
Serial.println(" ");
Serial.print("Motion detected: Sensor 2x");
Serial.println(t);
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
t++;
unique = true;
fired_by_sensor = 2;
ready_to_sleep = true;
}
void sleepNow() // here we put the arduino to sleep
{
Serial.println("Going to sleep!");
for(int i = 0; i < 3; i++){
Serial.print(".");
delay(1000);
}
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin
attachInterrupt(digitalPinToInterrupt(pirPin1), pir1, RISING);
attachInterrupt(digitalPinToInterrupt(pirPin2), pir2, RISING);
sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); // first thing after waking from sleep:
// disable sleep...
detachInterrupt(digitalPinToInterrupt(pirPin1));
detachInterrupt(digitalPinToInterrupt(pirPin2)); // disables interrupt 0 on pin 2 so the
// wakeUpNow code will not be executed
// during normal running time.
}
Components used:
Arduino pro mini
2x pir
433mhz transmitter + receiver
power source is cheap power bank...
And I am new to C/C++ programing...