Posting only relevant part. Obviously I am using Arduino Mega.
#include <Wire.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <DS3232RTC.h>
#include <Time.h>
#include "LowPower.h"
#define WSPEED 0
#define RLEVEL 1
#define RTCINT 4
#define BTINT 5
SoftwareSerial bt(10,11); // RX, TX
const byte wPin = 2;
const byte rPin = 3;
const byte btPin = 18; //interrupt from bluetooth
const byte rtcPin = 19; //interrupt from RTC
tmElements_t tm; //time from RTC
int BluetoothData; //bluetooth data
void pin19Interrupt(void) {
}
void pin18Interrupt(void) {
}
void setup() {
Serial.begin(9600);
ADCSRA = 0; // disable ADC (analog to digital conversion)
pinMode(rPin, INPUT);
pinMode(wPin, INPUT);
pinMode(btPin, INPUT);
pinMode(rtcPin, INPUT);
bt.begin(38400);
Serial.println(String("__DATE__ = ") + __DATE__);
Serial.println(String("__TIME__ = ") + __TIME__);
setTime(cvt_date(__DATE__, __TIME__));
RTC.set(now());
RTC.squareWave(SQWAVE_NONE);
RTC.setAlarm(ALM2_EVERY_MINUTE,0,0,0);
//RTC.setAlarm(ALM1_MATCH_HOURS,0,0,0);
RTC.setAlarm(ALM1_EVERY_SECOND,0,0,0);
RTC.alarmInterrupt(ALARM_2, true);
Serial.println(String("System date = ") + day() + "/" + month() + "/" + year() + " " + hour() + ":" + minute() + ":" + second() + "\n");
delay(1000);
}
void loop() {
if(RTC.alarm(ALARM_1)) {
Serial.println("ALARM 1");
}
if (RTC.alarm(ALARM_2)) {
Serial.println("ALARM 2");
}
if (bt.available()) {
BluetoothData = bt.read();
if(BluetoothData == '!') {
}
}
Serial.println("Entering sleep mode");
delay(999);
attachInterrupt(WSPEED, pin19Interrupt, FALLING);
attachInterrupt(RLEVEL, pin18Interrupt, FALLING);
attachInterrupt(BTINT, pin18Interrupt, FALLING); //bt
attachInterrupt(RTCINT, pin19Interrupt, FALLING); //rtc
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
detachInterrupt(0);
delay(1);
}
//CONVERT __DATE__ and __TIME__ to time_t
time_t cvt_date(char const *date, char const *time) {
char s_month[5];
int year;
tmElements_t t;
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
sscanf(date, "%s %hhd %d", s_month, &t.Day, &year);
sscanf(time, "%2hhd %*c %2hhd %*c %2hhd", &t.Hour, &t.Minute, &t.Second);
t.Month = (strstr(month_names, s_month) - month_names) / 3 + 1;
t.Year = year - 1970;
return makeTime(t);
}
EDIT: Interestingly, if I switch jumper from pin 3(int.1) to pin 18(int.5) I can easily wake Arduino up using reed switch with this very code. What is going on? How the heck int.1 is any different from int.5?