Problem with RTC DS1302

Hello, I built a project using the RTC ds1302 (clock module), an Arduino Uno and LCD 16x2.

Everything works great, the LCD shows the time, but the problem here is that a few seconds are skipped without a pattern.

This could be a problem of the clock module, or the arduino, or something else?

Sorry for my english, greetings from Brazil.

Hello Lucasorofino

Could you please post your code (use the code tags - the "#" button above the row of smileys).

the problem here is that a few seconds are skipped without a pattern.

Do you mean that the display stops for a few seconds and then catches up with the real time? Or does it jump forward a few seconds?

Regards

Ray

// ds1302 library can be found here : http://www.henningkarlsen.com/electronics

#include <LiquidCrystal.h>
#include <DS1302.h>

// Init the DS1302 (ce, data, clock);
DS1302 rtc(8, 9, 10);
Time t;

// Init the LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 

void setup()
{
// Used only in the beginning, to define day of week, time and date
rtc.setDOW(MONDAY); // Set Day-of-Week to FRIDAY
rtc.setTime(13, 10, 00); // Set the time to 12:00:00 (24hr format)
rtc.setDate(9, 6, 2014); // Set the date to August 6th, 2010

pinMode(7, OUTPUT); //Buzzer
pinMode(A0, OUTPUT); //LED

// Set the clock to run-mode, and disable the write protection
rtc.halt(false);
rtc.writeProtect(false);

// Setup LCD to 16x2 characters
lcd.begin(16, 2);
}

void loop()
{
// Display time centered on the upper line
lcd.setCursor(4, 0);
lcd.print(rtc.getTimeStr());

// Display abbreviated Day-of-Week in the lower left corner
lcd.setCursor(0, 1);
lcd.print(rtc.getDOWStr(FORMAT_SHORT));

// Display date in the lower right corner
lcd.setCursor(6, 1);
lcd.print(rtc.getDateStr());

do
   {
    t = rtc.getTime();
    digitalWrite(A0, HIGH); // turn on LED
    digitalWrite(7, HIGH); // turn on alarm
    lcd.setCursor(4,0);
    lcd.print("RIVOTRIL");
   }
while ((t.hour == 8) && (t.min == 02) && (t.sec == 00));

digitalWrite(A0, LOW); // turn off LED
digitalWrite(7, LOW); // turn off alarm
lcd.setCursor(4,0);
lcd.print(rtc.getTimeStr());
lcd.setCursor(12,0);
lcd.print(" ");


do
   {
    t = rtc.getTime();
    digitalWrite(A1, HIGH); // turn on LED
    digitalWrite(7, HIGH); // turn on alarm
    lcd.setCursor(4,0);
    lcd.print("PHARMATON");
   }
while ((t.hour == 8) && (t.min == 04) && (t.sec == 00));

digitalWrite(A1, LOW); // turn off LED
digitalWrite(7, LOW); // turn off alarm
lcd.setCursor(4,0);
lcd.print(rtc.getTimeStr());
lcd.setCursor(12,0);
lcd.print(" ");

do
   {
    t = rtc.getTime();
    digitalWrite(A2, HIGH); // turn on LED
    digitalWrite(7, HIGH); // turn on alarm
    lcd.setCursor(4,0);
    lcd.print("REDOXON ");
   }
while ((t.hour == 8) && (t.min == 7));

digitalWrite(A2, LOW); // turn off LED
digitalWrite(7, LOW); // turn off alarm
lcd.setCursor(4,0);
lcd.print(rtc.getTimeStr());

do
   {
    t = rtc.getTime();
    digitalWrite(A3, HIGH); // turn on LED
    digitalWrite(7, HIGH); // turn on alarm
    lcd.setCursor(4,0);
    lcd.print("REDOXON ");
   }
while ((t.hour == 8) && (t.min == 10));

digitalWrite(A3, LOW); // turn off LED
digitalWrite(7, LOW); // turn off alarm
lcd.setCursor(4,0);
lcd.print(rtc.getTimeStr());

delay (1000);
}

This is the code, an alarm and LED turns on at an especific time.

Here is an example of what happened, the display shows:

09:36:14, 09:36:15, 09:36:16, 09:36: 18, 09:36:19, 09:36:20....
09:41:39, 09:41:40, 09:41:42, 09:42:43, 09:41:44, 09:41:45

At these underlined moments, it jumps two seconds with a time of one second, and there's no sequency, it's aleatory.

You have delay(1000) at the end of loop().

During that 1 second delay, no other processing can happen, so the program is probably missing some of the changes in seconds. Try reducing or removing the delay.

Well, reducing the delay, it keeps the problem, but it jumps with the new time of delay.

When i removed, there were no jumps, but the display just gone crazy, I couldn't read the time because there were a lot of characters in front of, in a really strange way. By the way, the LEDs were blinking rapidly, as the buzzer was emitting sound very fast

