user16314: I am trying to setup an automated relay for my aquarium. The Arduino should switch off the pump every 3 hours for 20 minutes, and feed at particular times during the day. The code works fine for the first two on/off cycles. However after that the system stops responding.
I have tried many ways to solve the issue, I am led to believe that the issue could be a memory leak, which I have tried to fix that with watchdog timer reset, I have also tried resetting the Arduino but it doesn't work after the first reset.
I am still not sure what the problem could be, so any information would be appreciated.
I have included the code that I have stitched together using references from the web and a schematic of my circuit along with a picture of a tiny RTC module.
Schematic:

Tiny RTC:

My code is below, any advice would be appreciated.              
#include "Wire.h"
 #define DS1307_ADDRESS 0x68
 #define RELAY_PIN 2
 #define Feed_PIN 4
 #define led13 13

 void setup() {
   Wire.begin();
   Serial.begin(9600);
   pinMode(RELAY_PIN, OUTPUT);
   pinMode(Feed_PIN, OUTPUT);
   pinMode(led13, OUTPUT);
 }

 void loop() {  
   printDate();
   // digitalWrite(led13, HIGH);
   //delay(500);
   //delay(500);
   //digitalWrite(led13, LOW);
   // byte zero = 0x00;
   ///Wire.write(zero);
 }


 byte bcdToDec(byte val)  {
   // Convert binary coded decimal to normal decimal numbers
   return ( (val / 16 * 10) + (val % 16) );
 }


 void printDate() {
   // Reset the register pointer
   Wire.beginTransmission(DS1307_ADDRESS);
   byte zero = 0x00;
   Wire.write(zero);
   Wire.endTransmission();
   Wire.requestFrom(DS1307_ADDRESS, 7);
   int second = bcdToDec(Wire.read());
   int minute = bcdToDec(Wire.read());
   int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
   int weekDay = bcdToDec(Wire.read()); //0-6 -> sun-sat      
   int monthDay = bcdToDec(Wire.read());
   int month = bcdToDec(Wire.read());
   int year = bcdToDec(Wire.read());
   //print the date EG   3/1/11 23:59:59
   Serial.print(month);
   Serial.print("/");
   Serial.print(monthDay);
   Serial.print("/");
   Serial.print(year);
   Serial.print(" ");
   Serial.print(hour);
   Serial.print(":");
   Serial.print(minute);
   Serial.print(":");
   Serial.println(second);
   if ( (hour ==  9 && (minute >= 00 && minute <= 20)) ||
        (hour == 12 && (minute >= 20 && minute <= 40)) ||
        (hour == 15 && (minute >= 40 && minute <= 59)) ||
        (hour == 18 && (minute >= 00 && minute <= 20)) ||
        (hour == 20 && (minute >= 20 && minute <= 40)) ||
        (hour == 22 && (minute >= 40 && minute <= 59)) ||
        (hour ==  1 && (minute >= 00 && minute <= 20)) ||
        (hour ==  3 && (minute >= 20 && minute <= 40)) ||
        (hour ==  5 && (minute >= 40 && minute <= 59)))
   {
     Serial.print(month);
     Serial.print("/");
     Serial.print(monthDay);
     Serial.print("/");
     Serial.print(year);
     Serial.print(" ");
     Serial.print(hour);
     Serial.print(":");
     Serial.print(minute);
     Serial.print(":");
     Serial.println(second);
     Serial.print(':');
     Serial.print(':');
     Serial.println(", pump off!");

     digitalWrite(RELAY_PIN, HIGH);  
   } else {
     digitalWrite(RELAY_PIN, LOW);  
   }

   if ((hour ==  2 && minute == 43 && second >= 52) ||
       (hour == 10 && minute == 00 && second >= 52) ||
       (hour == 13 && minute == 00 && second >= 52) ||
       (hour == 16 && minute == 00 && second >= 52)) 
   {
     digitalWrite(Feed_PIN, HIGH);
     Serial.println("feeding fish.....");
   } else { 
     digitalWrite(Feed_PIN, LOW); 
   }
 }


user16318: Your code seems fine, I cannot give a comment yet due to me not being a high enough ranked member, so i will try to phrase this as annswer instead.
Your arduino might be the problem, but I don't think it is likely as the clock is known to be troublesome, but as "transistor" suggested you should let it run on the serial for some time so that you can see if time is reported correctly.
I time is not working then I would like to direct your attention to this thread on the arduino forum, as this is apperently a very poor module. Several people have clocks that doesn't function properbly and there is many post adressing troubleshooting, but since this is supposed to be answer I have taken the top issues and put in below.
https://forum.arduino.cc/index.php?topic=177297.0 
primarely they problems come from:
an unrechargeable Cr2032 battery is shipped, instead of a rechargeable li2032, this cause the charging Circuit on the board to act up.
Fix : replace to li2032 or remove charging circuitry - se link
ungrounded crystal - may or may not be a real issue
Fix: ground the crystal
Bad crystal:
Fix: replace crystal
Error in design - Resistor nr. 7 in place
Fix: remove resistor

16314: <p>I am trying to setup an automated relay for my aquarium. The Arduino should switch off the pump every 3 hours for 20 minutes, and feed at particular times during the day. The code works fine for the first two on/off cycles. However after that the system stops responding.</p>

<p>I have tried many ways to solve the issue, I am led to believe that the issue could be a memory leak, which I have tried to fix that with watchdog timer reset, I have also tried resetting the Arduino but it doesn't work after the first reset.</p>

<p>I am still not sure what the problem could be, so any information would be appreciated.</p>

<p>I have included the code that I have stitched together using references from the web and a schematic of my circuit along with a picture of a tiny RTC module.</p>

<p>Schematic:</p>

