Problems with RTC module

I have a 1307 rtc and i am having a few issues with it.

EDIT:

I wrote a simple sketch like this:

#include <Wire.h>
#include "RTClib.h"
 
RTC_DS1307 RTC;
 
void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }
 
}
 
void loop () {
    DateTime now = RTC.now();
    Serial.println(now.minute(), DEC);
    
}

The output on serial is like this:

12
12
12
12
165
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
0
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
165

As you can see, it is mostly correct, but often wrong. This is a problem because I am displaying the time on a LCD screen, and the fact that the numbers are changing is causing the lcd screen to shimmer. (if i remove the code to display the time, the shimmer is gone)

/////

original post below

I have set up the module so that the time is displayed on a character LCD. The time display correctly, however, i noticed there was a little "shimmer" on the display, on the line that displays the time.

I had though this was because that line was being over wrote, so i decided to write some code so that the screen would only be updated if the minutes had changed...

if(AoldMinute != minute || zoneSetup > 0){
    //Serial.println(zoneSetup);
   // Serial.println(AoldMinute);
    Serial.println("start");
    Serial.println(minute);
    Serial.println("end");
    AoldMinute = minute;
    //Serial.println(AoldMinute);
   refresh = 1;
  }

However, the problem with this is that when i print out minute, it changes frequently.

Here is the example serial output:

start
165
end
start
50
end
start
0
end
start
51
end
start
23
end
start
51
end
start
0
end
start
51
end
start
165
end
start
51
end
start
0

Interestingy, 23:51 is the time

here is where i get the minute from the rtc:

void loop()
{
  now = clock.now();
  month = now.month();
  day = now.day();
  dayName = now.dayOfWeek();
  year = now.year();
  hour = now.hour();
  timerHandler();

  if (hour > 12) {
    hour = hour - 12;
    PMflag = 1;
  } else PMflag = 0;

  if (hour == 0) {
    hour = 12;
  }




  minute = now.minute();
  second = now.second();

...

}

I have included my full code below.

RFledBoxTs.zip (14.1 KB)

EDIT:

I have moved the Serial.print instructions to right after minute = now.minute();

I get the right value most of the time, but about every 20 ~ 30 times through the loop, minute ends up being 0, 165, or 8...

Eight is the hour, but i have no idea what zero and 165 represent.

This is quite repetitive:

void fillProgramInformation() {
  if (programInformation[zone] == 1) {
    programInformation[program] = zoneFunction[0];
    programInformation[hue] = colorHSV[0].h;
    programInformation[saturation] = colorHSV[0].s;
    programInformation[value] = colorHSV[0].v;
    programInformation[speed] = zSpeed[0];
    programInformation[special] = colorSF[0];
    programInformation[timer] = timers[0];
  }
  if (programInformation[zone] == 2) {
    programInformation[program] = zoneFunction[1];
    programInformation[hue] = colorHSV[1].h;
    programInformation[saturation] = colorHSV[1].s;
    programInformation[value] = colorHSV[1].v;
    programInformation[speed] = zSpeed[1];
    programInformation[special] = colorSF[1];
    programInformation[timer] = timers[1];
  }
  if (programInformation[zone] == 3) {
    programInformation[program] = zoneFunction[2];
    programInformation[hue] = colorHSV[2].h;
    programInformation[saturation] = colorHSV[2].s;
    programInformation[value] = colorHSV[2].v;
    programInformation[speed] = zSpeed[2];
    programInformation[special] = colorSF[2];
    programInformation[timer] = timers[2];
  }
  if (programInformation[zone] == 4) {
    programInformation[program] = zoneFunction[3];
    programInformation[hue] = colorHSV[3].h;
    programInformation[saturation] = colorHSV[3].s;
    programInformation[value] = colorHSV[3].v;
    programInformation[speed] = zSpeed[3];
    programInformation[special] = colorSF[3];
    programInformation[timer] = timers[3];
  }
}

I recommend factoring like this:

void fillProgramInformation()
{
  if (programInformation[zone] >= 1 && programInformation[zone] <= 4)
  {
    programInformation[program] = zoneFunction[programInformation[zone]-1];
    programInformation[hue] = colorHSV[programInformation[zone]-1].h;
    programInformation[saturation] = colorHSV[programInformation[zone]-1].s;
    programInformation[value] = colorHSV[programInformation[zone]-1].v;
    programInformation[speed] = zSpeed[programInformation[zone]-1];
    programInformation[special] = colorSF[programInformation[zone]-1];
    programInformation[timer] = timers[programInformation[zone]-1];
  }
}

Don't take it as a random criticism. Repetition can be an insidious source of problems such as the one you are having, because of typos sneaking in when many lines have to be typed or changed together.

No, I appreciate any opportunity to make the code better.

thanks.

Qdeathstar:
No, I appreciate any opportunity to make the code better.

thanks.

There's a lot of code there, so of course I couldn't read it all. But I didn't see anything terribly wrong with the use of time variables. I'm beginning to suspect the library or RTC itself. Have you tested it independently of this program with a simpler time sketch?

By the way, anybody else that wants to look, the code in question is in the module _03_ButtonHandlers.

Aarg, i took your advice and wrote a simple sketch like this:

#include <Wire.h>
#include "RTClib.h"
 
RTC_DS1307 RTC;
 
void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }
 
}
 
void loop () {
    DateTime now = RTC.now();
    Serial.println(now.minute(), DEC);
    
}

The output on serial is like this (12 is correct, 0 and 165 are wrong):

12
12
12
12
165
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
0
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
165

As you can see, it is mostly correct, but often wrong. This is a problem because I am displaying the time on a LCD screen, and the fact that the numbers are changing is causing the lcd screen to shimmer. (if i remove the code to display the time, the shimmer is gone)

I'm using the rtc library recommended by adafruit.

Try your sketch using 4.7k external pull-up resistors on the i2c bus. The internal pull-ups enabled by the Wire library are relatively weak, and you may not be getting reliable readings.

I will give that a try, but I would expect random results if I was getting noise.... the values are always 0 or 165...

Qdeathstar:
I will give that a try, but I would expect random results if I was getting noise.... the values are always 0 or 165...

A DS1307 requires 5V operation.
Please check the voltage of your Arduino board with a multimeter.

If voltage falls below some threshold value, a DS1307 will deactivate its I2C interface and you read the 165 values (after BCD conversion).

So check for:

  • 5V voltage
  • loose contact