Code not working properly when combined.

So, my project so far is to develop an environmental control system for a reef aquarium. I’ve seen several people doing this, so I figured I’d give it a shot. I currently running this hardware:

Arduino Mega
Lighting system with 10 different time phases to simulate a natural day
WaveMaker (Simple switching a powerhead on and off)
8 channel relay power strip
4 channel relay auto doser
1 Atlas Scientific PH probe
1 float switch
1 thermometer (DS18B20)

I can write code that will run everything perfectly, but when I combine them together, I get some weird issues. I’m trying to run each function in thread style. I have functions within the loop that “updates” all these as the loop runs through, so every iteration, it gets a new PH reading, it compares to the timer if the wavemaker or doser needs to fire. I’m using this library to run my LEDs:
http://lithne.id.tue.nl/docs/LED/annotated.html

My issues seem to be coming a lot from whenever I try to utilize a relay. The wavemaker and dosers tend to either not always switch when they’re suppose to or the dosers have even reset the arduino board it seems. I feel like I might be overdriving the Arduino board… Maybe my codes not efficient enough? Thoughts? If you want to see more of the code, I’m more than willing to post a dropbox link to it or something.

Thanks in advance

Main Loop Code:

void loop() {
  //This assists in converting time numbers to corresponding strings.
  tmElements_t tm;
  
  phase = updatePhase();
  
  if(!LEDsAnimating()) {
    switch (phase) {
      case 0:
        beginDawn();
        break;
      case 1:
        beginSunrise();
        break;
      case 2:
        beginMorning();
        break;
      case 3:
        beginMidday();
        break;
      case 4:
        beginAfternoon();
        break;
      case 5:
        beginEvening();
        break;
      case 6:
        beginSunset();
        break;
      case 7:
        beginDusk();
        break;
      case 8:  
        beginMidnight();
        break;
      case 9:
        beginDarkness();
        break;
    }
  }
  
  updateLEDs();
  //updateWave();
  updateSensor();
  accomidatePH();
  //updateDoser();
  //updateDisplay();
      
}

Here’s the update codes.

int updatePhase() {
  //Dawn
  if(hour() >= 6 && hour() < 7)
    return 0;
  //Sunrise
  if(hour() >= 7 && hour() < 8)
    return 1;
  //Morning
  if(hour() >= 8 && hour() < 11)
    return 2;
  //Midday
  if(hour() >= 11 && hour() < 14)
    return 3;
  //Afternoon
  if(hour() >= 14 && hour() < 17)
    return 4;
  //Evening
  if(hour() >= 17 && hour() < 19)
    return 5;
  //Sunset
  if(hour() >= 19 && hour() < 20)
    return 6;
  //Dusk
  if(hour() >= 20 && hour() < 23)
    return 7;
  //Midnight
  if(hour() >= 23 || hour() < 2)
    return 8;
  //Darkness
  if(hour() >= 2 && hour() < 6)
    return 9;
  
}

void updateWave() {
  if(millis() % 2000 == 0) {
    int state = digitalRead(waveMaker);
    digitalWrite(waveMaker, !state);
  }
}

void updateSensor() {
  if(millis() % 2000 == 0) {
    Serial2.print("r/r");
    if (Serial2.available())
      SAL = Serial2.parseFloat();
    
    Serial3.print("r/r");
    if (Serial3.available())
      PH = Serial3.parseFloat();
  }  
}

void accomidatePH() {
}

void updateDisplay() {
  if(millis() % 2000 == 0) {
    Serial.print(month());
    Serial.print("/");
    Serial.print(day());
    Serial.print("/");
    Serial.print(year());
    Serial.print(" ");
    Serial.print(hour());
    Serial.print(":");
    Serial.print(minute());
    Serial.print(":");
    Serial.print(second());
    Serial.print(" ");
    Serial.print("Phase:");
    Serial.print(phase);
    Serial.print(" ");
    Serial.print("PH:");
    Serial.println(PH);
  }
}

void updateDoser() {
  if(hour() == 2 && minute() == 52 && second() == 0) {
    digitalWrite(Iodine, HIGH);
    tI = millis();
    Iod = true;
  }
  if(Iod && millis()-tI > 700) {
    digitalWrite(Iodine, LOW);
    Iod = false;
  }
}

Have you got snubber diodes on the relay coils?

I'm not the most hardware savy when it comes to this, so sorry if I say something stupid. I'm using this relay:
http://www.ebay.com/itm/8-Channel-5V-Relay-Module-Board-Shield-for-Arduino-PIC-AVR-MCU-DSP-ARM-/390594938127?pt=LH_DefaultDomain_0&hash=item5af145c50f
and a 4 channel equivalent for the dosers. They look like they have diodes on them, so I'm assuming that's the snubber diodes you mentioned. One of my thoughts is that these were part of the problem. I've been wanting the switch to SS Relays anyway. I'd like to know though if that is the issue regardless of if it really is something with my code.

f it really is something with my code.

That's more difficult to tell because only you can see your code.

Sorry for the dropbox link, but the code's a little longer than a post will allow. Feel free to check it out though:

Thanks again

The symptoms suggest you have an electrical problem. I suggest you try disconnecting the loads from the relays and see whether that hides the symptoms. If not, also disconnect the relay driver from the Arduino and see whether that hides them. If you have disconnected all of that and still get the problem, it's either a software issue or caused by some other hardware you have attached. In that case somebody needs to take a closer look at the hardware circuit, power supplies etc and the software.

I have a 12v 8a power supply mainly being used for the lights. It also powers the dosing pumps. The relay boards themselves get their 5v supply from the Arduino however as well as the thermometer and the float switch. And everything worked fine until the relay switches for the dosers was added, although I think looking back I saw some symptoms when I started toggling the relay for the wavemaker on the 8 channel. It would occasionally miss when it was suppose to switch. Would rerouting that 5v supply to perhaps my 12V PSU with a step down work? I've seen some people talking about simply adding a resistor to reduce 12v to 5v... I'm assuming a 2.5 ohm will work?