I have gotten the arduino to go into sleep mode via pin 2 change interrupt. My problem is I can't get it to wake up from sleep mode with pin 3 change interrupt. It is such an easy task but my code is not working when trying to wake it up.
#include <avr/interrupt.h>
#include <avr/sleep.h>
//Pin set up
int ledPin =12;
int wakePin = 3;
int sleepPin =2;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(sleepPin, INPUT_PULLUP);
pinMode(wakePin, INPUT_PULLUP );
attachInterrupt(0, sleepNow, LOW);
attachInterrupt(1,wakeUpNow,LOW);
}
void sleepNow(){
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
digitalWrite(ledPin, LOW);
attachInterrupt(1,wakeUpNow,LOW);
sleep_mode();
sleep_disable();
detachInterrupt(1);
delay(1000);
}
void wakeUpNow(){
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite (ledPin, LOW);
delay(1000);
}
I feel that there is a problem when you use an interrupt to put the arduino to sleep. If I use a simple switch to put it in sleep mode it wakes up. when I use an interrupt to put it into sleep mode it wont wake up. what is going on here??
I got it to work by using a boolean and then placing an if statement when true to call sleepmode. Now I want to have the program reset when waken. I got it to wake up using an interrupt anyone know a method to reset the program after it wakes up?
Here is the working code for sleep on interrupt and wake on another interrupt.
#include <avr/interrupt.h>
#include <avr/sleep.h>
//Pin set up
int ledPin =12;
int wakePin = 3;
int sleepPin =2;
volatile boolean flag;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(sleepPin, INPUT_PULLUP);
pinMode(wakePin, INPUT_PULLUP );
attachInterrupt(0, sleepy, LOW);
attachInterrupt(1,EMPTY,LOW);
}
void EMPTY(){
detachInterrupt(1);
}
void sleepy(){
flag = true;
}
void sleepNow(){
digitalWrite(ledPin, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(1,EMPTY,LOW);
sleep_mode();
}
void loop() {
// put your main code here, to run repeatedly:
// unsigned long startMillis = millis();
//while (millis() - startMillis < 5000);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite (ledPin, LOW);
delay(1000);
digitalWrite(ledPin, HIGH);
delay(1000);
if (flag){
sleepNow();
}
flag = false;
}