Go Down

Topic: Alarm_1 Interrupt Weird Trigger alerts (Read 139 times) previous topic - next topic


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>        //https://github.com/JChristensen/Button
#include <DS3232RTC.h>        //http://github.com/JChristensen/DS3232RTC
#include <Streaming.h>        //http://arduiniana.org/libraries/streaming/
#include <Time.h>         //http://www.arduino.cc/playground/Code/Time 
#include <Wire.h>         //http://arduino.cc/en/Reference/Wire (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_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

// 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.alarmInterrupt(ALARM_1, false);
  setSyncProvider(RTC.get);               // set RTC as the Syncprovider
  setSyncInterval(5);                     // time in sec of resync with RTC
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.
attachInterrupt(digitalPinToInterrupt(SQW_PIN), alarmIsr, FALLING);
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.print("Wake up!!!");
               lcd.print("Wake up!!!");
    for(int i=0;i<25; i++){
      if (buttonbtnBACK.wasPressed())
           alarmIsrWasCalled = false; 


        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]// your code is here[/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  :smiley-confuse:

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.



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

Code: [Select]

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.



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.



Problem solved!
Good job sorting this out.  :)

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. 

Go Up