Hello,
I want to use the same IRQ Arduino PIN to wake controller, and to trigger retrieval of the data from a sensor. The first interrupt wakes ATMEGA328. When awake, ATMEGA gets other interrupts to read data from a sensor. If the sensor does not send data for a long time, ATMEGA goes to sleep.
The code below works fine, but I am wondering, does it matter where I put the code line “enableInterrupt(wakePin, wakeUpNow, RISING)”? At setup or under sleepNow(), or in both? I have tested all of them, they all works fine, but I would want to understand, which one works best?
#include <avr/sleep.h>
#include "EnableInterrupt.h"
#define wakePin 9 // pin used for waking up
int led=13;
int count;
volatile int irq = 0;
void wakeUpNow() {
irq=1;
}
void setup() {
enableInterrupt(wakePin, wakeUpNow, RISING);
Serial.begin(9600);
}
void sleepNow() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
enableInterrupt(wakePin,wakeUpNow,RISING);
sleep_mode();
sleep_disable();
disableInterrupt(wakePin);
Serial.println("woke up!");
}
void loop() {
delay (1000);
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
if (irq)
{
irq=0;
count=0;
Serial.println( "We have an interrupt!");
//Read data from the sensor
}
NeedSleep();
}
void NeedSleep()
{
count++;
Serial.print(count);
if (count >= 6){
Serial.println("Timer: Entering Sleep mode");
delay(100);
sleepNow();
count = 0;
}
}