Well, reducing the delay, it keeps the problem, but it jumps with the new time of delay.

What did you reduce the period to? How could the display jump by less than 1s since your display only shows seconds, not fractions of seconds?

Another possible problem is that you have several loops like this ...

do
   {
    t = rtc.getTime();
    digitalWrite(A0, HIGH); // turn on LED
    digitalWrite(7, HIGH); // turn on alarm
    lcd.setCursor(4,0);
    lcd.print("RIVOTRIL");
   }
while ((t.hour == 8) && (t.min == 02) && (t.sec == 00));

This will execute all the statements in the curly brackets even if the condition is not true.

Maybe what you are trying to do is this?

t = rtc.getTime();
while ((t.hour == 8) && (t.min == 02) && (t.sec == 00))
{
    digitalWrite(A0, HIGH); // turn on LED
    digitalWrite(7, HIGH); // turn on alarm
    lcd.setCursor(4,0);
    lcd.print("RIVOTRIL");
}

Try changing all these loops and then test again without the delay or with a short delay, such as delay(100)

Regards

Ray

Great! Without the delay I resolved the problem of skip seconds, but now I have another little problem.

Firstly I tried to use do/while without the delay, but the display was very stranger.

So I decided to use the way you put

t = rtc.getTime();
while ((t.hour == 8) && (t.min == 02) && (t.sec == 00))
{
    digitalWrite(A0, HIGH); // turn on LED
    digitalWrite(7, HIGH); // turn on alarm
    lcd.setCursor(4,0);
    lcd.print("RIVOTRIL");
}

and everything was OK, but my intention was to print in display "RIVOTRIL" for a few seconds and then gone back to the clock, but in this way, it doesn't gone back, it stayed forever with "RIVOTRIL".

So I tried with if/else:

t = rtc.getTime();
if ((t.hour == 10) && (t.min == 47) && (t.sec >= 20) && (t.sec <= 25))
{
    digitalWrite(A0, HIGH); // turn on LED
    digitalWrite(7, HIGH); // turn on alarm
    lcd.setCursor(4,0);
    lcd.print("RIVOTRIL");
}
else
{
    digitalWrite(A0, LOW); // apaga LED
    digitalWrite(7, LOW); // Desliga alarme
    lcd.setCursor(4,0);
    lcd.print(rtc.getTimeStr());
    lcd.setCursor(12,0);
    lcd.print(" ");

and the only problem here is that "RIVOTRIL" appears with the clock, not in the place of the clock. I mean, I can see, at the same time, this name and the clock, so, for 5 seconds it's unreadable, it's a mixture.

Anyway, thank you so much for one less problem!

Sorry, my mistake.

In the while loop, there needs to be this statement inside the loop as well as just before it.

t = rtc.getTime();

Ok, so here is what I did

while ((t.hour == 10) && (t.min == 47) && (t.sec == 10))
{
    t = rtc.getTime();
    digitalWrite(A0, HIGH); // turn on LED
    digitalWrite(7, HIGH); // turn on alarm
    lcd.setCursor(4,0);
    lcd.print("RIVOTRIL");
}
t = rtc.getTime();
while ((t.hour == 10) && (t.min == 47) && (t.sec == 14))
{
   
    digitalWrite(A0, LOW); // apaga LED
    digitalWrite(7, LOW); // Desliga alarme
    lcd.setCursor(4,0);
    lcd.print(rtc.getTimeStr());
    lcd.setCursor(12,0);
    lcd.print(" ");

If I put the t=rtc.getTime(); inside the loop of the second while, nothing happens, but with this structure, it works fine, except one thing.
The buzzer and the LED turn on for 4 seconds, but the print "RIVOTRIL" only stays for 1 second, and I wanted for the 4 seconds. :~

You will need to change the conditional of the first loop. Instead of comparing the seconds with 10, check for seconds >= 10 and seconds < 14.

And you should add getTime inside second loop. Otherwise, you will get stuck in that loop too :slight_smile:

Like this?

while ((t.hour == 10) && (t.min == 47) && (t.sec >= 10) && (t.sec <= 14))
{
    t = rtc.getTime();
    digitalWrite(A0, HIGH); // turn on LED
    digitalWrite(7, HIGH); // turn on alarm
    lcd.setCursor(4,0);
    lcd.print("RIVOTRIL");
}

while ((t.hour == 10) && (t.min == 47) && (t.sec == 14))
{
   t = rtc.getTime();
    digitalWrite(A0, LOW); // apaga LED
    digitalWrite(7, LOW); // Desliga alarme
    lcd.setCursor(4,0);
    lcd.print(rtc.getTimeStr());
    lcd.setCursor(12,0);
    lcd.print(" ");

Nothing happens with this way, only the time passing =(

In first loop condition, try <14 instead of <=14.

And could you post the latest complete code.