Real time clock DS1302 keeps resetting after few days

Hi guys,
Can someone please look into my code and say did I doo something wrong there?

I'm trying to automate aquarium light dimming, everything works except every two days clock starts counting from zero, and i think its around 14:30 every time, not so sure.


#include <Stepper.h>
#include <virtuabotixRTC.h> //Library used

const int stepsPerRevolution = 1050;
int  curentStep =0;
bool radi = true;
// Creates an instance of stepper class
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
Stepper myStepper = Stepper(stepsPerRevolution, 2, 7, 3, 8);
virtuabotixRTC myRTC(11, 12, 13);
int sun;
void setup() {
  pinMode(4, OUTPUT);
  //myRTC.setDS1302Time(5, 49, 19, 3, 12, 1, 2024);
 // seconds, minutes, hours, day of the week, day of the month, month, year
   myStepper.setSpeed(10);
   Serial.begin(9600);  
  
}



void loop() {
myRTC.updateTime();
Serial.print(myRTC.hours);
      Serial.print(":");
      Serial.print(myRTC.minutes);   
      Serial.print(":");
      Serial.println(myRTC.seconds);
    
 if ((myRTC.hours >= 8) && (myRTC.hours < 10) ){
  if(myRTC.seconds == 0  || myRTC.seconds == 20 || myRTC.seconds == 40  ){
   if(radi == true){
     if(curentStep < 360){
      curentStep  += 1;
      myStepper.step(1);
      Serial.print("step is: ");
      Serial.println(curentStep);
    }
    }
  }
  sun = 1;
 }
 else if((myRTC.hours >= 10) && (myRTC.hours < 13)){
    if(myRTC.seconds == 0  || myRTC.seconds == 10 || myRTC.seconds == 20  || myRTC.seconds == 30 || myRTC.seconds == 40 || myRTC.seconds == 50 || myRTC.seconds == 55  ){
   if(radi == true){
     if(curentStep < 1050){
      curentStep  += 1; 
      myStepper.step(1);
      Serial.print("step is: ");
      Serial.println(curentStep);
    }
    else{
      digitalWrite(4, HIGH);
    }
    
    }
  }
  sun = 2;
 }
 else if((myRTC.hours >= 15) && (myRTC.hours < 17)){
  
     if(myRTC.seconds == 0  || myRTC.seconds == 10 || myRTC.seconds == 20  || myRTC.seconds == 30  || myRTC.seconds == 40  || myRTC.seconds == 50 ){
   if(radi == true){
      digitalWrite(4, LOW);
     if(curentStep >360){
      curentStep  -= 1;
      myStepper.step(-1);
      Serial.print("step is: ");
      Serial.println(curentStep);
    }
    }
  }
  sun = 3;
 }
  else if((myRTC.hours >= 17) && (myRTC.hours < 20)){
    if(myRTC.seconds == 0  || myRTC.seconds == 20  || myRTC.seconds == 40 ){
   if(radi == true){
     if(curentStep >0){
    curentStep  -= 1;
    myStepper.step(-1);

Serial.print("step is: ");
Serial.println(curentStep);
    }
    }
  }
  sun = 4;
 }
 else{
  sun = 0;
 }

delay(500);
radi = !radi;
}


"radi" changes its value at a rate of almost two times per second. It mandates if the stepper moves.

What is radi for?

I'm using delay 500ms not to miss condition when myRTC.seconds == xx.
radi I am using just to make sure that code is used every second, not half a second.
You think that it can cause the reset?

You could try a 1K resistor between the data pin and the Arduino like here
1rtc

I saw that example, was thinking to try it out tomorrow. Maybe will help.

Hello, just to say I have changed RTC module.
Everything is fine now.
Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.