RTC interupt and button interupt

Greetings guys!

I am still stuck on same project.

This time is rtc ds3232 wake up and button wake up from sleep.

i have 2 codes which work perfect apart, but i don’t know how to combine them to work.

first is

#include <DS3232RTC.h>        //http://github.com/JChristensen/DS3232RTC
#include <Time.h>             //http://playground.arduino.cc/Code/Time
#include <Wire.h>             //http://arduino.cc/en/Reference/Wire
#define WAKE_PIN 3
#define LED_PIN 13

void setup(void)
{
    pinMode(LED_PIN, OUTPUT);

    // RTC config:
    RTC.squareWave(SQWAVE_NONE);
    pinMode(WAKE_PIN, INPUT_PULLUP);
    attachInterrupt(INT0, wakeUp, FALLING);  // INT0 == 0 for Atmega328p

    RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 1);
    RTC.alarm(ALARM_2);
    RTC.alarmInterrupt(ALARM_2, true);
}

void loop(void)
{
  if(RTC.alarm(ALARM_2)){
    digitalWrite(LED_PIN, HIGH);
    delay(5000);
    digitalWrite(LED_PIN, LOW);
  }
}

this is just trigger alarm from rtc.

second one is button wake up from sleep.

    #include <avr/sleep.h>  
      
    int wakePin = 2;                 // pin used for waking up  
    int led=13;  
      
    void wakeUpNow() {  
      // execute code here after wake-up before returning to the loop() function  
      // timers and code using timers (serial.print and more...) will not work here.  
      // we don't really need to execute any special functions here, since we  
      // just want the thing to wake up  
    }  
      
    void setup() {  
      pinMode(wakePin, INPUT_PULLUP);  
      pinMode(led, OUTPUT);   
      attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function wakeUpNow when pin 2 gets LOW  
    }  
      
    void sleepNow() {  
        set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here  
        sleep_enable();          // enables the sleep bit in the mcucr register  
        attachInterrupt(0,wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function  
        sleep_mode();            // here the device is actually put to sleep!!  
        // 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.  
    }  
      
    void loop() {  
      digitalWrite(led, HIGH);  
      delay(1000);  
      digitalWrite(led, LOW);  
      sleepNow();     // sleep function called here  
    }

I would like to combine those 2 codes in a way:

if i press button let turn on and off (later in process display is on and off) and goes back to sleeep

if time is as set les turn on and off (later it send sms) and goes back to sleeep

Can you help me please…

Is that so hard? or no one have idea?

Alright, what have YOU tried so far? Where are you stuck? What doesn't work as expected? What DO you expect?

Hint: the Arduino has multiple pins with interrupt capabilities, in fact every pin can cause a pin change interrupt.

DrDiettrich:
Hint: the Arduino has multiple pins with interrupt capabilities, in fact every pin can cause a pin change interrupt.

this is irrelavant, because i will put arduino in power down mode, so just pin 2, 3 are avaiable.

I have come so far…

#include <DS3232RTC.h>        //http://github.com/JChristensen/DS3232RTC
#include <Time.h>             //http://playground.arduino.cc/Code/Time
#include <Wire.h>             //http://arduino.cc/en/Reference/Wire
#define RTC_WAKE_UP 3
#define LED_PIN 13
#define BUTTON_WAKE_UP 2
#include <avr/sleep.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup(void)
{
  pinMode(LED_PIN, OUTPUT);
  // RTC config:
  RTC.squareWave(SQWAVE_NONE);
  pinMode(RTC_WAKE_UP, INPUT_PULLUP);
  //   attachInterrupt(INT0, , FALLING);  // INT0 == 0 for Atmega328p
  //attachInterrupt(digitalPinToInterrupt(RTC_WAKE_UP), wakeUp, FALLING);
  RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 1);
  RTC.alarm(ALARM_2);
  attachInterrupt(digitalPinToInterrupt(RTC_WAKE_UP), wakeUp, FALLING);

  RTC.alarmInterrupt(ALARM_2, true);

  pinMode(BUTTON_WAKE_UP, INPUT_PULLUP);


  Wire.begin();
  Serial.begin(9600);
  lcd.begin(20, 4);
  //lcd.noBacklight();
  lcd.backlight();
}
void sleepNow() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
  sleep_enable();          // enables the sleep bit in the mcucr register
  attachInterrupt(digitalPinToInterrupt(BUTTON_WAKE_UP), wakeUp, LOW);
  //  attachInterrupt(digitalPinToInterrupt(RTC_WAKE_UP), wakeUp, FALLING);

  // use interrupt 0 (pin 2) and run function
  sleep_mode();            // here the device is actually put to sleep!!
  // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
  sleep_disable();         // first thing after waking from sleep: disable sleep...
  detachInterrupt(0);
  detachInterrupt(1); // disables interrupt 0 on pin 2 so the wakeUpNow code will not be executed during normal running time.
}

void loop(void)
{
  if (RTC.alarm(ALARM_2)) {
    lcd.backlight();
    delay(1000); ////delays are just for serial print, without serial they can be removed
    lcd.clear();
    delay(1000);
    lcd.setCursor(0, 0);
    lcd.print("rtc - Init");
    lcd.setCursor(0, 1);
    lcd.print("SMS system");
    delay(10000);
    lcd.clear();
    //  lcd.noBacklight();
    // sleepNow();
  }

  lcd.backlight();
  delay(1000); ////delays are just for serial print, without serial they can be removed
  lcd.clear();
  delay(1000);
  lcd.setCursor(0, 0);
  lcd.print("butoon - Init");
  lcd.setCursor(0, 1);
  lcd.print("SMS system");
  delay(1000);
  lcd.clear();
  // lcd.noBacklight();

  // sleep mode is set here
  sleepNow();

}

