Alarm_1 Interrupt Weird Trigger alerts

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

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar, then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code.

That code won't even compile. There is no closing brace at the end of setup().

Sorry :confused:

First Post, please consider my hand slapped. I will do better next time. I will read the link attached. As far as i can tell, everything posted as I pasted it to the discussion post.

I understand the the code wont compile as posted, I said in the description I only posted portions that were appropriate for the problem with the sketch. It compiles and loads just fine to the board.

Again, thanks for any feedback.

James

Can you successfully run the library alarm examples? Try one with polling of the interrupt flag, and another with the interrupt output.

pinMode(SQW_PIN, INPUT_PULLUP);

What RTC module are you using? If there is no external pull up to Vcc on the INT/SQW pin you might try adding one, as the internal pullup restance on the interrupt pin maybe too weak.

Yes, The library alarm examples do work on my board using the same pinout as my trouble sketch. I will try and use an additional pullup resistor. I am using the DS3231 module I got with the ELEGOO Mega2560 kit. I was curious, could the battery that comes with the module be low and therefore not producing the correct output on the SQW pin? I was thinking about changing it.

James

Yes, The library alarm examples do work on my board using the same pinout as my trouble sketch.

That's good news. It sounds like the hardware setup is OK if you have run an interrupt example as well as a polled example.

I was curious, could the battery that comes with the module be low and therefore not producing the correct output on the SQW pin?

Replacing the battery is always a good idea, but the library examples are working with the battery in the condition it is in.

Please tell me more about how things are powered. The battery should not be running the RTC if the Arduino is not in a sleep state.

You mentioned building an alarm clock, so I was thinking the Arduino was fully powered, and you were trying to wake up yourself. If you are trying to wake a sleeping Arduino with a battery backed rtc alarm interrupt you are in a different situation.

What would help us to help you is for you to provide an "MCVE" . That is a minimal, complete, verifiable example. Then others could actually run and trouble shoot your code. Use the code tags when posting

Problem solved! I went back and reviewed my setup better and also spent some time reading and re-reading karl Jacobsens documentation. I found that my code was out of order as well as I didnt account for alarm_1 reset flag as well as the alarm_2 at all. Once I cleaned up those issues it is now working wonderfully. thanks for the assistance, your questions got me back in the books.

James

Problem solved!

Good job sorting this out. :slight_smile:

From your user name and local time zone it looks like you are with the US Coast Guard somewhere outside of the US.

If so, I hope you are getting paid.