Button interrupt with DS3231 RTC

I’m running into some weird (at least to me - as I’m a complete newbie) problems when using a simple sketch to get the values from an DS3231 to be displayed when an interrupt is called by a button press.

Here is the code I’m running, compiles fine:

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
byte year, month, date, DoW, hour, minute, second;
int buttonInterrupt0 = 1;
void setup() 
{
  pinMode(3, INPUT);
  digitalWrite(3, HIGH); 
  attachInterrupt(buttonInterrupt0, Button0Pressed, FALLING);
  Serial.begin(9600);
  Wire.begin();
}

void loop() 
{
  for (int i = 0; i < 100; i++)
  {
    delay(10);
  }
}


void Button0Pressed()
{
  Serial.print("Button pressed");
  ReadDS3231();
}

void ReadDS3231()
{
  Serial.print("RTC read");
  int second,minute,hour,date,month,year,temperature; 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
  temperature=Clock.getTemperature();
  
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");
  Serial.print(temperature); 
  Serial.print('\n');
}

I think this is pretty basic stuff, but when I press the button, nothing happens in serial monitor.

What I’ve done so far:

  1. tried running just the interrupt button part without the void ReadDS3231() like this:
void Button0Pressed()
{
  Serial.print("Button pressed");}

that works as expected.

  1. tried running just the contents of void ReadDS3231 in void loop, also works as expected and gives out the values repeatedly.

Has anyone got an idea what could be wrong here? I’m pretty sure it has to be something basic I’m overlooking.

THX in advance

uuzi

you cannot do a lot in an Interrupt Service Routine… like Serial.print() or reading a sensor:

void Button0Pressed()
{
  Serial.print("Button pressed");
  ReadDS3231();
}

try setting a simple flag and running the code outside the ISR like this:

uncompiled, untested:

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
byte year, month, date, DoW, hour, minute, second;
volatile boolean flag = false;
int buttonInterrupt0 = 1;
void setup() 
{
  pinMode(3, INPUT);
  digitalWrite(3, HIGH); 
  attachInterrupt(buttonInterrupt0, Button0Pressed, FALLING);
  Serial.begin(9600);
  Wire.begin();
}

void loop() 
{
  //Get rid of this nonsense:
  /*
  for (int i = 0; i < 100; i++)
  {
    delay(10);
  }*/
  if (flag)
  {
    Serial.print("Button pressed");
    ReadDS3231();
    flag = false;
  }
}


void Button0Pressed()
{
  flag = true;
}

void ReadDS3231()
{
  Serial.print("RTC read");
  int second,minute,hour,date,month,year,temperature; 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
  temperature=Clock.getTemperature();
  
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");
  Serial.print(temperature); 
  Serial.print('\n');
}