Hi
I have been playing with the arduino for quite a while now, but I am still an electronic beginner.
I am trying to do the following sketch:
- every x seconds the arduino wakes up and read the value of some sensors, then it goes back to sleep. During this time, it cannot be interrupted by the PIR
- in sleep mode however it can be interrupted from the sleep mode and send a message
- for the sleep routine I am using the LowPower library
The sensors part is working well. The interruption has some issue since everytime the PIR detects a movement, the code goes back in the setup() function... I canno get what can cause that. Any idea would be welcome.
Here is the code I am using:
volatile int interrupt = 0;
void setup(void) {
// Setup PINs so they can do what we need
pinMode(2, INPUT);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
/* Initialization */
Serial.begin(9600);
//vw_set_ptt_inverted(true);
vw_setup(2000);
vw_set_tx_pin(5);
Serial.println("Setup done");
delay(5000); //some time for the PIR sensor to stabalyze
Serial.println("Ready");
}
void loop()
{
wakeUp();
Serial.println("Going to sleep");
rest();
}
void pin2Interrupt()
{
const char *msg = "PIR";
/* This brings us back from sleep. */
detachInterrupt(0); //disable interrupts while we wake up
interrupt = 1;
if (interrupt) { // funny, isn't it?
digitalWrite(LED_PIN, HIGH);
vw_send((uint8_t *)msg,strlen(msg));
vw_wait_tx();
digitalWrite(LED_PIN, LOW);
}
}
void rest()
{
attachInterrupt(0,pin2Interrupt,FALLING);
interrupt = 0;
delay(75);
int i =0;
while (i++ < 3) {
// Sleep for 8 s with ADC module and BOD module off
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
// some time for the arduino to wake up
delay(75);
}
void wakeUp(void)
{
Serial.print("Waking up...");
if (interrupt) {
Serial.println("interrupted");
interrupt = 0;
} else { Serial.println("timer"); }
// read the sensors
...
}