Reef LED Light Using DS1307 Randomly Outputting Bad Times

Hey all, working on a fade in/out reef LED lighting system. Adding the code and libraries used below. Code complies with no issues, but when running in the serial monitor I’m outputting an hour:minute using the Serial.print command. Let my code run overnight hooked up to some test leds and it is being thrown by bad RTC values being output randomly in the times. See below. I have also attached my current code. Please let me know if there are any suggestions on why the RTC output is doing this, or I have anything coded incorrectly.

33 10:28 White:18.67% In=1 Out=0 Blue:38.67% In=1 Out=0
34 10:28 White:18.67% In=1 Out=0 Blue:38.67% In=1 Out=0
35 165:165 White:18.67% In=1 Out=0 Blue:38.67% In=1 Out=0
36 10:28 White:18.67% In=1 Out=0 Blue:38.67% In=1 Out=0
37 10:28 White:18.67% In=1 Out=0 Blue:38.67% In=1 Out=0

8 10:29 White:19.33% In=1 Out=0 Blue:39.11% In=1 Out=0
9 10:29 White:19.33% In=1 Out=0 Blue:39.11% In=1 Out=0
10 21:29 White:19.33% In=1 Out=1 Blue:39.11% In=1 Out=1
11 10:29 White:19.33% In=1 Out=1 Blue:39.11% In=1 Out=1

28 10:55 White:25.33% In=1 Out=0 Blue:40.44% In=1 Out=0
29 10:55 White:25.33% In=1 Out=0 Blue:40.44% In=1 Out=0
30 3:20 White:25.33% In=1 Out=0 Blue:40.44% In=1 Out=0
31 10:55 White:25.33% In=1 Out=0 Blue:40.44% In=1 Out=0

45 11:52 White:58.67% In=1 Out=0 Blue:40.44% In=1 Out=0
46 3:20 White:58.67% In=1 Out=0 Blue:40.44% In=1 Out=0
47 11:52 White:58.67% In=1 Out=0 Blue:40.44% In=1 Out=0
48 11:52 White:58.67% In=1 Out=0 Blue:40.44% In=1 Out=0

//2.7 Gallon Pico Reef LED Light Controller Version 1.01
//Base code created by Harlequin 22-01-11.
//Enhanced by plant_arms 2012-08-25
//Based on various code libraries avalable on the public domain along with coding compiled by the author.
 
#include <Wire.h>
#include "RTClib.h"
 
RTC_DS1307 RTC;

int whiteFadeOn = 0;
int whiteFadeOff = 0;
int blueFadeOn = 0;
int blueFadeOff = 0;

int bluePin = 5;                // blue LEDs connected to digital pin5
int whitePin = 6;               // white LEDs connected to digital pin6

double blueFadeValue = 0;
double whiteFadeValue = 0;

int userWhiteMax =        60;   // maximum percentage intensity of white leds, 0-100 user defined
int userBlueMax =         40;   // maximum percentage intensity of blue leds, 0-100 user defined
double blueMaxPwm;                 // variable used to the userBlueMax value into a PWM value
double whiteMaxPwm;                // variable used to covert the userWhiteMax value into a PWM value

int hour;
int minute; 
int lightsOn =          9;       // time of day (hour, 24h clock) to begin lights fading in
int lightsOff =         20;      // time of day (hour, 24h clock) to begin lights fading out
int fadeTime =          90;      // amount of time in minutes for the fade in/out to last 

int counter = 0;                //counter for the PWM
 
