Go Down

Topic: PIR interruption which makes arduino go back in setup() function (Read 2529 times) previous topic - next topic


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:
Code: [Select]

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.println("Setup done");
  delay(5000); //some time for the PIR sensor to stabalyze
void loop()
  Serial.println("Going to sleep");
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));
    digitalWrite(LED_PIN, LOW);

void rest()
  interrupt = 0;
  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

void wakeUp(void)
  Serial.print("Waking up...");
  if (interrupt) {
    interrupt = 0;
  } else { Serial.println("timer"); }
// read the sensors


u r inside an interrupt routine and use these vw_...() routines...
can u just set that interrupt flag and do the real work in the loop() function?

what if u just toggle the LED_PIN in that interrupt routine?
will it reset then 2?


definitely follow strategy 1 of Riddick => keep ISR minimal in size and do the work in the main loop() ,,
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)



Ok, it works, thanks a lot.
But I opened another topic for my next issue: putting arduino to sleep and wake up on external interrupt OR after 5mn ... both are woking independently or if sleep time is <= 8s ... but not if this is a longer sleep... more details in the topic.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131