Program going dead

Hi all, I have been testing my program for a couple of days now and it seemed to work fine until I introduced a power outage to it. It is a reef controller that has time, temp, lighting, heat control, moon lighting, and 2 relays. The time, temp, relays, and heat are not affected by power outages. The moon lighting, and lighting are only affected by power outages after midnight. I lose the on-off display and also lose power. I have tried resetting the display and on the board. Once the clock reaches the photo period it works fine until a power outage after midnight. I will post the code.

/*

REEFCONTROLLER
Orginally by unknown source, modified by Jim H.

*/

#include <OneWire.h>
#include <DallasTemperature.h>
#include "Wire.h" 
#define DS1307_I2C_ADDRESS 0x68 //set rtc
#include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins
#define ONE_WIRE_BUS 8 //Define the pin of the DS18B20


/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  R E L A Y   P A R T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S I M P L E   O N   A N D   O F F   F E A T U R E |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



const int ledPin1 =  A2;          // pin number for relay 1 was digital pin 2
const int ledPin2 =  A3;          // digital pin 3


int ledState1 = LOW;
int ledState2 = LOW;
long previousMillis1 = 0;
long previousMillis2 = 0;
long interval1 = 15000;  // interval at which to blink (milliseconds) for RELAY1
long interval2 = 20000;

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L E D   D I M M I N G   P A R T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  F A D E S   I N   A N D   O U T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



int blueramptime = 0 ;     // time for blue LEDs to dim on and off in minutes
int whiteramptime = 0 ;    // time for white LEDs to dim on and off in minutes
int bluemin = 0 ;          // minimmum dimming value of blue LEDs, range of 0-255
int bluemax = 255 ;        // maximum dimming value of blue LEDs, range of 0-255
int whitemin = 0 ;         // minimum dimming value of white LEDs, range of 0-255
int whitemax = 255 ;       // maximum dimming value of white LEDs, range of 0-255
int photoperiod = 1 ;      // amount of time array is on at full power in minutes
int ontime = 12 ;          // time of day (hour, 24h clock) to begin photoperiod fade in
int blue = 3;              // blue LEDs connected to digital pin 3 (pwm)
int white = 11;            // white LEDs connected to digital pin 11 (pwm)
int heater = A0;           // heater relay , digital pin 0
int moon = A1;             // moon light relay connected to analog pin 1
int heater_on_temp = 78;   //Turn on heater at this temp
int heater_off_temp = 80;  //turn heater off once below this temp


int bluepercent[11] = { 0, 1, 2, 5, 8 ,12, 18, 27, 44, 80, 255 };   // this line is needed if you are using meanwell ELN60-48D
int whitepercent[11] = { 0, 1, 2, 5, 8 ,12, 18, 27, 44, 80, 255 };   // these are the values in 10% increments


LiquidCrystal lcd(12, 13, 4, 5, 6, 7);   // typically 8, 9, 4, 5, 6, 7
                                         // have to change to free up more pwm pins
// open pins 10,9,2,1,0,

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  R T C   C L O C K   D S 1 3 0 7  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



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


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

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
  Wire.send(decToBcd(minute));
  Wire.send(decToBcd(hour));   // If you want 12 hour am/pm you need to set
  // bit 6 (also need to change readDateDs1307)
  Wire.send(decToBcd(dayOfWeek));
  Wire.send(decToBcd(dayOfMonth));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second = bcdToDec(Wire.receive() & 0x7f);
  *minute = bcdToDec(Wire.receive());
  *hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
  *dayOfWeek = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month = bcdToDec(Wire.receive());
  *year = bcdToDec(Wire.receive());
}



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  O N E S E C O N D |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



void onesecond() //function that runs once per second while program is running
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  lcd.setCursor(0, 0);
  if(hour>0)
  {
    if(hour<=12)
    {
      lcd.print(hour, DEC);
    }
    else
    {
      lcd.print(hour-12, DEC);
    }
  }
  else
  {
    lcd.print("12");
  }
  lcd.print(":");
  if (minute < 10) {
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  //lcd.print(":");
  //if (second < 10) {
  //  lcd.print("0");
  //}
  //lcd.print(second, DEC);
  if(hour<12)
  {
    lcd.print("AM ");
  }
  else
  {
    lcd.print("PM ");
  }
  //lcd.print(" ");
 // delay(1000);
}


