0

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); 
   }
 }
  • Important information: what time does it stop at and what is the last serial print? Add the info to your question rather than the comments. – Transistor Jan 11 '16 at 21:04
  • When do you intend to "digitalWrite(Feed_PIN, LOW);"? – Mikael Patel Jan 11 '16 at 22:02
  • the last serial print is nothing. as the serial monitor stops scrolling too. – user16314 Jan 11 '16 at 22:34
  • im not writing digital LOW because after the minute is over the if condition is not met – user16314 Jan 11 '16 at 22:35
  • 1
    How do you know the system has stopped responding? Serial monitor? If you put back your LED pin13 blink does it freeze? I'm asking about exact times because that may point us to your if clauses. – Transistor Jan 12 '16 at 10:22
1

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

  • Here's what the exact timings are :start time ::, pump off! 1/13/16 5:50:49 1/13/16 5:50:49 ::, pump off! 1/13/16 5:50:49 1/13/16 5:50:49 ::, pump off! 1/13/16 5:50:49 1/13/16 5:50:49 ::, pump off! 1/13/16 5:50:50 end time 1/13/16 15:42:55 1/13/16 15:42:55 ::, pump off! 1/13/16 15:42:55 1/13/16 15:42:55 ::, pump off! 1/1/0 0:0:80 1/1/0 0:0:80 1/1/0 0:0:80 1/1/0 0:0:80 1/1/0 0:0:80 1/1/0 0:0:80 after it starts spaziing 0:0:80 is the outpu – user16314 Jan 15 '16 at 21:29
0

Perhaps you can try while (Wire.available() != 7); before calling Wire.read() 7 times in succession. Also remove leading zeros in the integer literals of 0 in your IF conditions. And make bcdToDec return an int instead of a byte, in the unlikely case of any overflow issues.

0

I know a very rough brute force idea, but I'm afraid you won't like it.

Here, how it goes: Use additional relay for powering arduino: While arduino is turned on, let the power be connected to NC contact and after each cycle, just turn this output to HIGH. How it works, you ask? Well, after a cycle, you will with logic 1 (HIGH) state cause to "unplug" the arduino power, which will lead to loosing power supply. However, the relay has a spring, that in case of loosing power, it goes back to NC, in which case, your arduino will be reset and working for another cycle.

That should 100% work, but as I said before: It's a horrible brute force idea :)

-1

I had something same with my Arduino UNO weather station until I switch to the PSU with lead battery. The most common thing is the power supply. Try to use more powerful and use the ferrites on the PSU wires.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.