#include <avr/sleep.h>
int btnPin = 2;
int count = 0;
void wakeUp(){
}
void sleepNow(){
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(0, wakeUp, LOW);
sleep_mode();
sleep_disable();
detachInterrupt(0);
}
void setup() {
Serial.begin(9600);
pinMode(btnPin, INPUT);
attachInterrupt(0, wakeUp, RISING);
}
void loop() {
Serial.print("Awake for ");
Serial.print(count);
Serial.println("sec");
count++;
delay(1000);
if (Serial.available()) {
int val = Serial.read();
if (val == 'S') {
Serial.println("Serial: Entering Sleep mode");
delay(100);
count = 0;
sleepNow();
}
}
if (count >= 10) {
Serial.println("Timer: Entering Sleep mode");
delay(100);
count = 0;
sleepNow();
}
}