/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  H E A T E R    O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void HeaterOn()
{
  digitalWrite(heater, HIGH);  
  lcd.setCursor(8, 1);
  lcd.print("On");
  //analogWrite(fan, 255);
    
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  H E A T E R    O F F |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void HeaterOff()
{
  digitalWrite(heater, LOW);
  lcd.setCursor(8, 1);
  lcd.print("Off");
    //analogWrite(fan, 0);
    
}


/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  M O O N  L I G H T  O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void MoonOn()
{
  digitalWrite(moon, HIGH);  
  lcd.setCursor(13,1);
  lcd.print("ON ");
  
    
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  M O O N  L I G H T   O F F |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void MoonOff()
{
  digitalWrite(moon, LOW);
  lcd.setCursor(13,1);
  lcd.print("OFF");
    
    
}

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  R E L A Y 1 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 




void relay1()  //FUNCTION TO TURN ON AND OFF RELAY 1. 
{  
  unsigned long currentMillis = millis(); 

  if(currentMillis - previousMillis1 > interval1)  
  {  
    previousMillis1 = currentMillis;    
    if (ledState1 == LOW) 
      ledState1 = HIGH; 
    else 
      ledState1 = LOW; 
    digitalWrite(ledPin1, ledState1); 
  } 
} 

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  R E L A Y 2 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 




void relay2() 
{ 
  unsigned long currentMillis2 = millis(); 

  if(currentMillis2 - previousMillis2 > interval2)  
  { 
    previousMillis2 = currentMillis2;    
    if (ledState2 == LOW) 
      ledState2 = HIGH; 
    else 
      ledState2 = LOW; 
    digitalWrite(ledPin2, ledState2); 
  } 
} 




/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S E T U P  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 


void setup() { 
  pinMode(ledPin1, OUTPUT);     // set the digital pin as output:
  pinMode(ledPin2, OUTPUT);
  pinMode (heater, OUTPUT); 
  pinMode(moon, OUTPUT);       // Set analog pin 1 as a output 
  sensors.begin();             // Start up the DS18B20 Temp library

This is the second part of code.

 /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S E T U P - D I S P L A Y |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 



  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 
  Wire.begin(); 

  // Change these values to what you want to set your clock to. 
  // You probably only want to set your clock once and then remove 
  // the setDateDs1307 call. 
  second = 00; 
  minute = 55; 
  hour = 23; 
  dayOfWeek = 2;  // Sunday is 0 
  dayOfMonth = 3; 
  month = 1; 
  year = 12; 
   
  //Use the next line for setting the clock 
 // setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year); 

  analogWrite(blue, bluemin); 
  analogWrite(white, whitemin);  
  lcd.begin(16, 2); // set up the LCD's number of rows and columns:  
  lcd.setCursor(11, 1); 
  lcd.print("M:");  
  lcd.setCursor(0, 1); 
  lcd.print("L:");
  lcd.setCursor(6, 1);
  lcd.print("H:"); 
} 

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 

void loop() 
{ 
  onesecond();  
  relay1();
  relay2(); 


  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - D I M   F U N C T I O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); 
  int daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes 
       

  int bluerampup; 
     if (daybyminute >= (ontime*60))  
       bluerampup = (((ontime*60) + blueramptime) - daybyminute); 
     else 
       bluerampup = blueramptime; 
        
  int whiterampup; 
    if (daybyminute >= (ontime*60 + blueramptime))  
       whiterampup = (((ontime*60) + blueramptime + whiteramptime) - daybyminute); 
     else 
       whiterampup = whiteramptime; 

  int whiterampdown; 
    if (((ontime * 60) + photoperiod + blueramptime + whiteramptime) <= daybyminute) 
      whiterampdown = (((ontime*60) + photoperiod + blueramptime + 2*whiteramptime) - daybyminute); 
    else 
      whiterampdown = whiteramptime; 
       
  int bluerampdown; 
    if (((ontime * 60) + photoperiod + blueramptime + 2*whiteramptime) <= daybyminute) 
      bluerampdown = (((ontime*60) + photoperiod + 2*blueramptime + 2*whiteramptime) - daybyminute); 
    else 
      bluerampdown = blueramptime; 
        /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - F A D E  I N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 


 if (daybyminute >= (ontime*60)) 
  {  
    if (daybyminute <= ((ontime*60) + blueramptime + (whiteramptime/10*9))) //if time is in range of fade in, start fading in + (whiteramptime/10*9) 
    { 
      
      MoonOff();  
      for (int i = 1; i <= 10; i++) // setting ib value for 10% increment. Start with 0%  
      {  
        analogWrite(blue, bluepercent[i]);  
       
        int countdown = ((bluerampup*60)/10); // calculates seconds to next step 
        while (countdown>0) 
          { 
          onesecond(); // updates clock once per second 
          countdown--;  
          relay1();
          relay2(); 
        } 
      }       

      // fade white LEDs in from min to max. 
      for (int i = 1; i <= 10; i++) // setting i value for 10% increment. Start with 0% 
      {  
        analogWrite(white, whitepercent[i]);  
        lcd.setCursor(2, 1);  
        lcd.print("ON ");  

        int countdown = ((whiterampup*60)/10); // calculates seconds to next step 
        while (countdown>0) 
        { 
          onesecond(); // updates clock once per second 
          countdown--;  
          relay1();
          relay2(); 
        } 
      }  
    } 
  } 


  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - M A X  V A L U E |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 



 if (daybyminute >= ((ontime * 60) + blueramptime + whiteramptime))  
  {  
    if ( daybyminute < ((ontime * 60) + blueramptime + whiteramptime + photoperiod)) // if time is in range of photoperiod, turn lights on to maximum fade value 
    { 
     
      MoonOff(); 
             
      analogWrite(blue, 255);        
      analogWrite(white, 255);  
        lcd.setCursor(2, 1);  
        lcd.print("ON ");  
       
    }  
  } 

  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - F A D E  O U T |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 

  if (((ontime * 60) + photoperiod + blueramptime + whiteramptime) <= daybyminute) 
  {  
    if (((ontime * 60) + photoperiod + whiteramptime + 2*blueramptime + (blueramptime/10*9)) >= daybyminute) 
    { 
     
      MoonOff(); 
      
       
      // fade white LEDs out from max to min in increments of 1 point: 
      for (int i = 10; i >= 0; i--) // setting i value for 10% increment. Start with 10% 
      {  
        analogWrite(blue, 255); 
       
         
        analogWrite(white, whitepercent[i]);  
        lcd.setCursor(2, 1); 
        lcd.print("OFF ");   

        int countdown = ((whiterampdown*60)/10); // calculates seconds to next step 
        while (countdown>0) 
        { 
          onesecond(); // updates clock once per second 
          countdown--;  
          relay1();
          relay2(); 
        } 

      }  

      // fade blue LEDs out from max to min in increments of 1 point: 
      for (int i = 10; i >= 0; i--) // setting i value for 10% increment. Start with 10% 
      {  
        analogWrite(blue, bluepercent[i]); 
        int countdown = ((bluerampdown*60)/10); // calculates seconds to next step 
        while (countdown>0) 
        { 
          onesecond(); // updates clock once per second 
          countdown--;  
          relay1(); 
          relay2();
        } 
      } 
    } 
  } 

// DS18B20 display 
sensors.requestTemperatures(); // Send the command to get temperatures 
    // set the cursor to column 0, line 1 
  // (note: line 1 is the second row, since counting begins with 0): 
 // 
 
  lcd.setCursor(9, 0); 
  lcd.print(sensors.getTempFByIndex(0));  
  lcd.print((char)223); 
  lcd.print("F   "); 
  lcd.setCursor(8, 1);
 // lcd.print(sensors.getTempFByIndex(1);
  if (sensors.getTempFByIndex(0) < heater_on_temp)  HeaterOn();  
  if (sensors.getTempFByIndex(0) > heater_off_temp)  HeaterOff(); 
      
  delay(1000); 
  //*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  Night Time |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 

if (((ontime * 60) + photoperiod + (2 * blueramptime) + (2 * whiteramptime)) < daybyminute) 
  {           
    MoonOn(); 
    // analogWrite(white, whitepercent[i]);  
        lcd.setCursor(2, 1); 
        lcd.print("OFF "); 
  }  
     
 if (daybyminute < (ontime*60)) 
{    
} 
   
}  // END LOOP

Is there a recurring power outage or is the board supposed to shut itself down ? The code is quite large, can you explain what connection you think exists between this "power outage" and the code ?

The power outage is simulated by me. I am looking for the program (since its operates off the rtc) to come back on and resume the program based off the rtc. The heater works and is based off the temperature part of the program. The program starts in am and works, will run through pm and work, but when it hits 12:00am and there is a simulated power outage it does until it comes back to on-time part of the program.

but when it hits 12:00am and there is a simulated power outage it does until it comes back to on-time part of the program.

Sorry, but I don't understand this sentence.

Do you simulate the power outage always at 12:00 ? If so, why ? I'm puzzled...

I suggest you look at the Time library

http://arduino.cc/playground/Code/Time

I'll try to get a closer look at your code asap...

Do you simulate the power outage always at 12:00 ? If so, why ? I'm puzzled...

I wondered about that too. Are you saying that a power outage before midnight is ok, but not after?

I wasn't too clear about this. I am simulating power outages by disconnecting and then applying power due to the fact its a reef tank and I need the program to come back on once there is power again. After midnight if power is lost, when the program restarts the moon lights will not function again until the program runs through its photo period, which would be the following evening. If the moon lights come on at say 7pm and shut off the following day at 10 am, if for any reason I lose power, (say power interrupted at 4am and comes back on at 5am) I want the program to energize the moon light to continue the rest of the program.

Then there's probably a flaw in how the program deals with time. It should activate and deactivate things not by looking at how much time has passed since boot, but by looking at what the time of day is. This is one thing I'd look for...

HTH