rtc controlled relay, millis controlled 5 seconds timer to turn off

as you can see, im horrible at explaining,

basically, i want a rtc ds3231 to tell my arduino to activate a relay at XX:XX for 5 seconds then shut off.

its at the bottom where i lose touch with the small grasp i have on this, i know its pretty ambitious tackling this with my little knowledge, but i feel i learn best when challenged. i know you guys like helping people in the right track so any help, even knowing if im close or not would be amazing,
thanks :slight_smile:

#include "RTClib.h"
#include <Wire.h>
#include <LiquidCrystal.h>
DS3231 rtc;





//water pump time on (ideally only for 5 seconds)
const int OnHour = 22;
const int OnMinute = 39;
const int OnSecond = 1;


//amount of time water is on for
const int watertime = 5000;

//water pump relay pin
const int water =  2;




void setup() {

  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  rtc.adjust(DateTime(__DATE__, __TIME__));
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }



  //set pins (outputs)

  pinMode(water, OUTPUT);

  //set pins (inputs)

}





void loop() {
  water1();
}







void water1()
{

  static unsigned char waterstate = LOW;
  static unsigned long wateron = 0;


  DateTime now = rtc.now();

  // serial print out of time
  char buf[100];
  strncpy(buf, "DD.MM.YYYY  hh:mm:ss\0", 100);
  Serial.println(now.format(buf));




////////////////////////////////////////////////////////////////// THIS IS MY ISSUE? ////////////////////////////////////////////////




  if (now.hour() == OnHour && now.minute() == OnMinute && now.second() == OnSecond)
  {
    Serial.println("ok");
    digitalWrite(water, HIGH);
  }
////////// specifically here?? ////////////////////////

  if (waterstate == HIGH)
  {
    if (millis() - wateron > watertime)
    {
      digitalWrite(water, LOW);
      waterstate = LOW;
    }
  }
}

if (waterstate == HIGH) <--- this will never happen you initialised it as LOW in this routine!

This on the other had might work:

if (now.hour() == OnHour && now.minute() == OnMinute && now.second() == OnSecond)
{
Serial.println("ok");
digitalWrite(water, HIGH);
wateron = millis(); <---- save millis here
}

if (digitalRead(water)==HIGH)
{
if (millis() - wateron > watertime)
{
digitalWrite(water, LOW);
}
}

ya that worked,

thanks so much.