void wakeUp () {
  //no-op

}

this is working… but now i would like to add that when i press button also wake up and do some action … like 2 times blink.

can you guys help me add this into code

Alright, then use those pins...

attachInterrupt(digitalPinToInterrupt(WAKE_PIN), anotherISR, FALLING);

digitalPinToInterrupt makes it more readable. But I would change the names, they are just terrible....

Thanks, that helped a lot but not enough :slight_smile:

now when i press button each time wake up (check)
when it should wake up from rtc - no go (but if i press button after time when it should be awake by rtc - text on lcd change).

here is code with lcd -

#include <DS3232RTC.h>        //http://github.com/JChristensen/DS3232RTC
#include <Time.h>             //http://playground.arduino.cc/Code/Time
#include <Wire.h>             //http://arduino.cc/en/Reference/Wire
#define RTC_WAKE_UP 3
#define LED_PIN 13
#define BUTTON_WAKE_UP 2
#include <avr/sleep.h>  
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); 
void setup(void)
{
    pinMode(LED_PIN, OUTPUT);
    // RTC config:
    RTC.squareWave(SQWAVE_NONE);
    pinMode(RTC_WAKE_UP, INPUT_PULLUP);
 //   attachInterrupt(INT0, , FALLING);  // INT0 == 0 for Atmega328p
//attachInterrupt(digitalPinToInterrupt(RTC_WAKE_UP), wakeUp, FALLING);
    RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 1);
    RTC.alarm(ALARM_2);
   
    RTC.alarmInterrupt(ALARM_2, true);

    pinMode(BUTTON_WAKE_UP, INPUT_PULLUP);  


 Wire.begin();
      Serial.begin(9600);
      lcd.begin(20, 4);
      //lcd.noBacklight();
      lcd.backlight();
}
  void sleepNow() {  
       set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here  
        sleep_enable();          // enables the sleep bit in the mcucr register  
       attachInterrupt(digitalPinToInterrupt(BUTTON_WAKE_UP), wakeUp, LOW);
       attachInterrupt(digitalPinToInterrupt(RTC_WAKE_UP), wakeUp, FALLING);

 // use interrupt 0 (pin 2) and run function  
       sleep_mode();            // here the device is actually put to sleep!!  
       // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP  
        sleep_disable();         // first thing after waking from sleep: disable sleep...  
        detachInterrupt(0);   
         detachInterrupt(1); // disables interrupt 0 on pin 2 so the wakeUpNow code will not be executed during normal running time.  
    }  
      
void loop(void)
{
  if(RTC.alarm(ALARM_2)){
      lcd.backlight();
      delay(1000); ////delays are just for serial print, without serial they can be removed
      lcd.clear();
      delay(1000); 
      lcd.setCursor(0, 0);
      lcd.print("rtc - Init");
      delay(10000); 
       lcd.clear();
             lcd.noBacklight();
      // sleepNow(); 
  }

      lcd.backlight();
      delay(1000); ////delays are just for serial print, without serial they can be removed
      lcd.clear();
      delay(1000); 
      lcd.setCursor(0, 0);
      lcd.print("butoon - Init");
      delay(1000); 
      lcd.clear();
      lcd.noBacklight();

 // sleep mode is set here  
     sleepNow(); 
  
}

void wakeUp (){
  //no-op

}

I like to complicate my life

Why not attach the interrupts in setup?

And can you, after the edit, please press Ctrl+T in the IDE before pasting the code? It's a total mess now...

If both interrupts must do the same, you can simply wire the button between GND and the same pin as the RTC interrupt pin. The RTC output is open collector (open drain) so there is no chance of blowing anything up.

sterretje:
If both interrupts must do the same, you can simply wire the button between GND and the same pin as the RTC interrupt pin. The RTC output is open collector (open drain) so there is no chance of blowing anything up.

Ill try this..

but i want to do that, that if rtc wake up - send sms and if button is pressed just data is displayed on screen.

if that works that both are on same pin.. it's ok... 1-2 sms more in 1 week are not problem.

I said "If both interrupts must do the same" :wink: So no, two different actions on one pin will not work. There is no way for the system to differentiate.

But if both actions run same code..than that will go?

Should indeed; both the RTC interrupt and the button interrupt will do the same thing.

OK. I was really bussy those days so i did put arduino project away for time..

So there is what ill try to do tomorow...

i will wire same both (button and rtc to same pin) and then when code starts ill do if (time is xx) then send sms else just show data and go to sleep again ..

hope that will work. if or if not.. ill tell you what i managed..

here is a question can i wire button directly so GND - Button - PIN2 or i need resistor between GND and button*

No need for a resistor.

smfoto:
here is a question can i wire button directly so GND - Button - PIN2 or i need resistor between GND and button*

A resistor is not required in series with the button, but to Vcc. If the button is open, this resistor pulls the input pin high, and the closed button will pull the input low. That's why every input pin has a built-in resistor to Vcc, which can be activated by mode INPUT_PULLUP.