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:

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:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:  
      case 9:

Here’s the update codes.

int updatePhase() {
  if(hour() >= 6 && hour() < 7)
    return 0;
  if(hour() >= 7 && hour() < 8)
    return 1;
  if(hour() >= 8 && hour() < 11)
    return 2;
  if(hour() >= 11 && hour() < 14)
    return 3;
  if(hour() >= 14 && hour() < 17)
    return 4;
  if(hour() >= 17 && hour() < 19)
    return 5;
  if(hour() >= 19 && hour() < 20)
    return 6;
  if(hour() >= 20 && hour() < 23)
    return 7;
  if(hour() >= 23 || hour() < 2)
    return 8;
  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) {
    if (Serial2.available())
      SAL = Serial2.parseFloat();
    if (Serial3.available())
      PH = Serial3.parseFloat();

void accomidatePH() {

void updateDisplay() {
  if(millis() % 2000 == 0) {
    Serial.print(" ");
    Serial.print(" ");
    Serial.print(" ");

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:
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?