Waterin and lightning Random

Hi guys,
I want to water 5min and light 12hours my plant per day. The problem is that I want it random.
For example 2hours light, then shut down, then 1h32 light etc... the same for water.
I have an arduino nano with RTC DS3132 and two relays 220V and this is the code I used for the beginning.

#include <DS3231.h>

#include <Wire.h>





DS3231 clock;
RTCDateTime dt;
int led = 8;
int relay = 9;

void setup()
{
  Serial.begin(9600);
  
  // Initialize DS3231
  Serial.println("Initialize DS3231");;
  clock.begin();

  // Disarm alarms and clear alarms for this example, because alarms is battery backed.
  // Under normal conditions, the settings should be reset after power and restart microcontroller.
  clock.armAlarm1(false);
  clock.armAlarm2(false);
  clock.clearAlarm1();
  clock.clearAlarm2();

  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
 
  // Manual (Year, Month, Day, Hour, Minute, Second)
 

  // Set Alarm - Every second.
  // DS3231_EVERY_SECOND is available only on Alarm1.
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm1(0, 0, 0, 0, DS3231_EVERY_SECOND);

  // Set Alarm - Every full minute.
  // DS3231_EVERY_MINUTE is available only on Alarm2.
  // setAlarm2(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm2(0, 0, 0, 0, DS3231_EVERY_MINUTE);
  
  // Set Alarm1 - Every 20s in each minute
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  clock.setAlarm1(0, 0, 0, 20, DS3231_MATCH_S);

  // Set Alarm2 - Every 01m in each hour
  // setAlarm2(Date or Day, Hour, Minute, Mode, Armed = true)
  clock.setAlarm2(0, 0, 1,     DS3231_MATCH_M);

  // Set Alarm - Every 01m:25s in each hour
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm1(0, 0, 1, 25, DS3231_MATCH_M_S);

  // Set Alarm - Every 01h:10m:30s in each day
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm1(0, 1, 10, 30, DS3231_MATCH_H_M_S);

  // Set Alarm - 07h:00m:00s in 25th day in month
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm1(25, 7, 0, 0, DS3231_MATCH_DT_H_M_S);

  // Set Alarm - 10h:45m:30s in every Friday (1 - Mon, 7 - Sun)
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm1(5, 10, 40, 30, DS3231_MATCH_DY_H_M_S);
  
  // Check alarm settings
  checkAlarms();
}

void checkAlarms()
{
  RTCAlarmTime a1;  
  RTCAlarmTime a2;

  if (clock.isArmed1())
  {
    a1 = clock.getAlarm1();

    Serial.print("Alarm1 is triggered ");
    switch (clock.getAlarmType1())
    {
      case DS3231_EVERY_SECOND:
        Serial.println("every second");
        break;
      case DS3231_MATCH_S:
        Serial.print("when seconds match: ");
        Serial.println(clock.dateFormat("__ __:__:s", a1));
        break;
      case DS3231_MATCH_M_S:
        Serial.print("when minutes and sencods match: ");
        Serial.println(clock.dateFormat("__ __:i:s", a1));
        break;
      case DS3231_MATCH_H_M_S:
        Serial.print("when hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("__ H:i:s", a1));
        break;
      case DS3231_MATCH_DT_H_M_S:
        Serial.print("when date, hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("d H:i:s", a1));
        break;
      case DS3231_MATCH_DY_H_M_S:
        Serial.print("when day of week, hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("l H:i:s", a1));
        break;
      default: 
        Serial.println("UNKNOWN RULE");
        break;
    }
  } else
  {
    Serial.println("Alarm1 is disarmed.");
  }

  if (clock.isArmed2())
  {
    a2 = clock.getAlarm2();

    Serial.print("Alarm2 is triggered ");
    switch (clock.getAlarmType2())
    {
      case DS3231_EVERY_MINUTE:
        Serial.println("every minute");
        break;
      case DS3231_MATCH_M:
        Serial.print("when minutes match: ");
        Serial.println(clock.dateFormat("__ __:i:s", a2));
        break;
      case DS3231_MATCH_H_M:
        Serial.print("when hours and minutes match:");
        Serial.println(clock.dateFormat("__ H:i:s", a2));
        break;
      case DS3231_MATCH_DT_H_M:
        Serial.print("when date, hours and minutes match: ");
        Serial.println(clock.dateFormat("d H:i:s", a2));
        break;
      case DS3231_MATCH_DY_H_M:
        Serial.println("when day of week, hours and minutes match: ");
        Serial.print(clock.dateFormat("l H:i:s", a2));
        break;
      default: 
        Serial.println("UNKNOWN RULE"); 
        break;
    }
  } else
  {
    Serial.println("Alarm2 is disarmed.");
  }
}

void loop()
{
  dt = clock.getDateTime();

  Serial.println(clock.dateFormat("d-m-Y H:i:s - l", dt));

  // Call isAlarm1(false) if you want clear alarm1 flag manualy by clearAlarm1();
  if (clock.isAlarm1())
  {
    Serial.println("ALARM 1 TRIGGERED!");
    digitalWrite(led, HIGH);
    delay(5000);
    digitalWrite(relay, HIGH);
    delay(1000);
   
  }
  else 
digitalWrite(led, LOW);
digitalWrite(relay, LOW);

  // Call isAlarm2(false) if you want clear alarm1 flag manualy by clearAlarm2();
  if (clock.isAlarm2())
  {
    Serial.println("ALARM 2 TRIGGERED!");
    
 }
 
  delay(1000);
}

Here or here

If you have a RTC, it really becomes difficult to understand why you can't think for yourself, instead of relying on a library that makes doing what you want 10 times more difficult.

You can, on any pass through loop(), see if it has become a new day. If it is, select a random water on hour from 0 to 12, storing the result in a static variable. Select a random light on hour and minute. Store them in static variables.

On every pass through loop(), see if it is time to turn the light on, the water on, the light off, and the water off.

You need about 20 lines of code, including reading from the RTC.

Paul S, thanks for the reponse! can you send me exemple of code please? I'm a beginner, maybe I understood what you said but I don't really know how to do.

godivaPrima, I'm not a botanist too, but the project is an artwork. The goal is to have an autonomous wild system. It's mean that nature grow with her rules. Light and water are also a part of this. It's not really real of course, it's not an A.I. but, I want to check it more real as possible.

val1108:
Paul S, thanks for the reponse! can you send me exemple of code please? I'm a beginner, maybe I understood what you said but I don't really know how to do.

godivaPrima, I'm not a botanist too, but the project is an artwork. The goal is to have an autonomous wild system. It's mean that nature grow with her rules. Light and water are also a part of this. It's not really real of course, it's not an A.I. but, I want to check it more real as possible.

It seems like an interesting experiment - I thought it was a science fair project: one panel shows the random plants, vs the next panel with the normal pattern plants. I think it would make a good demonstration of the importance of a consistent diurnal cycle (or conversely, the lack of consistency.) Cool project Val.

You don't generally get full working sketches for your project on this forum, it's a 'tough-teaching' learning place.

Follow Paul's scheme and build your program. Don't try to do the whole sketch at once. Build your sketch piece by piece, improving each part until it works the way you want.

If you do build it piece by piece, stay away from delay(); It will bite you fast when you try to pull it all together.

-jim lee