Pages: [1]   Go Down
Author Topic: PIR interruption which makes arduino go back in setup() function  (Read 1071 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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
...
}
Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

1.
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?

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

-Arne

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14029
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

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.
Thanks!!
Logged

Pages: [1]   Go Up
Jump to: