RTC Preventing serial output

First of all, I'm sorry if this is posted in the wrong section... i'm new to forums :drooling_face:

I had a working program and project until this week. It all suddenly stopped and I could no longer read form the serial port of my Uno. Started messing with it and concluded the board died because I could no longer write/upload sketches to it. Ordered a brand new board and I am running into problems. This sketch/program was fully functional for 6 months and has only had hardware changes. The only thing I have added is some Serial.print lines for troubleshooting as well as removing my latitude and longitude.

I have an Arduino Uno R3 with an attached I2c tiny RTC attached through the Analog ports. I know the RTC is "wired" correctly, it will work with the example sketch from the RTClib.h library.

However I cannot get my project to properly run. It appears as if the Serial monitor/ entire program stops working after I run "if(!rtc.isrunning())." I only can get the program to output test1. If i unplug the RTC, it outputs "properly" but with the wrong time obviously.

Suggestions?

Also, for those wondering,my project is to power a relay that powers a linear actuator that opens and closes a gate for my chicken coop at sunrise and sunset.

ChickenCoopRTC_PT2Fpublic.ino (6.57 KB)

Please read the advicing topics like "How to use the Frum", "How to attach code" etc.
Software doesn't break down so something, somewhere, in the hardware being not 100% the best is what I would think of.
I strongly ask for the wiring diagram and links to the hardware.

ais4awesome4:
This sketch/program was fully functional for 6 months and has only had hardware changes.

That strongly suggests that "twas the hardware changes what done it" :slight_smile:

What were they?

Have you tried undoing them?

...R

The original uno seems to have died. I bought another uno and uploaded the sketch. Do you think the RTC could be defective even though it works with the example sketch?

Do you think the RTC could be defective even though it works with the example sketch?

Not likely. There may be problems running on the battery when the system is powered down, but that is not your present issue.

What is connected to A3?

Do you only see "Testing 1" but not "Testing 3" in your output?

#include <Dusk2Dawn.h>
#include <RTClib.h>

RTC_DS1307 rtc;

// Multiple instances can be created. Arguments are longitude, latitude, and
// time zone offset in hours from UTC.
// The first two must be in decimal degrees (DD), e.g. 10.001, versus the more
// common degrees, minutes, and seconds format (DMS), e.g. 10° 00′ 3.6″. The
// time zone offset can be expressed in decimal fractions, e.g. "5.75" for
// Nepal Standard Time, in the few cases where the zones are offset by 30 or
// 45 minutes.
// HINT: An easy way to find the longitude and latitude for any arbitrary
// location is to find the spot in Google Maps, right click the place on the
// map, and select "What's here?". At the bottom, you’ll see a card with the
// coordinates.
Dusk2Dawn chickenCoop(long,lat ,offset );

