BLUF: I need to make an alarm clock. This code works, just doesnt work correctly. I can set the alarm. The time comes and goes and no alarm. Nothing happens. If I pull the pin out of the arduino interrupt slot, the alarm triggers, the LCD displays and the buzzer operates. Why? I can't figure out how to make it work without having to physically pull the pin. Any help is greatly appreciated.
Arduino Mega2560 and DS3231 RTC and DHT11
Here posts of signifcant portions of sketch:
#include <Button.h> //GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses
#include <DS3232RTC.h> //GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks
#include <Streaming.h> //Streaming | Arduiniana
#include <Time.h> //Arduino Playground - Time
#include <Wire.h> //Wire - Arduino Reference (included with Arduino IDE)
#include <EEPROM.h>
#include <LiquidCrystal.h>
const int rs = 53, en = 52, d4 = 51, d5 = 50, d6 = 49, d7 = 48;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//DHT11 code
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}
#include <dht11.h> // DHT11 info from LAB2-3 sketch
dht11 DHT11;
#define DHT11PIN 47
#define BUTTON_PIN_UP 6
#define BUTTON_PIN_DOWN 5
#define BUTTON_PIN_MENU_SELECT 8
#define BUTTON_PIN_btnBACK 4
#define SQW_PIN 2 //interrupt Alarm pin
#define DEBOUNCE_MS 20 //A debounce time of 20 milliseconds usually works well for tactile button switches.
#define REPEAT_FIRST 1000 //ms required before repeating on long press
#define REPEAT_INCR 200 //repeat interval for long press
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
#define INVERT true
int count; //The number that is adjusted
int lastCount = -1; //Previous value of count (initialized to ensure it's different when the sketch starts)
unsigned long rpt = REPEAT_FIRST; //A variable time that is used to drive the repeats for long presses
Button buttonUp(BUTTON_PIN_UP, PULLUP, INVERT, DEBOUNCE_MS); //Declare the buttons
Button buttonDown(BUTTON_PIN_DOWN, PULLUP, INVERT, DEBOUNCE_MS);
Button buttonSelect(BUTTON_PIN_MENU_SELECT, PULLUP, INVERT, DEBOUNCE_MS);
Button buttonbtnBACK(BUTTON_PIN_btnBACK, PULLUP, INVERT, DEBOUNCE_MS);
// input actions
enum {btnMENU, btnBACK, btnUP, btnDOWN};
int8_t button;
int8_t trigger;
int Second;
int Minute;
int Hour;
int Day;
int Month;
int Year;
int8_t DoW;
String day_of_week;
unsigned char address, data;
int AL_Hour;
int AL_Minute;
bool alarm_active = false;
int alarm_out = 10;
uint32_t blink_interval = 500;
uint32_t blink_previousMillis = 0;
boolean blink_state = false;
boolean RTC_error = true;
boolean long_press_button = false;
void setup()
{
RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);
RTC.alarm(ALARM_1);
RTC.alarmInterrupt(ALARM_1, false);
RTC.squareWave(SQWAVE_NONE);
setSyncProvider(RTC.get); // set RTC as the Syncprovider
setSyncInterval(5); // time in sec of resync with RTC
lcd.begin(16,2);
Wire.begin();
pinMode(11,INPUT_PULLUP); // Not sure what this is for?
pinMode(alarm_out, OUTPUT);
RTC.squareWave(SQWAVE_NONE); //Disable the default square wave of the SQW pin.
pinMode(SQW_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SQW_PIN), alarmIsr, FALLING);
RTC.alarm(ALARM_1);
RTC.alarmInterrupt(ALARM_1, true);
volatile boolean alarmIsrWasCalled = false;
void alarmIsr()
{
alarmIsrWasCalled = true;
}
void loop()
{
change_states(); // change states of FSM
check_inputs(); // check inputs (buttons)
check_RTC(); // check connection RTC
if (alarmIsrWasCalled){
if (RTC.alarm(ALARM_1)) {
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Wake up!!!");
lcd.setCursor(1,1);
lcd.print("Wake up!!!");
for(int i=0;i<25; i++){
buttonbtnBACK.read();
if (buttonbtnBACK.wasPressed())
{
alarmIsrWasCalled = false;
break;
}
beep();
}
}
lcd.clear();
}
alarmIsrWasCalled = false;
}// End of LOOP