Arduino gets stuck at randoms times ??

Hi all I am new to the forum,

Now to my problem. I am controlling a fish tank with my arduino. It is my first time using an arduino so the code isnt going to be the most efficient. The problem i am getting is the loop seems to get stuck at random intervals. Sometimes it 5hrs some times it is 1.5 days.

Hardware.
Arduino Duemilanove
4 port relay board (from ebay) controlling heaters, fans and Auto top off for water evaporation
3 DS18B20 thermistors
real time clock
and some leds (180W worth with PWN inputs and constant current drivers)
oh and a float switch

Now i thought it might of been an input power supply problem as i read something about the input power in the arduino could make it over heat ?? So i changed from my original 12v 500ma to a 9v 800ma and there didnt seem to be a difference, if anything i think the time between freezing got shorter (worse). So i changed to a nokia phone charger 5v 350ma and that seems to have helped a little… maybe. Is there something that I am doing wrong with the hardware or is it in my code somewhere ?

Any help would be great
Thanks

#include <OneWire.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <WProgram.h>
#include <DS1307.h> 



OneWire ds(10);                        // DS18S20 TEMPERATURE PIN SELECTION
LiquidCrystal_I2C lcd(0x27,20,4);      // ADRESS, ROWS, COLOUMS FOR THE LCD SCREEN

float c2f(float cel) {
return (cel * (9.0/5.0)) + (float)3200;
}

/*      WHAT PINS ARE WHAT ??

1  =  WHO KNOWS WHAT TX IS ???
2  =  FAN - RELAY PIN
3  =  HEATER - RELAY PIN
4  =  FLOAT SWITCH - RELAY PIN
5  =  RELAY PIN
6
7  =  FLOAT SWITCH
8
9  =  BLUE LEDS STRINGS
10  = DALLAS TEMP SENSORS
11  = WHITE LED STRINGS
12
13

A1
A2
A3
A4  =  SDA - LCD AND RTC
A5  =  SCL - LCD AND RTC


*/


int relay_2 = 2;                // REALY BLOCK ONE - WATER LEVEL HEATERS AND FAN AND ONE EXTRA
int relay_3 = 3;
int relay_4 = 4;
int relay_5 = 5;

int floatswitch = 7;             // FLOAT SWITCH PIN

int lowlv;                       // VARIABLE FOR READING THE PIN FOR WATER LEVEL (HIGH OR LOW)

float temp_C;                    // TEMP VARIABLE THAT GOES INTO THE 15 PIECE ARRAY FOR AVERAGING.
int Temp_dallas;

float ross = 0;                  // SIMPLE 3 POINT AVERAGE VARIABLES
float ross1 =0;

int index = 0;                   // COUNTING INTERGERS
int k = 1;
int j = 1;
int l = 0;
int m = 0;
float n = 0;


float Av= 0;                     // VARIABLES FOR TEMPERATURE ARRAY
float avg = 0;
float input[15];
float total_avg = 0;


int hour = 0;                    // SETTING VARIABLES TO ATTACH TO THE DS1307 CLOCK
int minutes = 0;
int second = 0;
int date = 0;
int month = 0;
int year = 0;
int mil_time = 0;

int blue = 9;                    // PIN NUMBER FOR BLUE STRINGS
int blue_in = 610;              // START TO FADE IN
int blue_on = 640;              // FULL POWER
int blue_out = 1800;             // START TO FADE OUT
int blue_off = 1830;             // OFF

int white = 11;                  // PIN NUMBER FOR WHITE STRINGS
int white_in  = 625;
int white_on = 655;
int white_out = 1725;
int white_off = 1755;

int fade_inc = 600;            // NUMBER OF INCREMENTS FOR 10 MINUTES - THE TIME FOR FADING IN AND OUT (WORKING USING THE DIFFERENCE BETWEEN IN AND ON AND OUT AND OFF ON THE COLOURS
int fade_max = 255;
int fade_min = 1;

int percentage_fade = 0;       // TO GET THE fade_inc INTO THE 255 FORM FOR DIMMING THE LEDS
int percent = 0;               // A SCALE OF ABOVE FROM 0 TO 100 FOR HUMAN READING (EASE OF READING A NORMAL PERCENTAGE)
int percentage_fade_white = 0;
int percent_white = 0;

int fan_1 = 0;                 // THE WATER FANS / WAVEMAKERS IN THE TANK - NUMBER FROM LEFT TO RIGHT ON RELAY BLOCK ??
int fan_2 = 0;
int fan_3 = 0;



void setup() {
/* // UNCOMMENT TO SET THE TIME ON THE DS1307 CLOCK
  RTC.stop();
  RTC.set(DS1307_SEC,00);        //set the seconds
  RTC.set(DS1307_MIN,30);       //set the minutes
  RTC.set(DS1307_HR,07);        //set the hours
  RTC.set(DS1307_DOW,6);        //set the day of the week
  RTC.set(DS1307_DATE,28);      //set the date
  RTC.set(DS1307_MTH,03);       //set the month
  RTC.set(DS1307_YR,12);        //set the year
  RTC.start();
*/


  // initialize inputs/outputs
  // start serial port
  Serial.begin(9600);
  
  //  *** INPUT PINS ***
  
   pinMode(floatswitch, INPUT);   // FLOAT SWITCH INPUT
   pinMode(10, INPUT);            // DALLAS TEMP INPUT
  
  //  *** OUTPUT PINS ***
  
   pinMode(1, OUTPUT);            //  FREE ATM
   pinMode(relay_2, OUTPUT);      // RELAY PINS 
   pinMode(relay_3, OUTPUT);      // RELAY PINS
   pinMode(relay_4, OUTPUT);      // RELAY PINS
   pinMode(relay_5, OUTPUT);      // RELAY PINS 
     
   //pinMode(13, OUTPUT);         // Set the LED pin as output
   
   
   //  *** PINS INITIAL STATES ***

   digitalWrite(relay_2, HIGH);   // STARTING VALUES
   digitalWrite(relay_3, HIGH);
   digitalWrite(relay_4, HIGH);
   digitalWrite(relay_5, HIGH);
   
  
   lcd.init();                    // INITILISE THE LCD 
   lcd.backlight();
   lcd.clear();                   // CLEAR THE LCD
}
void loop(){
  
  //_____________________________________________________________________
  
  Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
  Serial.print(":");
  Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
  Serial.print(":");
  Serial.print(RTC.get(DS1307_SEC,false));//read seconds
  Serial.print("      ");                 // some space for a more happy life
  Serial.print(RTC.get(DS1307_DATE,false));//read date
  Serial.print("/");
  Serial.print(RTC.get(DS1307_MTH,false));//read month
  Serial.print("/");
  Serial.print(RTC.get(DS1307_YR,false)); //read year 
  Serial.println();
  
 hour     = RTC.get(DS1307_HR,true); // This is in military time [0,23]
 minutes  = RTC.get(DS1307_MIN,false);
 second   = RTC.get(DS1307_SEC,false);
 date     = RTC.get(DS1307_DATE,false);
 month    = RTC.get(DS1307_MTH,false);
 year     = RTC.get(DS1307_YR,false);
 mil_time = hour*100 + minutes;
 
 //Serial.print("military time: ");
// Serial.println(mil_time);


//  lcd.setCursor(0,3);
//  
//  lcd.print(hour);
//  lcd.print(":");
//  lcd.print(minutes);
//  lcd.print(":");
//  lcd.print(second);
  
   if (mil_time < 1000)
 {
  lcd.setCursor(0,3);
  lcd.print("0");
  lcd.print(mil_time);
 }

else
 {
  lcd.setCursor(0,3);
  lcd.print(mil_time);
 }
  
  
  //_____________________________________________________________________
  
  
  
  
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
  int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract, ledPin;
  
  if ( !ds.search(addr)) {
            ds.reset_search();
      return;
  }
  for( i = 0; i < 8; i++) {
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n");
      return;
  }
  if ( addr[0] != 0x28) {
      Serial.print("Device is not a DS18S20 family device.\n");
      return;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end

  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.

  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
  }

   LowByte = data[0];
  HighByte = data[1];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  Tc_100 = c2f(Tc_100);
  
  Whole = (Tc_100 / 100);  // separate off the whole and fractional portions

  Fract = (Tc_100 % 100);

  temp_C = ((float(Whole) + (float)Fract/100) -32)*5/9;     // THIS IS THE USED TEMP THAT IS CALLED INTO THE FUNCTION TO FIND AVERAGES
   
  
 //__________________________ LCD DISPLAY EACH INDIVIDUAL TEMP
  
  
  k = (k + 1)%3;                    

  
  //  if (k >= 3)             // AT K = 3 AT THE END OF THE ARRAY SO USE K = 0 TO START THE COUNT AGAIN
  //  k = 0;                  // WRAP AROUND TO K = 0 AGAIN
  //Serial.print("K itern: ");
  //Serial.println((k));
  

 switch (k) 
 {
  case 0:                    // 
   lcd.setCursor(14,0);
   lcd.print("3:");
   lcd.print(temp_C,1);
  break;
   
   case 1:                    // your hand is on the sensor
   lcd.setCursor(7,0);
   lcd.print("2:");
   lcd.print(temp_C,1);
  break;
  
  case 2:    // your hand is close to the sensor
   lcd.setCursor(0,0);
   lcd.print("1:");
   lcd.print(temp_C,1);
  break;
 } 

//if (k == 1)
//{
//  lcd.setCursor(0,0);
//  lcd.print("1:");
//  lcd.print(temp_C,1);
//}
//else if (k == 2)
//{
//  lcd.setCursor(7,0);
//  lcd.print("2:");
//  lcd.print(temp_C,1);
//}
//else
//{
//  lcd.setCursor(14,0);
//  lcd.print("3:");
//  lcd.print(temp_C,1);
//}

  
 //_____________________________ FINDING AND DISPLAYING THE AVERAGE TEMP THROUGH AND ARRAY (ROSS'S CODE COMMENTED OUT)
 
 
  Av = Av - input[index];

  input[index] = temp_C;
  
  //Serial.println(input[index]);
  
  //Serial.println(Av);
  
  Av = input[index] + Av;
  
  //Serial.println(index);
  //Serial.println(Av);
  avg = Av / 15;

  //Serial.print("Average C: ");     
  //Serial.println(avg);   
  lcd.setCursor(0,1);
  lcd.print("TEMP:");
  lcd.print(avg,1);
  
  index = (index + 1)%15;
  
  // Ross's 3 point Average                      3 POINT AVERAGE
  //ross=ross1;
  //ross1 = (temp_C + ross + ross1)/3.0;
  //Serial.print("Ross C: ");     
  //Serial.println(ross1);   
  //total_avg = (ross1 + avg)/2.0;
  //Serial.print("Combined: ");     
  //Serial.println(total_avg); 
 
  //if (index >= 15)              
    // ...wrap around to the beginning: 
    //index = 0;                           

  
 //______________________________________________________ TEMP AND FAN RELAYS - WANT TO WORK BETWEEN 24 TO 26 DEGREES C
 
 
if (avg < 25)
{
  digitalWrite(relay_3, LOW);
 
 //Serial.print("H20 lv: ");     
 //Serial.println(lowlv);   
  lcd.setCursor(0,2);
  lcd.print("HEATER:ON ");
}

else
{
  digitalWrite(relay_3, HIGH);
 //Serial.print("H20 lv: ");     
 //Serial.println(lowlv);   
  lcd.setCursor(0,2);
  lcd.print("HEATER:OFF");
}
 
 if (avg > 26)
{
  digitalWrite(relay_2, LOW);
 //Serial.print("H20 lv: ");     
 //Serial.println(lowlv);   
  lcd.setCursor(11,2);
  lcd.print("FAN:ON ");
}

else
{
  digitalWrite(relay_2, HIGH);
 //Serial.print("H20 lv: ");     
 //Serial.println(lowlv);   
  lcd.setCursor(11,2);
  lcd.print("FAN:OFF");
}
   
 //_______________________________________________________ FLOAT SWITCH RELAY 
  
  lowlv = digitalRead(floatswitch);   // read input value and store it in lowlv
  
  
 if (lowlv == LOW)
{
  digitalWrite(relay_4, HIGH);
  
 //Serial.print("H20 lv: ");     
 //Serial.println(lowlv);   
  lcd.setCursor(11,1);
  lcd.print("H2O:NORM");
 }

else
{
  digitalWrite(relay_4, LOW);

 //Serial.print("H20 lv: ");     
 //Serial.println(lowlv);   
  lcd.setCursor(11,1);
  lcd.print("H2O: LOW");
}

 //_______________________________________________________________ LED DIMMIG FOR BLUE STRINGS
 
if (mil_time >= blue_in && mil_time < blue_on)
  {
      // fade blue LEDs in from min to max.
       percent = ((mil_time - blue_in)*(10/3));
       percentage_fade = percent * 2.55;
      
       analogWrite(blue, percentage_fade); 
       
        lcd.setCursor(5,3);
        lcd.print("B:");
        lcd.print(percent);
        lcd.print("%");
        //lcd.print(percentage_fade);
  }
   else if (mil_time >= blue_on && mil_time < blue_out)
  {
   //FULL STRENGTH MID DAY SUN :)
      
       digitalWrite(blue, HIGH); 
       lcd.setCursor(5,3);
       lcd.print("B:100%");
        
  }
     else if (mil_time >= blue_out && mil_time < blue_off)
  {      // fade blue LEDs in from min to max.
       percent = (blue_off - mil_time)*(10/3);
       percentage_fade = percent * 2.55;
      
       analogWrite(blue, percentage_fade); 
       
        lcd.setCursor(5,3);
        lcd.print("B:");
        lcd.print(percent);
        lcd.print("%");
        //lcd.print(percentage_fade);
   } 
   
   else 
   {
     //     SET EVERY OTHER TIME (NIGHT TIME) TO LOW - SHOULD BE ABLE TO CODE IN A MOON THING HERE MAYBE ?? SAVE IT FOR LATER
     analogWrite(blue, 10); 
     lcd.setCursor(5,3);
     lcd.print("B:10  %");
    }

  //_______________________________________________________________ LED DIMMIG FOR BLUE STRINGS
 
if (mil_time >= white_in && mil_time < white_on)
  {
      // fade blue LEDs in from min to max.
       percent_white = (mil_time - white_in)*(10/3);
       percentage_fade_white = percent_white * 2.55;
      
       analogWrite(white, percentage_fade_white); 
       
        lcd.setCursor(11,3);
        lcd.print("W:");
        lcd.print(percent_white);
        lcd.print("%");
        
       
  }
   else if (mil_time >= white_on && mil_time < white_out)
  {
   //FULL STRENGTH MID DAY SUN :)
      
       digitalWrite(white, HIGH); 
       lcd.setCursor(11,3);
       lcd.print("W:100%");
        
  }
     else if (mil_time >= white_out && mil_time < white_off)
  {      // fade blue LEDs in from min to max.
       percent_white = (white_off - mil_time)*(10/3);
       percentage_fade_white = percent_white * 2.55;
      
       analogWrite(white, percentage_fade_white); 
       
        lcd.setCursor(11,3);
        lcd.print("W:");
        lcd.print(percent_white);
        lcd.print("%");
   } 
   
   else 
   {
     //     SET EVERY OTHER TIME (NIGHT TIME) TO LOW - SHOULD BE ABLE TO CODE IN A MOON THING HERE MAYBE ?? SAVE IT FOR LATER
     digitalWrite(white, LOW); 
     lcd.setCursor(11,3);
     lcd.print("W:00%");
    }
 
 delay(3000);
  Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
  Serial.print(":");
  Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
  Serial.print(":");
  Serial.print(RTC.get(DS1307_SEC,false));//read seconds
  Serial.print("      ");                 // some space for a more happy life
  Serial.print(RTC.get(DS1307_DATE,false));//read date
  Serial.print("/");
  Serial.print(RTC.get(DS1307_MTH,false));//read month
  Serial.print("/");
  Serial.print(RTC.get(DS1307_YR,false)); //read year 
  Serial.println();

Read and print a bunch of time values.

 hour     = RTC.get(DS1307_HR,true); // This is in military time [0,23]
 minutes  = RTC.get(DS1307_MIN,false);
 second   = RTC.get(DS1307_SEC,false);
 date     = RTC.get(DS1307_DATE,false);
 month    = RTC.get(DS1307_MTH,false);
 year     = RTC.get(DS1307_YR,false);

Read and store the same time values.

Why? Read and store first, then print what you stored.

  for( i = 0; i < 8; i++) {
  }

Useful!

Tools + Auto Format will make us all a lot happier. Deleting commented out code that is clearly not part of the problem would be nice, too.

Creating some functions would be good. The loop() function is far too long.

So cutting some of the functions out of the main loop and calling them back into the main loop would help??

Just went through and cleaned out all of the old comments and such. Sorry about that.

So cutting some of the functions out of the main loop and calling them back into the main loop would help??

How? Moving some of the code into functions, debugging those functions, and then calling the function from loop() would help make the code easier to understand.