Storing Day number from RTC so it won't change

Hello,

I would like to ask if there is a way to store value from RTC into integer to keep it from changing later

for ex. I have

int DayNow = day.now();

and I don't want this value to change every day.

You can fill in the rest.

bool dayIsChanged = false

void setup()
{
    Serial.begin(115200);
    
}

void loop()
{
  if(dayIsChanged == false)
  {
   int DayNow = day.now();
   dayIsChanged = true;  // day will not change until dayIsChanged  is made false
  }
}

What about if I want to wait for 3 days for example ?

What are you trying to do?

I'm trying to make a watering controller which is checking soil moisture.

If it's high, I want to wait for 3 days and check moisture again. If it gets lower than some value, I want to add water and if it's not, I want to wait one more day.

I don't want to check everyday.

Then the day doesn't matter. Just record the time when you apply water and use that to see if it's worth checking the soil again.

You can use millis for this purpose.

Anyway, something like this?

void setup()
{
   Serial.begin(115200);
}

void loop()
{
   // call a function (newDay()) that increments daysSinceChange every new day
   // maybe at midnight and changes the day after 3.  I leave to you how to
   // determin a new day and call the function.
}

void newDay()
{
   static unsigned int daysSinceChange = 0;
   daysSinceChange ++;
   if (daysSinceChange > 3)
   {
      int DayNow = day.now();
      daysSinceChange = 0;
   }
}


Won't the daysSinceChange variable increment every time I will call out newDay function in main loop ?

I'm a bit of a newbie :sweat_smile:

Yes it will. Don't call it until the start of a new day, say at midnight using data from your RTC.

Or, probably the simplest is like @ wildbill said, put it in a millis() timer.

millis() for timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

So I made it like this :

// Part of void loop();

  if (hour == now.hour() && min == now.minute()) {
      newDay();
  } 

  delay(1000);
}

void newDay()
{
   DateTime now = rtc.now();
   static unsigned int daysSinceChange = 0;
   int i=3;
   daysSinceChange ++;
   if (daysSinceChange > i){
      DayNow = now.day();
      daysSinceChange = 0;
   }

   if (SoilHumPercentage <= 70) {
      digitalWrite(Relay, HIGH);
      delay(5000);
      digitalWrite(Relay, LOW);
      i = 3;
   } else {
      i++;
   }
}

No idea if it works.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.