This is what I have so far. The problem is, that the ran only twice and then it fall asleep forever. Don't you know where could be mistake? Maybe the code is total bullshit, I am trying...
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Streaming.h> //http://arduiniana.org/libraries/streaming/
#include <Time.h> //http://playground.arduino.cc/Code/Time
#include <Wire.h> //http://arduino.cc/en/Reference/Wire
#include <avr/sleep.h>
#define SQW_PIN 2
void setup(void)
{
Serial.begin(115200);
//setSyncProvider() causes the Time library to synchronize with the
//external RTC by calling RTC.get() every five minutes by default.
setSyncProvider(RTC.get);
Serial << "RTC Sync";
if (timeStatus() != timeSet){
Serial << " FAIL!";
}
Serial << endl;
printDateTime( RTC.get() );
Serial << " --> Current RTC time." << endl;
//Disable the default square wave of the SQW pin.
RTC.squareWave(SQWAVE_NONE);
//Attach an interrupt on the falling of the SQW pin.
//digitalWrite(SQW_PIN, HIGH); //redundant with the following line
pinMode(SQW_PIN, INPUT_PULLUP);
//Set an alarm at every 20th second of every minute.
RTC.setAlarm(ALM1_MATCH_SECONDS, 20, 0, 0, 1); //daydate parameter should be between 1 and 7
RTC.alarm(ALARM_1); //ensure RTC interrupt flag is cleared
RTC.alarmInterrupt(ALARM_1, true);
}
//volatile boolean alarmIsrWasCalled = false; don't think we need it
void alarmIsr()
{
Serial.println("alarmIsr"); // to get know whether the fuction ran or not
// alarmIsrWasCalled = true; don't think we need it
}
void sleepNow() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
// use interrupt 0 (pin 2) and run function
attachInterrupt(0,alarmIsr, CHANGE);// here the device is actually put to sleep!!
sleep_mode();
Serial.println("SleepNow"); //to get know whether the fuction ran or not
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
// sleep_disable(); // first thing after waking from sleep: disable sleep...
// detachInterrupt(0); // disables interrupt 0 on pin 2 so the wakeUpNow code will not be executed during normal running time.
}
int i = 1; //to get know how many times the loop went
void loop(void)
{
// alarmIsrWasCalled = false; don't think we need it
delay(100);
Serial.println(i);
delay(100);
i++;
sleepNow();
}
void printDateTime(time_t t)
{
Serial << ((day(t)<10) ? "0" : "") << _DEC(day(t)) << ' ';
Serial << monthShortStr(month(t)) << " " << _DEC(year(t)) << ' ';
Serial << ((hour(t)<10) ? "0" : "") << _DEC(hour(t)) << ':';
Serial << ((minute(t)<10) ? "0" : "") << _DEC(minute(t)) << ':';
Serial << ((second(t)<10) ? "0" : "") << _DEC(second(t));
}