Ive been trying to figure out how to but my arduino to sleep and wake it up again with an interrupt and continue to run my program.
The code is attache but Ive tried to make it as easy as possible. The problem is that the second or third time it goes to sleep the pininterrupt funtion is triggered (w/o an interrupt actually happening) which detaches the interrupt pin right before the device goes to sleep.
The serial output looks like this:
Opening port
Port open
Interrupt pin state: 0 Awake time state: 0.00
Interrupt pin state: 0 Awake time state: 1.00
Interrupt pin state: 0 Awake time state: 2.00
Interrupt pin state: 0 Awake time state: 3.00
Interrupt pin state: 0 Awake time state: 4.00
Interrupt pin state: 0 Awake time state: 5.00
Starting sleep
Woke up at: 5.00 Here I triggered the interrupt pin
Interrupt pin state: 1 Awake time state: 0.00
Interrupt pin state: 0 Awake time state: 1.00
Interrupt pin state: 0 Awake time state: 2.00
Interrupt pin state: 0 Awake time state: 3.00
Interrupt pin state: 0 Awake time state: 4.00
Interrupt pin state: 0 Awake time state: 5.00
Starting sleep
Woke up at: 10.00 This code is not supposed to run. This means that the pinInterrupt callback function was triggered
After this the arduino is asleep w. no means to wake it up.
Here is the code:
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/io.h>
float awakeTimer = 0;
float wokeUpTime = 0;
boolean makeHigh = true;
int sensorReading = 0;
void setup(void)
{
pinMode(13, OUTPUT); // set pin 13 as an output so we can use LED to monitor
Serial.begin(9600);
}
void loop(void)
{
sl eep_disable();
awakeTimer = (millis() / 1000 - wokeUpTime);
serialPrintState();
if (awakeTimer >= 5) {
sleepNow();
}
else {
runProgram();
}
}
void pinInterrupt(void)
{
wokeUpTime = millis() / 1000;
Serial.print("Woke up at: ");
Serial.println(wokeUpTime);
detachInterrupt(0);
delay(100);
}
void sleepNow(void) {
Serial.println("Starting sleep");
attachInterrupt(0, pinInterrupt, RISING);
delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
}
void runProgram(void) {
delay(1000);
digitalWrite(13, makeHigh);
makeHigh = !makeHigh;
}
void serialPrintState() {
int dtwo = digitalRead(2);
Serial.print("Interrupt pin state: ");
Serial.print(dtwo);
Serial.print(" Awake time state: ");
Serial.println(awakeTimer);
}
Can anybody help me into figuring out why the pinInterrupt function is ran without an actual interrupt? Also please give me tips on all the stuff im doing wrong in my sketch =)
SleepInterrupt.ino (1.08 KB)