NOOB - Doubts about creating exception by schedule

I’m creating a project where I want to free the source and hardware as free.

Full-source:

The code below needs a condition that after midnight it runs only 3 times, and do not take into account the variations of ON and OFF

void controlaIrrigacao()                                          //Função genérica para um quarto menu...
{
  unsigned long currentMillis = millis();

  OnTime = (estadoOn * 60000);

  OffTime = (estadoOff * 60000);

  switch (estadoSTR1)                                     //Controle do set1
  {
    case 0x01:                                        //Caso 1
      if (estadoSTR1 == 1 && startMillis == 0) {
        startMillis = currentMillis;
        state_reles_rain = HIGH;  // turn it on
        digitalWrite(pin_rele_rain, state_reles_rain);
      }

      if ((state_reles_rain == LOW) && (currentMillis - previousMillis >= OnTime))
      {
        state_reles_rain = HIGH;  // turn it on
        previousMillis = currentMillis;     // Remember the time
        digitalWrite(pin_rele_rain, state_reles_rain);    // Update the actual LED
      } else if ((state_reles_rain == HIGH) && (currentMillis - previousMillis >= OffTime))
      {
        state_reles_rain = LOW;  // Turn it off
        previousMillis = currentMillis;    // Remember the time
        digitalWrite(pin_rele_rain, state_reles_rain);   // Update the actual LED
      }

      break;
    case 0x02:                                    //Caso 2
      startMillis = currentMillis;
      state_reles_rain = LOW;  // turn it on
      digitalWrite(pin_rele_rain, state_reles_rain);
      break;                                  //Break
  }
} //end controlaIrrigacao

projeto_gaia_v2.ino (25.9 KB)

mills() does not have any notion of "midnight" since it only tracks elapsed time since startup. If you want to know when midnight occurs, you will need a real time clock (RTC) module.

Hello blh64 in the code has all the items related to the RTC

I did not put the whole code because it would get too big

If you need this on a google drive link

#include <DS3231.h>

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

// --- Configurações Iniciais ---
void setup() {
  Serial.begin(9600);
  Serial1.begin(115200);

  rtc.begin();
  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(MONDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(05, 2, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(1, 28, 2019);   // Set the date to January 1st, 2014

  //Inicializa LCD 16 x 2
  lcd.begin (16, 2);

  pinMode(butMOD, INPUT_PULLUP);         //Entrada para os botões (digitais 2) com pull-ups internos
  pinMode(butSTR, INPUT_PULLUP);         //Entrada para os botões (digitais 3) com pull-ups internos
  pinMode(On_buttonPin, INPUT_PULLUP);         //Entrada para os botões (digitais 2) com pull-ups internos
  pinMode(Off_buttonPin, INPUT_PULLUP);         //Entrada para os botões (digitais 3) com pull-ups internos

  pinMode (sen_nivel_minimo, INPUT);     //Sensor de nivel minimo da caixa d agua
  pinMode (sen_nivel_maximo, INPUT);     //Sensor de nivel maximo da caixa d agua
  pinMode (pin_led_green, OUTPUT);       //Led de sinalização que a caixa d agua esta cheia
  pinMode (pin_led_red, OUTPUT);         //Led de sinalização que a caixa d agua esta vazia

  pinMode (pin_rele_water, OUTPUT);     //Rele de agua
  pinMode (pin_rele_fert, OUTPUT);      //Rele do fertilizante
  pinMode (pin_rele_rain, OUTPUT);      //Rele do sistema de irrigação

  pinMode(buzzer, OUTPUT);              //Saida para o buzzer no pino 50

  pinMode(ECPin, INPUT);
  pinMode(ECPower, OUTPUT); //Setting pin for sourcing current
  pinMode(ECGround, OUTPUT); //setting pin for sinking current

  digitalWrite(pin_rele_water, LOW);    // Setup dos reles para manter desligados
  digitalWrite(pin_rele_fert, LOW);     // Setup dos reles para manter desligados
  digitalWrite(pin_rele_rain, LOW);     // Setup dos reles para manter desligados
  digitalWrite(ECGround, LOW); //We can leave the ground connected permanantly

  t_butMOD   = 0x00;                                      //limpa flag do botão Mode
  t_butSTR   = 0x00;                                      //limpa flag do botão start

  delay(100);

  sensors.begin();// gives sensor time to settle

  delay(100);

  R1 = (R1 + Ra);

  noTone(buzzer);

}

Then set the alarm on your RTC to trigger at midnight and when it does, run your routine 3 times and then reset the alarm to go off again on the next midnight