void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
 
  if (!RTC.isrunning()) { //uncomment the ! to be able to reset the datetime value
    // this will allow you to set RTC to the current computer date and time if uncommented and compiled 
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }
 
}
/* analogWrite(bluePin, blueMin);
   analogWrite(whitePin, whiteMin);*/
   void loop () {
     maxFade();
     rtcSet();
     ledFade();
     SerialOut();
             
   }  
    
 
   void ledFade(){
   //this works with the led, use the rtc.now to pull the rtc values needed in the fade code
   if(counter < 60){ //specifies the rate of dimming, 60 = 60seconds a fade occurs
     counter++;
   }
   else{
   
   if (whiteFadeOn == 1){
     if(whiteFadeValue <= whiteMaxPwm) {
       analogWrite(whitePin, whiteFadeValue);
       whiteFadeValue = whiteFadeValue+(whiteMaxPwm/fadeTime); //increases the fade value per minute(counter) by the whiteMaxPwm / fadeTime entered by user
     }  
   }
     
   if (blueFadeOn == 1){
     if(blueFadeValue <= blueMaxPwm) {
       analogWrite(bluePin, blueFadeValue);
       blueFadeValue = blueFadeValue+(blueMaxPwm/fadeTime); //increases the fade value per minute(counter) by the blueMaxPwm / fadeTime entered by user 
     }  
   }
      
   if (blueFadeOff == 1){
     if(blueFadeValue >= 0) {
       analogWrite(bluePin, blueFadeValue);
       blueFadeValue = blueFadeValue-(blueMaxPwm/fadeTime); //decreases the fade value per minute(counter) by the blueMaxPwm / fadeTime entered by user
     }  
   }
   
   if (whiteFadeOff == 1){
     if(whiteFadeValue >= 0) {
       analogWrite(whitePin, whiteFadeValue);
       whiteFadeValue = whiteFadeValue-(whiteMaxPwm/fadeTime); //decreases the fade value per minute(counter) by the whiteMaxPwm / fadeTime entered by user 
     }  
   }
   
      
   counter = 0;
 }
 }
 
 
 //void rtcSet class to control the leds with a timer
   void rtcSet(){
   DateTime now = RTC.now();
   hour = now.hour();
   minute = now.minute();
   
   if((hour >= lightsOn)&&(hour < 12)){
  // if((hour == lightsOn)&&(minute == 0)){  //sets blue fade on to be on from 09:00 (lightsOn) until 12
     blueFadeOn = 1;  
  }
   if((hour >= lightsOn+1)&&(hour < 12)){ //sets white fade on for 09:30
     whiteFadeOn = 1;  
  }
   if((hour >= 12)&&(hour < lightsOff)){ //this resets the whiteFadeOn and blueFadeOn values to be 0 at noon 
     whiteFadeOn = 0;  
     blueFadeOn = 0;
     whiteFadeValue = whiteMaxPwm;
     blueFadeValue = blueMaxPwm;
     analogWrite(whitePin, whiteFadeValue);
     analogWrite(bluePin, blueFadeValue);
   }
   if((hour >= lightsOff)&&(hour < 24)){ //starts the white fade out for evening
     whiteFadeOff = 1;  
  }
   if((hour >= lightsOff+1)&&(hour < 24)){
     blueFadeOff = 1;  
  }
   if((hour >= 0)&&(hour < lightsOn)){  //this resets the whiteFadeOff and BlueFadeOff values to be 0 for a time between midnight and lightsOn
     whiteFadeOff = 0;
     blueFadeOff = 0;
     analogWrite(whitePin, 0);  //sets the light PWM value to be at 0 when the lights are supposed to be off in case of power outage
     analogWrite(bluePin, 0);
  }  
}
     
   
 
 //void maxFade class used to covert the user percentage input to PWM and set to white/blue  
   void maxFade(){
     blueMaxPwm = userBlueMax*2.55;  //converts the user input value (0-100) to a PWM value (0-255)
     whiteMaxPwm = userWhiteMax*2.55; //converts the user input value (0-100) to a PWM value (0-255)   
     
   }
   
 
 
 //void SerialOut class to output print statements to the serial lcd display, time, temp, and light prcnt
   void SerialOut(){ 
     DateTime now = RTC.now();
 
    Serial.print(counter);
    Serial.print(' '); 
 /*
    Serial.print(now.year(), DEC);
    Serial.print('-');
    
    if (int(now.month()) < 10) {
       Serial.print('0');
       Serial.print(now.month(), DEC);}
      else {Serial.print(now.month(), DEC);}
    
    Serial.print('-');
    
    if (int(now.day()) < 10) {
       Serial.print('0');
       Serial.print(now.day(), DEC);}
      else {Serial.print(now.day(), DEC);}
    
    Serial.print(' ');
    
    if (int(now.hour()) < 10) {
       Serial.print('0');
       Serial.print(now.hour(), DEC);}
      else {Serial.print(now.hour(), DEC);}
    
    Serial.print(':');

    if (int(now.minute()) < 10) {
       Serial.print('0');
       Serial.print(now.minute(), DEC);}
      else {Serial.print(now.minute(), DEC);}
      
    Serial.print(':');

    if (int(now.second()) < 10) {
       Serial.print('0');
       Serial.print(now.second(), DEC);}
      else { 
    Serial.print(now.second(), DEC);}
    */
    Serial.print(hour);
    Serial.print(':');
    Serial.print(minute);
    
    //outputs white percentage value to serial lcd
    Serial.print(" White:");
    Serial.print(whiteFadeValue/2.55);
    Serial.print("% In=");
    Serial.print(whiteFadeOn);
    Serial.print(" Out=");
    Serial.print(whiteFadeOff); 
        
    //outputs blue percentage value to serial lcd
    Serial.print(" Blue:");
    Serial.print(blueFadeValue/2.55);
    Serial.print("% In=");
    Serial.print(blueFadeOn);
    Serial.print(" Out=");
    Serial.print(blueFadeOff);
        
   /*output for the blue percent and white percent*/    
/*    Serial.print('blue: ');
    Serial.print(blue_prcnt);
    Serial.print('%');
    
    Serial.print('white: ');
    Serial.print(white_prcnt);
    Serial.print('%');*/

     Serial.println();
    delay(1000);
   }  //closes rtc

Does this:

 /*
    Serial.print(now.year(), DEC);
    Serial.print('-');
    
    if (int(now.month()) < 10) {
       Serial.print('0');
       Serial.print(now.month(), DEC);}
      else {Serial.print(now.month(), DEC);}
    
    Serial.print('-');
    
    if (int(now.day()) < 10) {
       Serial.print('0');
       Serial.print(now.day(), DEC);}
      else {Serial.print(now.day(), DEC);}
    
    Serial.print(' ');
    
    if (int(now.hour()) < 10) {
       Serial.print('0');
       Serial.print(now.hour(), DEC);}
      else {Serial.print(now.hour(), DEC);}
    
    Serial.print(':');

    if (int(now.minute()) < 10) {
       Serial.print('0');
       Serial.print(now.minute(), DEC);}
      else {Serial.print(now.minute(), DEC);}
      
    Serial.print(':');

    if (int(now.second()) < 10) {
       Serial.print('0');
       Serial.print(now.second(), DEC);}
      else { 
    Serial.print(now.second(), DEC);}
    */

have anything to do with your problem? If not, delete it. I don’t want to have to wade through a ton of commented out code to find where your problem might be.

The rtcSet() function does not set the RTC. That function needs a new name,

Fix that. Remove all the commented out code, and post again.

Thanks for the reply on this, I've actually been working on this for the past hour and re-wired it. No random output times at this point, I think it might have been due to some faulty connections to the ds1307

No random output times at this point, I think it might have been due to some faulty connections to the ds1307

Thanks for letting us know. Glad that you solved your problem.