#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..!
1 Answer 1
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).2018年02月03日 20:38:26 +00:00Commented Feb 3, 2018 at 20:38
function should execute once
... are you sure?sleep_disable(); // first thing after waking from sleep: disable sleep...
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