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);
}