Ok
I am using an Atmel 1284P with the Mighty Optiboot bootloader if that information is needed.
So I can get each of these things working individually but the moment I put them together I get a very weird result.
I am trying to use an RTC DS3231 with the Arduino sleep function and woken up with an interrupt from the RTC. (Real time clock) with the SQW pin of the RTC attached to my micros interrupt pin.
Each of these things work fine when done alone but once put together the code freezes and does not continue.
Here is my code as of now. I’m trying to wake the micro every minute, the global var T being the interval for the waiting time.
#include <avr/sleep.h>
#include <avr/power.h>
#include <Wire.h>
#include "ds3231.h"
struct ts t; // The Time structure
int T = 1; // The sleeping time in minutes
void setup()
{
pinMode(2,INPUT); // my interuppt is on pin 2
Serial.begin(9600);
Wire.begin();
DS3231_init(DS3231_INTCN); // Start the RTC interface
}
void loop()
{
Serial.println("Running"); // something to see that the micro is wake for a bit and not frozen
delay(5000); // a delay in my main code I will be loggin data
set_next_alarm(); /// set the next RTC alarm
DS3231_clear_a2f(); // clear a2 alarm flag and let INT go into high ,,, disabling the interrupt
sleep_enable(); // get sleep ready
attachInterrupt(2,wakeUp, LOW); // set the interrupt things
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is power down for most power savings
cli();
sei();
sleep_cpu(); // Sleeps here
sleep_disable(); // Wakes up here
// I have ccopied this bit of the sleeping code from the arduino fourums
}
void wakeUp() // run when interrupt is called
{
detachInterrupt(2); // detach the interrupt
sleep_disable();
}
void set_next_alarm(void) // Set the next alarm here
{
unsigned char wakeup_min;
DS3231_get(&t); // get the current time
// calculate the minute when the next alarm will be triggered
wakeup_min = (t.min / T + 1) * T;
if (wakeup_min > 59) {
wakeup_min -= 60;
}
boolean flags[4] = { 0, 1, 1, 1 };
DS3231_set_a2(wakeup_min, 0, 0, flags); // set the alarm 2
// activate Alarm2
DS3231_set_creg(DS3231_INTCN | DS3231_A2IE);
}
But I get a frozen program when the code runs it sleeps and the alarm is triggered on the RTC. I know the RTC is triggering because I have put an LED on the SQW pin and see it go LOW but the RTC keeps the pin LOW and nothing else happens.
Any ideas or help would be appreciated I have been at this for a good 3 weeks but don’t have a clue here.
attachInterrupt(2,wakeUp, LOW);
a) are you sure you got the right interrupt/pin here? b) "but the RTC keeps the pin high" - Why do you set up the interrupt forLOW
? - A level interrupt cannot wake up the device from anything than "idle". -- See if using "idle" sleep mode changes anything. – JimmyB