chang RTC time on demand

I use this library:

Setting the initial time WITHIN the setup() works alright but using the same code (for example) on a button press seems to hang my arduino. (loop() is not running anymore)

setTime(13, 12, 11, 2, 11, 2014);
RTC.set(now());

Could it be a problem in this library.? Maybe there should be a stop command first before writing to the RTC? Any suggestion or knowledge about this library?

btw, only using setTime works alright, but it's the RTC.set(now()) command that hangt my arduino.

but using the same code (for example) on a button press seems to hang my arduino. (loop() is not running anymore)

Please post the example code using the button press which hangs the Arduino. It's not clear how you are loading the values into the setTime function, and how you are calling the RTC.set with buttons.

If you need help, you really have to show us all your code and an image of your wiring.

Have you read this? HERE

What happens if you try to do it twice in your setup function?

KenF, putting it in there twice gives me the same result.

// Interrupt service routine for interrupt 1
void incrementTime() {
  buttonincr_time = millis();
  //check to see if increment() was called in the last 250 milliseconds
     if (buttonincr_time - last_buttonincr_time > 250)
     {
       uren = hour() + 1;
       /*
       setTime(uren, minute(), second(), day(), month(), year());
       RTC.set(now());
       */
       setTime(13, 12, 11, 2, 11, 2014);
       RTC.set(now());
     }
  //remember last millis for next button press
  last_buttonincr_time = buttonincr_time;
}

Like I wrote, commenting RTC.set(now()); and everything works as aspected. As you can see, I tried to set time with a variable but also hardcoded as above. But both ways work and the Time library updates the time. But calling RTC.set(now()) stop the loop()

LarryD: don't have the wiring digital, only on a breadboard :wink: If above is not enough I can post the 800lines of code.

Creamers,

You have not provided complete code, but from your comments its looks like you are trying to set the RTC from within an ISR.

The Wire library and I2C require interrupts to be enabled, but they are disabled within the ISR.

Poll for the button press in your loop, and try calling your setting function in response to that, instead of from an interrupt.

Edit: Another approach would use the ISR to set a flag, and then check for that flag within the loop.

cattledog:
its looks like you are trying to set the RTC from within an ISR.

Good catch! I don't usually read the comments so totally missed that.

@cattledog, will try it right away when I'm at home!

Thx for this detailed information.!

@cattledog: Works like a charm !

Everyone thanks for helping out!