<p><img src="https://i.stack.imgur.com/kgoou.jpg" alt="">
Tiny RTC:</p>

<p><img src="https://i.stack.imgur.com/xrGPd.jpg" alt=""></p>

<p>My code is below, any advice would be appreciated.              </p>

<pre><code>#include "Wire.h"
 #define DS1307_ADDRESS 0x68
 #define RELAY_PIN 2
 #define Feed_PIN 4
 #define led13 13

 void setup() {
   Wire.begin();
   Serial.begin(9600);
   pinMode(RELAY_PIN, OUTPUT);
   pinMode(Feed_PIN, OUTPUT);
   pinMode(led13, OUTPUT);
 }

 void loop() {  
   printDate();
   // digitalWrite(led13, HIGH);
   //delay(500);
   //delay(500);
   //digitalWrite(led13, LOW);
   // byte zero = 0x00;
   ///Wire.write(zero);
 }


 byte bcdToDec(byte val)  {
   // Convert binary coded decimal to normal decimal numbers
   return ( (val / 16 * 10) + (val % 16) );
 }


 void printDate() {
   // Reset the register pointer
   Wire.beginTransmission(DS1307_ADDRESS);
   byte zero = 0x00;
   Wire.write(zero);
   Wire.endTransmission();
   Wire.requestFrom(DS1307_ADDRESS, 7);
   int second = bcdToDec(Wire.read());
   int minute = bcdToDec(Wire.read());
   int hour = bcdToDec(Wire.read() &amp; 0b111111); //24 hour time
   int weekDay = bcdToDec(Wire.read()); //0-6 -&gt; sun-sat      
   int monthDay = bcdToDec(Wire.read());
   int month = bcdToDec(Wire.read());
   int year = bcdToDec(Wire.read());
   //print the date EG   3/1/11 23:59:59
   Serial.print(month);
   Serial.print("/");
   Serial.print(monthDay);
   Serial.print("/");
   Serial.print(year);
   Serial.print(" ");
   Serial.print(hour);
   Serial.print(":");
   Serial.print(minute);
   Serial.print(":");
   Serial.println(second);
   if ( (hour ==  9 &amp;&amp; (minute &gt;= 00 &amp;&amp; minute &lt;= 20)) ||
        (hour == 12 &amp;&amp; (minute &gt;= 20 &amp;&amp; minute &lt;= 40)) ||
        (hour == 15 &amp;&amp; (minute &gt;= 40 &amp;&amp; minute &lt;= 59)) ||
        (hour == 18 &amp;&amp; (minute &gt;= 00 &amp;&amp; minute &lt;= 20)) ||
        (hour == 20 &amp;&amp; (minute &gt;= 20 &amp;&amp; minute &lt;= 40)) ||
        (hour == 22 &amp;&amp; (minute &gt;= 40 &amp;&amp; minute &lt;= 59)) ||
        (hour ==  1 &amp;&amp; (minute &gt;= 00 &amp;&amp; minute &lt;= 20)) ||
        (hour ==  3 &amp;&amp; (minute &gt;= 20 &amp;&amp; minute &lt;= 40)) ||
        (hour ==  5 &amp;&amp; (minute &gt;= 40 &amp;&amp; minute &lt;= 59)))
   {
     Serial.print(month);
     Serial.print("/");
     Serial.print(monthDay);
     Serial.print("/");
     Serial.print(year);
     Serial.print(" ");
     Serial.print(hour);
     Serial.print(":");
     Serial.print(minute);
     Serial.print(":");
     Serial.println(second);
     Serial.print(':');
     Serial.print(':');
     Serial.println(", pump off!");

     digitalWrite(RELAY_PIN, HIGH);  
   } else {
     digitalWrite(RELAY_PIN, LOW);  
   }

   if ((hour ==  2 &amp;&amp; minute == 43 &amp;&amp; second &gt;= 52) ||
       (hour == 10 &amp;&amp; minute == 00 &amp;&amp; second &gt;= 52) ||
       (hour == 13 &amp;&amp; minute == 00 &amp;&amp; second &gt;= 52) ||
       (hour == 16 &amp;&amp; minute == 00 &amp;&amp; second &gt;= 52)) 
   {
     digitalWrite(Feed_PIN, HIGH);
     Serial.println("feeding fish.....");
   } else { 
     digitalWrite(Feed_PIN, LOW); 
   }
 }
</code></pre>
 16318: <p>Your code seems fine, I cannot give a comment yet due to me not being a high enough ranked member, so i will try to phrase this as annswer instead.</p>

<p>Your arduino might be the problem, but I don't think it is likely as the clock is known to be troublesome, but as "transistor" suggested you should let it run on the serial for some time so that you can see if time is reported correctly.</p>

<p>I time is not working then I would like to direct your attention to this thread on the arduino forum, as this is apperently a very poor module. Several people have clocks that doesn't function properbly and there is many post adressing troubleshooting, but since this is supposed to be answer I have taken the top issues and put in below.</p>

<p><a href="https://forum.arduino.cc/index.php?topic=177297.0" rel="nofollow">https://forum.arduino.cc/index.php?topic=177297.0</a> </p>

<p>primarely they problems come from:</p>

<p>an unrechargeable <strong>Cr</strong>2032 battery is shipped, instead of a rechargeable <strong>li</strong>2032, this cause the charging Circuit on the board to act up.</p>

<p>Fix : replace to <strong>li</strong>2032 or remove charging circuitry - se link</p>

<p>ungrounded crystal - may or may not be a real issue</p>

<p>Fix: ground the crystal</p>

<p>Bad crystal:</p>

<p>Fix: replace crystal</p>

<p>Error in design - Resistor nr. 7 in place</p>

<p>Fix: remove resistor</p>
