0
#include <avr/sleep.h>  

int wakePin = 2;                 // pin used for waking up  
int led=13;
int flag=0;

void wakeUpNow() 
{  
  // execute code here after wake-up before returning to the loop() function  
  // timers and code using timers (serial.print and more...) will not work here.  
  // we don't really need to execute any special functions here, since we  
  // just want the thing to wake up  
  int count=10;
  while(count!=0)
  {
    delay(1000);
    count--;
    Serial.println(count);  
    delay(1000);   
  }
}  

void setup() 
{  
  Serial.begin(9600);
  pinMode(wakePin, INPUT_PULLUP);  // wakePin is pin no. 2
  pinMode(led, OUTPUT);          //   led is pin no. 13
  attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function wakeUpNow when pin 2 gets LOW
}  

void sleepNow() 
{  
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here  
    sleep_enable();                        // enables the sleep bit in the mcucr register  
                      delay(500);  
                      Serial.println("Rajat");  
                      delay(500);
    attachInterrupt(0,wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function  
                      delay(500);  
                      Serial.println("Rajat");  
                      delay(500);
    sleep_mode();     // here the device is actually put to sleep...!!
      // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP    
                      delay(500);  
                      Serial.println("Rajat2");  
                      delay(500);
    detachInterrupt(0);      // disables interrupt 0 on pin 2 so the wakeUpNow code will not be executed during normal running time. 
    sleep_disable();         // first thing after waking from sleep: disable sleep...  
                      delay(500);  
                      Serial.println("Rajat3");  
                      delay(500);
       detachInterrupt(0);      // disables interrupt 0 on pin 2 so the wakeUpNow code will not be executed during normal running time. 

}  

void loop() 
{ 
      digitalWrite(led, HIGH);   
      delay(1000);  
      digitalWrite(led, LOW);
      delay(1000);  
    /* delay(500);  
      Serial.println("jam");  
      delay(500);  */
      if (flag==0)
      {
      sleepNow();     // sleep function called here
      flag=1;
      }
}  

After Last Rajat I give ground to INT0.

Output in serial monitor :

Rajat
Rajat
9
8
7
6
5
4
3
2
1
0
9
8
7
6
5
4
3
2
1
0
9
8
7
6
5
4
3
2
1
0
9
8
7
6
5
4
3
2
1
0
9
8
7
6
5
4
3
2
1
0
9
8
7
6
5
4
3
2
1
0
9
8
7
6
5
4
3
2
1
0
9
8
7
6
5
4
3
2
& so on..!
  • function should execute once ... are you sure? – jsotola Jan 4 '18 at 7:06
  • after 2 nd rajat is printed, I give ground continously to INT0. now seeing stepwise..! 1) wakeupnow function will be executed than 2) We detach the interrupt to stop it from continuously firing while the interrupt pin is low using detachinterrupt(0) so now after getting ground wakeupnow should not be exexuted as interrupt is detached from pin 2 even though pin 2 is getting ground – bandejiya Jan 4 '18 at 7:29
  • this is not the first thing, is it? sleep_disable(); // first thing after waking from sleep: disable sleep... – jsotola Jan 4 '18 at 7:33
  • Yeah sorry forgot to edit comments, It is written wrong over there..! after getting interrupt 1st thing is wakeupnow function starts to execute..! but what's wrong in my previous comment..! – bandejiya Jan 4 '18 at 7:47
  • but what's wrong in my previous comment ... everything ... you are making an incorrect assumption about INT0 .... read the documentation ... also look at my first comment ... it is a big red flag – jsotola Jan 4 '18 at 8:02
2

You're detaching in completely the wrong place. After sleep_mode() returns from sleep and before the interrupt is detached you have an entire second of time where your interrupt can still fire. And it will fire, since a LOW interrupt will keep firing while the input is LOW.

sleep_mode();     // here the device is actually put to sleep...!!
  // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP    
                  delay(500);  
                  Serial.println("Rajat2");  
                  delay(500);
detachInterrupt(0);   

Instead you should detach the interrupt within the interrupt routine itself:

void wakeUpNow() 
{  
  // execute code here after wake-up before returning to the loop() function  
  // timers and code using timers (serial.print and more...) will not work here.  
  // we don't really need to execute any special functions here, since we  
  // just want the thing to wake up  

    detachInterrupt(0);   

  int count=10;
  while(count!=0)
  {
    delay(1000);
    count--;
    Serial.println(count);  
    delay(1000);   
  }
}  

Also, all those delays and Serial prints in an interrupt are seriously problematic. An interrupt should never delay() and should never Serial.println().

  • I agree with Majenko. Since interrupts are off inside an ISR, delays won't work. Thus your code will hang indefinitely. Don't do delays inside an ISR. (Nor serial prints). – Nick Gammon Feb 3 '18 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.