[SOLVED] Stop relays from resetting on each loop of the program

I'm doing a reptile vivarium, and am suffering an irritation.

the code in its simple terms is,

check time, if time is right, turn on lamp (otherwise turn lamp off)
pause

but everytime the code loops around, all 4 of the relays turn off, pause a second and then turn on again, which will probably get annoying to the inhabitants.

if ((hour > lightsOn) && (hour < lightsOff)) {
  digitalWrite(Lamp1,LOW);  // Turns ON Relays 1
    Serial.print("Lamp1 On"); 
  digitalWrite(Lamp2,LOW);  // Turns ON Relays 3
    Serial.println("Lamp2 On");
  } else {
      digitalWrite(Lamp1,HIGH);        // Turns Relay Off
        Serial.print("Lamp1 Off");
      digitalWrite(Lamp2,HIGH);        // Turns Relay Off
        Serial.println("Lamp2 Off");
  }
  
if ((t1 < lowTemp) && (t1 < highTemp)) {
   digitalWrite(Heater1,LOW);          // Turns ON Relays 2
   Serial.print("Heater1 On");
    } else { 
        digitalWrite(Heater1,HIGH);    // Turns Relay Off
        Serial.print("Heater1 Off");
    }

if ((t2 < lowTemp) && (t2 < highTemp)) {
  digitalWrite(Heater2,LOW);           // Turns ON Relays 4
  Serial.println("Heater2 On");  
  } else {
     digitalWrite(Heater2,HIGH);         // Turns Relay Off      
     Serial.print("Heater2 Off");
     Serial.println();
  }

is the relay turn on / off code

and the full code is attached.

Vivarium4.ino (4.39 KB)

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Why two temp/RH sensors, two zones?
Check the two if statements that you test temp of each sensor , the use of t1 and t2.

Thanks..Tom...... :slight_smile:

hi,

ill knock up an image, ive just got it breadboarded atm.

there are 2 heat sensors as i have 2 vivariums on top of each other.

soo, 1 arduino (pro mini), 1 ds1307 rtc, 2 temp sensors, one in each vivarium, 1 4 relay module and this is connected to 1 heater and 1 lamp in each vivarium.

edit


it seems like i had a minor cabling error on the breadboard. i poked all the cables and then the relay clicking stopped...

thanks though :slight_smile:

Hi,
Good to see. :slight_smile:

Please don't edit your sketch in your original post as this now causes a discontinuity in the thread.

Check the two if statements that you test temp of each sensor , the use of t1 and t2.

You original posting had:

if ((t1 < lowTemp) && (t2 < highTemp)) {
   digitalWrite(Heater1,LOW);          // Turns ON Relays 2
   Serial.print("Heater1 On");
    } else { 
        digitalWrite(Heater1,HIGH);    // Turns Relay Off
        Serial.print("Heater1 Off");
    }

if ((t2 < lowTemp) && (t2 < highTemp)) {
  digitalWrite(Heater2,LOW);           // Turns ON Relays 4
  Serial.println("Heater2 On");  
  } else {
     digitalWrite(Heater2,HIGH);         // Turns Relay Off      
     Serial.print("Heater2 Off");
     Serial.println();
  }

I have pointed out a program error, which now does not exist in your original sketch

You should have re-posted your corrected sketch so others can see and learn.

Tom..... :slight_smile: