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..!
Gerben
11.3k3 gold badges22 silver badges34 bronze badges
asked Jan 4, 2018 at 6:55
7
  • function should execute once ... are you sure? Commented Jan 4, 2018 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 Commented Jan 4, 2018 at 7:29
  • this is not the first thing, is it? sleep_disable(); // first thing after waking from sleep: disable sleep... Commented Jan 4, 2018 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..! Commented Jan 4, 2018 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 Commented Jan 4, 2018 at 8:02

1 Answer 1

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().

answered Jan 4, 2018 at 11:59
1
  • 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). Commented Feb 3, 2018 at 20:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.