Relays toggle with every second on half of the triggers, why?

I have spent some time reading the web and have compiled a nice code. It is for a garden irrigation system. The system will include 3 zones that will run twice daily. The issue i’m having is the first 3 times (AM) cause the relay to toggle every second. The second three (PM) work the way I want. I have tried commenting the lcd.clear out and nothing seem to help. Any assistance from the community is appreciated.

UNO → RTC-> 1602 (i2c) → 4-channel relay

Video Of Error

//http://hack.lenotta.com
//
#include <Wire.h>
#include "RTClib.h"
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
RTC_DS1307 RTC;

#define DS3231_I2C_ADDRESS 0x68 // Convert normal decimal numbers to binary coded decimal
#define I2C_ADDR    0x3F // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7


LiquidCrystal_I2C	lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);



//Set Relay Pins
int Zone1 = 5;
int Zone2 = 6;
int Zone3 = 7;



void setup () {
  
      lcd.begin (16,2); //  <<----- My LCD was 16x2
      lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
      lcd.setBacklight(HIGH);
      lcd.home (); // go home


  //Initialize the serial port, wire library and RTC module
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
    pinMode(Zone1, OUTPUT);
    pinMode(Zone2, OUTPUT);
    pinMode(Zone3, OUTPUT);
    //IF YOU USE RTC FOR THE FIRST TIME: If we remove the comment from the following line,
    // we will set up the module time and date with the computer one
    //RTC.adjust(DateTime(__DATE__, __TIME__));
}

void loop () {
    DateTime now = RTC.now();
    //We print the day
    Serial.print(now.day(), DEC);
    Serial.print('/');
    //We print the month
    Serial.print(now.month(), DEC);
    Serial.print('/');
    //We print the year
    Serial.print(now.year(), DEC);
    Serial.print(' ');
    //We print the hour
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    //We print the minutes
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    //We print the seconds
    Serial.print(now.second(), DEC);
    Serial.println();
    //We check the time and sent through the serial port every 3s
    
    
    
    
    
    
    // DateTime now = RTC.now();
    
    lcd.setCursor(0,0);
    //We print the month
    lcd.print(now.month(), DEC);
    lcd.print('/');
    //We print the day
    lcd.print(now.day(), DEC);
    lcd.print('/');

    //We print the year
    lcd.print(now.year(), DEC);
    //lcd.print(' ');
    lcd.setCursor(0,1);
    
     //We print the hour
    lcd.print(now.hour(), DEC);
    lcd.print(':');
    //We print the minutes
    lcd.print(now.minute(), DEC);
    lcd.print(':');
    //We print the seconds
    lcd.print(now.second(), DEC);
   
    
  
    
    
    
    
    
    
   
   
   
 // Setup For Zone 1 AM
    
    if(now.day() % 2 >= 0 ){
      if(now.hour() == 17){
       if(now.minute() >= 44 && now.minute() <= 50)
       {
          digitalWrite(Zone1, LOW);
          Serial.print("Relay On");
          lcd.setCursor(11,0);
          lcd.print("Z1 ON");
        }
        else{
          digitalWrite(Zone1, HIGH);
           Serial.print("Relay Off");
        }
      }
    }
  
    
    
    
    
 //Setup For Zone 2 AM
 
    if(now.day() % 2 >= 0){
      if(now.hour() == 17){
       if(now.minute() >= 37 && now.minute() <= 37)
       {
          digitalWrite(Zone2, LOW);
          Serial.print("Relay On");
          lcd.setCursor(11,0);
          lcd.print("Z2 ON");
        }
        else{
          digitalWrite(Zone2, HIGH);
           Serial.print("Relay Off");
        }
      }
      }
    
       
       
      
      
 //Setup For Zone 3 AM
 
      if(now.day() % 2 >= 0){
      if(now.hour() == 17){
       if(now.minute() >= 38 && now.minute() <= 38)
       {
          digitalWrite(Zone3, LOW);
          Serial.print("Relay On");
          lcd.setCursor(11,0);
          lcd.print("Z3 ON");
        }
        else{
          digitalWrite(Zone3, HIGH);
           Serial.print("Relay Off");
        }
     
      }
      }
        //Set Time for 1 second after relay goes off
       if(now.day() % 2 >= 0){
      if(now.hour() == 17){
       if(now.minute() ==39)
       if(now.second() ==01)
       {
         Serial.print("LCD Clear");
          lcd.clear();
          }
      }
       }
          
          
          
          
       // Setup For Zone 1 PM
    
    if(now.day() % 2 >= 0){
      if(now.hour() == 17){
       if(now.minute() >= 40 && now.minute() <= 40)
       {
          digitalWrite(Zone1, LOW);
          Serial.print("Relay On");
          lcd.setCursor(11,0);
          lcd.print("Z1 ON");
          lcd.setCursor(11,1);
           lcd.print("    ");
        }
        else{
          digitalWrite(Zone1, HIGH);
           Serial.print("Relay Off");
        }
      }
    }
    
    
    
 //Setup For Zone 2 PM
 
    if(now.day() % 2 >= 0){
      if(now.hour() == 17){
       if(now.minute() >= 41 && now.minute() <= 41)
       {
          digitalWrite(Zone2, LOW);
          Serial.print("Relay On");
          lcd.setCursor(11,0);
          lcd.print("Z2 ON");
   
        }
        else{
          digitalWrite(Zone2, HIGH);
           Serial.print("Relay Off");
        }
      }
      }
 
      
      
      
 //Setup For Zone 3 PM
 
      if(now.day() % 2 >= 0){
      if(now.hour() == 17){
       if(now.minute() >= 42 && now.minute() <= 42)
       {
          digitalWrite(Zone3, LOW);
          Serial.print("Relay On");
          lcd.setCursor(11,0);
          lcd.print("Z3 ON");
        }
        else{
          digitalWrite(Zone3, HIGH);
           Serial.print("Relay Off");
        }
      }
      }
      //Set Time for 1 second after relay goes off 
      if(now.day() % 2 >= 0){
    if(now.hour() == 17){
       if(now.minute() ==43)
      if(now.second() ==01)
       {
         Serial.print("LCD Clear");
         lcd.clear();
        
      }
      }
       }
     
     

  
    delay(1000);
}