void setup() 
{
  Serial.begin(9600);
  if(!rtc.begin())
  {
    Serial.println ("RTC NOT FOUND!");
    while (1);
  }

    Serial.println("testing1");

  if(!rtc.isrunning()) 
  {
        Serial.println("testing2");

    Serial.println("RTC is NOT running!");
    // this will only complie if batery is dead/missing
    // following line sets the RTC to the date & time this sketch was compiled
     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // Following is to set specific date and time
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

    Serial.println("testing3");


  pinMode(A3, OUTPUT);
  digitalWrite(A3, HIGH);
  pinMode(A2, OUTPUT);
  digitalWrite(A2, LOW);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
}

//*****************************************************************************************

int setDatesunrise(DateTime & currTime);
int setDateSunset(DateTime & currTime);
void calcSunrise(const int chickenCoopSunrise, char sunriseTime[]);
void calcSunset(const int chickenCoopSunset, char sunriseTime[]);
void convertSRT (int & srHour, int & srMin, const char sunriseTime[]);
void convertSST (int & ssHour, int & ssMIN, const char sunsetTime[]);
void displayTime(const int hr, const int min, const int sec);
void closeGate(const int ssHour, const int ssMin, const int hr, const int min);
void openGate(const int srHour, const int srMin, const int hr, const int min);
void addBufferSR(int & srHour, int & srMin);
void addBufferSS(int & ssHour, int & ssMin);

//*****************************************************************************************

void loop() 
{
  DateTime currTime = rtc.now();
  int hr = (currTime.hour()),
      min = (currTime.minute()),
      sec = (currTime.second()),
      srHour,
      srMin,
      ssHour,
      ssMin,
      chickenCoopSunrise,
      chickenCoopSunset;
  char sunriseTime[6] = {'2','0',':','3','9','\0'},
       sunsetTime[6] = {'2','0',':','3','7','\0'};
  bool isClosed = false;

  Serial.print("Time is: ");
  Serial.print(hr);
  Serial.print(":");
  Serial.print(min);
  Serial.print(":");
  Serial.println(sec);

  chickenCoopSunrise = setDateSunrise(currTime);
  chickenCoopSunset = setDateSunset(currTime);
  calcSunrise(chickenCoopSunrise, sunriseTime);
  calcSunset(chickenCoopSunset, sunsetTime);
  convertSRT(srHour, srMin, sunriseTime);
  addBufferSR(srHour, srMin);
  convertSST(ssHour, ssMin, sunsetTime);
  addBufferSS(ssHour, ssMin);
  displayTime(hr, min, sec);
  closeGate(ssHour, ssMin, hr, min);
  openGate(srHour, srMin, hr, min);
  
  delay(15000);
}

//*********************************************************************************************

int setDateSunrise(DateTime & currTime)
{
  int y = (currTime.year());
  int m = (currTime.month());
  int d = (currTime.day());

  // Available methods are sunrise() and sunset(). Arguments are year, month,
  // day, and if Daylight Saving Time is in effect.
  int chickenCoopSunrise = chickenCoop.sunrise(y, m, d, false);

  return chickenCoopSunrise;
}

int setDateSunset(DateTime & currTime)
{
  int y = (currTime.year());
  int m = (currTime.month());
  int d = (currTime.day());

  // Available methods are sunrise() and sunset(). Arguments are year, month,
  // day, and if Daylight Saving Time is in effect.
  int chickenCoopSunset = chickenCoop.sunset(y, m, d, false);
  
  return chickenCoopSunset;
}

void calcSunrise(const int chickenCoopSunrise, char sunriseTime[])
{
  Dusk2Dawn::min2str(sunriseTime, chickenCoopSunrise);
  Serial.print("Sunrise is:\t ");
  Serial.println(sunriseTime);
}

void calcSunset(const int chickenCoopSunset, char sunsetTime[])
{
  Dusk2Dawn::min2str(sunsetTime, chickenCoopSunset);
  Serial.print("Sunset is:\t ");
  Serial.println(sunsetTime);
}

void convertSRT(int & srHour, int & srMin, const char sunriseTime[])
{
  char strHour[3],
       strMin[3];
       
  strHour[0] = sunriseTime[0];
  strHour[1] = sunriseTime[1];
  strHour[2] = '\0';
  strMin[0] = sunriseTime[3];
  strMin[1] = sunriseTime[4];
  strMin[2] = '\0';
  
  srHour = atoi(strHour);
  srMin = atoi(strMin);
}

void convertSST(int & ssHour, int & ssMin, const char sunsetTime[])
{
  char strHour[3],
       strMin[3];
       
  strHour[0] = sunsetTime[0];
  strHour[1] = sunsetTime[1];
  strHour[2] = '\0';
  strMin[0] = sunsetTime[3];
  strMin[1] = sunsetTime[4];
  strMin[2] = '\0';
  
  ssMin = atoi(strMin);
  ssHour = atoi(strHour);
}

void displayTime(const int hr, const int min, const int sec)
{
  Serial.print ("Current time is: ");
  Serial.print(hr);
  Serial.print(":");
  Serial.print(min);
  Serial.print(":");
  Serial.println(sec);
  Serial.println("***********************************************");
}

void closeGate(const int ssHour, const int ssMin, const int hr, const int min)
{
  if(hr == ssHour)
  {
    if(min == ssMin)
    {
      digitalWrite(9,LOW);
      delay(1000);
      digitalWrite(8,HIGH);
      delay(45000);
      digitalWrite(8,LOW);
    }
  }
}

void openGate(const int srHour, const int srMin, const int hr, const int min)
{
  if(hr == srHour)
  {
    if (min == srMin)
    {
      digitalWrite(8,LOW);
      delay(1000);
      digitalWrite(9,HIGH);
      delay(45000);
      digitalWrite(9,LOW);
    }
  }
}

void addBufferSR(int & srHour, int & srMin)
{
  srHour--;
  
  Serial.print("Gate will open at: ");
  Serial.print(srHour);
  Serial.print(":");
  Serial.println(srMin);
  
}

void addBufferSS(int & ssHour, int & ssMin)
{
  ssMin += 30;

  if(ssMin > 60)
  {
    ssHour++;
    ssMin = ssMin - 60;
  }

   Serial.print("Gate will close at: ");
   Serial.print(ssHour);
   Serial.print(":");
   Serial.println(ssMin);
  
}

I know this isn't what you're asking about but anything you can do to whittle down the size of this thing, would be a good thing.

Your two functions here, do exactly the same thing. You can call either one, with the same data, and you will get the same result. That is wasteful. You should just pick one and rename it in a generic way:

void convertSRT(int & srHour, int & srMin, const char sunriseTime[])
{
  char strHour[3],
       strMin[3];
       
  strHour[0] = sunriseTime[0];
  strHour[1] = sunriseTime[1];
  strHour[2] = '\0';
  strMin[0] = sunriseTime[3];
  strMin[1] = sunriseTime[4];
  strMin[2] = '\0';
  
  srHour = atoi(strHour);
  srMin = atoi(strMin);
}

void convertSST(int & ssHour, int & ssMin, const char sunsetTime[])
{
  char strHour[3],
       strMin[3];
       
  strHour[0] = sunsetTime[0];
  strHour[1] = sunsetTime[1];
  strHour[2] = '\0';
  strMin[0] = sunsetTime[3];
  strMin[1] = sunsetTime[4];
  strMin[2] = '\0';
  
  ssMin = atoi(strMin);
  ssHour = atoi(strHour);
}

Also it can be written:

void convertSxT(int & ssHour, int & ssMin, const char sunTime[])
{
  ssMin = atoi(sunTime + 3);
  ssHour = atoi(sunTime);
}

:wink:

void convertSRT(int & srHour, int & srMin, const char sunriseTime[])
{
  char strHour[3],
       strMin[3];
       
  strHour[0] = sunriseTime[0];
  strHour[1] = sunriseTime[1];
  strHour[2] = '\0';
  strMin[0] = sunriseTime[3];
  strMin[1] = sunriseTime[4];
  strMin[2] = '\0';
 
  srHour = atoi(strHour);
  srMin = atoi(strMin);
}

The majority of that code can be eliminated because of the way atoi() processes the text, ignoring the remainder of the string once a non-numeric character is encountered. All you need are these two lines:

  srHour = atoi(sunriseTime);
  srMin = atoi(sunriseTime+3);

Although I would not even go to the trouble of using atoi(). Since Dusk2Dawn gives you the sunrise and sunset time in minutes past midnight, a simple calculation on that number will give you hours and minutes. Unfortunately doing that may also produce a slightly different time than the min2str() function in the library because the library does a simple integer calculation using floats.

A bit tangental but may save you money is that with an Uno with socketed AVR, you can replace the AVR chip with another bootloaded AVR. You can buy bootloaded or load a bootloader on a blank yourself.

That covers burned the chip mistakes but still there are board bits possible to kill but AVR pins should die first. Your chip may have died and the board be okay. IIRC, a loopback test could have told you that.

Removing the chip and putting it in a standalone project is also why the Uno has a socket --- you keep the dev board out of the end product and you have it for the next thing.

Here's an on-ramp for bootloading your own AVR's and minimal Duino circuits with different clock options, etc.
https://gammon.com.au/breadboard

The Uno socket can take Atmega328/168/88/48 family chips, all pin-compatible. Need less RAM and making many? Use the smaller memory chip and save some money.

Let's see, code uploaded 6 months ago, Arduino broke bought a new one, if new one was installed correctly and old parts still work, then should work. What changed in 6 months? Uno and library updates.

I set up a weather station, in Feb, using a BME680. A few weeks ago, I pulled the weather station in to do a code update, the weather station stopped working. Found the uController was spitting out errors about the sensor being bad. Found the library I used in Feb. had 2 updates since then. Reverted back to the version from Feb. all worked again and I got the library author to fix the library. Just a possibility.

You might have been able to fix the broken Arduino with a new chip in the socket, unless your Uno was a "Pro" with surface mount Atmega328.

Call me strange but I think people hang in longer when it doesn't cost so much. Blank 328P-PU costs less than $2.50 (did not long ago, I got 4 or 5 for $2.15 ea.

Idahowalker:
Let's see, code uploaded 6 months ago, Arduino broke bought a new one,

My reading of the Original Post is that some hardware changes were made BEFORE the Arduino broke.

...R