Same code with the useless whitespace removed and formatted with ctrl-T:

//http://hack.lenotta.com
//
#include <Wire.h>
#include "RTClib.h"
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
RTC_DS1307 RTC;

#define DS3231_I2C_ADDRESS 0x68 // Convert normal decimal numbers to binary coded decimal
#define I2C_ADDR    0x3F // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C	lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

//Set Relay Pins
int Zone1 = 5;
int Zone2 = 6;
int Zone3 = 7;

void setup () {

  lcd.begin (16, 2); //  <<----- My LCD was 16x2
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); // go home

  //Initialize the serial port, wire library and RTC module
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
  pinMode(Zone1, OUTPUT);
  pinMode(Zone2, OUTPUT);
  pinMode(Zone3, OUTPUT);
  //IF YOU USE RTC FOR THE FIRST TIME: If we remove the comment from the following line,
  // we will set up the module time and date with the computer one
  //RTC.adjust(DateTime(__DATE__, __TIME__));
}

void loop () {
  DateTime now = RTC.now();
  //We print the day
  Serial.print(now.day(), DEC);
  Serial.print('/');
  //We print the month
  Serial.print(now.month(), DEC);
  Serial.print('/');
  //We print the year
  Serial.print(now.year(), DEC);
  Serial.print(' ');
  //We print the hour
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  //We print the minutes
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  //We print the seconds
  Serial.print(now.second(), DEC);
  Serial.println();
  //We check the time and sent through the serial port every 3s

  // DateTime now = RTC.now();

  lcd.setCursor(0, 0);
  //We print the month
  lcd.print(now.month(), DEC);
  lcd.print('/');
  //We print the day
  lcd.print(now.day(), DEC);
  lcd.print('/');

  //We print the year
  lcd.print(now.year(), DEC);
  //lcd.print(' ');
  lcd.setCursor(0, 1);

  //We print the hour
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  //We print the minutes
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  //We print the seconds
  lcd.print(now.second(), DEC);

  // Setup For Zone 1 AM

  if (now.day() % 2 >= 0 ) {
    if (now.hour() == 17) {
      if (now.minute() >= 44 && now.minute() <= 50)
      {
        digitalWrite(Zone1, LOW);
        Serial.print("Relay On");
        lcd.setCursor(11, 0);
        lcd.print("Z1 ON");
      }
      else {
        digitalWrite(Zone1, HIGH);
        Serial.print("Relay Off");
      }
    }
  }

  //Setup For Zone 2 AM

  if (now.day() % 2 >= 0) {
    if (now.hour() == 17) {
      if (now.minute() >= 37 && now.minute() <= 37)
      {
        digitalWrite(Zone2, LOW);
        Serial.print("Relay On");
        lcd.setCursor(11, 0);
        lcd.print("Z2 ON");
      }
      else {
        digitalWrite(Zone2, HIGH);
        Serial.print("Relay Off");
      }
    }
  }

  //Setup For Zone 3 AM

  if (now.day() % 2 >= 0) {
    if (now.hour() == 17) {
      if (now.minute() >= 38 && now.minute() <= 38)
      {
        digitalWrite(Zone3, LOW);
        Serial.print("Relay On");
        lcd.setCursor(11, 0);
        lcd.print("Z3 ON");
      }
      else {
        digitalWrite(Zone3, HIGH);
        Serial.print("Relay Off");
      }

    }
  }

  //Set Time for 1 second after relay goes off
  if (now.day() % 2 >= 0) {
    if (now.hour() == 17) {
      if (now.minute() == 39)
        if (now.second() == 01)
        {
          Serial.print("LCD Clear");
          lcd.clear();
        }
    }
  }

  // Setup For Zone 1 PM

  if (now.day() % 2 >= 0) {
    if (now.hour() == 17) {
      if (now.minute() >= 40 && now.minute() <= 40)
      {
        digitalWrite(Zone1, LOW);
        Serial.print("Relay On");
        lcd.setCursor(11, 0);
        lcd.print("Z1 ON");
        lcd.setCursor(11, 1);
        lcd.print("    ");
      }
      else {
        digitalWrite(Zone1, HIGH);
        Serial.print("Relay Off");
      }
    }
  }

  //Setup For Zone 2 PM

  if (now.day() % 2 >= 0) {
    if (now.hour() == 17) {
      if (now.minute() >= 41 && now.minute() <= 41)
      {
        digitalWrite(Zone2, LOW);
        Serial.print("Relay On");
        lcd.setCursor(11, 0);
        lcd.print("Z2 ON");

      }
      else {
        digitalWrite(Zone2, HIGH);
        Serial.print("Relay Off");
      }
    }
  }

  //Setup For Zone 3 PM

  if (now.day() % 2 >= 0) {
    if (now.hour() == 17) {
      if (now.minute() >= 42 && now.minute() <= 42)
      {
        digitalWrite(Zone3, LOW);
        Serial.print("Relay On");
        lcd.setCursor(11, 0);
        lcd.print("Z3 ON");
      }
      else {
        digitalWrite(Zone3, HIGH);
        Serial.print("Relay Off");
      }
    }
  }

  //Set Time for 1 second after relay goes off
  if (now.day() % 2 >= 0) {
    if (now.hour() == 17) {
      if (now.minute() == 43)
        if (now.second() == 01)
        {
          Serial.print("LCD Clear");
          lcd.clear();
        }
    }
  }
  delay(1000);
}

Use CTRL T to format your code.

if(now.minute() >= 42 && now.minute() <= 42)
:wink:
if(now.minute() == 42 )

Narrow the problem down by commenting code sections out.

If things go off and on like that include seconds==0 to conditionals.

Don’t use delay()

The

LarryD:
if(now.minute() >= 42 && now.minute() <= 42)
:wink:
if(now.minute() == 42 )

The times were the same for testing. Since it will be covering a 10 minute window I will be changing the numbers accordingly.

LarryD:
If things go off and on like that include seconds==0 to conditionals.

Don’t use delay()

If I’m thinking about this correctly ==0 would only run at the start of the minute. Either way I tried >= and == the relays just stayed on all the time.

I have removed the delay.