Day Countdown with RTC

I want to make a hydroponic project that can count down the harvest day using RTC and display it on LCD, and I'm having a problem on the program. The LCD will display others thing like nutrition and Ph but I'm having a problem on the day countdown only. Like I'm stuck? Anyway here's the program. Please help, thank you!

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS3231.h>
 
DS3231  rtc(SDA, SCL);
LiquidCrystal_I2C lcd(0x27, 20, 4);

int dates=28;

void setup()
{
Serial.begin(115200);
Serial.println("Trial LCD + DS3231");
 
rtc.begin();
 
lcd.begin(); 
lcd.setBacklight(255);
 
rtc.setDate(7, 3, 2023);
rtc.setDOW(0);
 
delay(5000);
lcd.clear();
}
 
void loop()
{
lcd.setCursor(00,00);
lcd.print("HIDROPONIK"); //hydroponic
 
lcd.setCursor(00,1);
lcd.print("Nutrisi: PPM"); //nutrition

lcd.setCursor(00,2);
lcd.print("PH: "); 

if (rtc.getDateStr() != dates) {
  lcd.setCursor(00,3);
  lcd.print("Panen:"); //harvest
  lcd.setCursor(7,3);
  lcd.print(rtc.getDateStr());
  lcd.setCursor(11,3);
  lcd.print(" hari");
}
else{
  Serial.println("Hari panen");
  lcd.clear();
  lcd.setCursor(0,3);
  lcd.print("HARI PANEN");
}

delay (5000);
lcd.clear();
}

Hi, I've moved your topic to a more appropriate forum section. (It has nothing to do with the version of the IDE you are using.)

Thanks for using code tags!

1 Like

Thank you! I'm new to arduino community forum.

You can calculate the difference between 2 dates by using rtc.unixtime(). This returns a date & time as a number of seconds. Do this to the current date and your harvest date, subtract one from the other and divide by the number of seconds in a day.

Each forum section/category has a sticky post at the top "About the XYZ Category" which explains what type of questions are appropriate there. This will help you choose the best forum section for your question.

It shows an error

'class DS3231' has no member named 'unixtime'

Ah, sorry, my bad. It is a method of the DateTime class, not the DS3231 class.

I think you must use it like this

uint32_t timeStamp = RTClib::now().unixtime();

Umm... is this the code supposed to be?

myDT = RTClib::now();

uint32_t timeStamp = RTClib::now().unixtime();

#define SECONDS_IN_ONE_DAY 86400
uint64_t timeStamp = myDT.unixtime();
timeStamp += SECONDS_IN_ONE_DAY;
myDT = DateTime(timeStamp);

Anyway, it shows an error

'myDT' does not name a type

I wonder if 'myDT' need to be replaced with 'rtc'?

No. But thanks for this insight into your level of programming experience.

Question: how will the harvest date be set in your project? How will you avoid losing this date of there is a power outage?

Let's assume you have the harvest date set in your code somehow

DateTime harvest = DateTime(2023, 8, 3, 0, 0, 0);

You can then calculate the countdown like this

DateTime now = RTClib::now();
int countdown = (harvest.unixtime() - now.unixtime()) / 86400;

Oh, thank you for knowing me better haha.
And thank you for explaining it, I understand better now.

But DateTime is still an error though,
'DateTime' does not name a type; did you mean 'Time'?

Oh. Maybe you are not using the library I thought. How did you install the DS3231 library? From Library Manager or a .zip file? Which one is it, because there are more than one DS3231 library available I think.

1 Like

From .zip file GitHub - NorthernWidget/DS3231: Communicates between Arduino-programmed AVR and Maxim DS3231 RTC: splice of Ayars' (http://hacks.ayars.org/2011/04/ds3231-real-time-clock.html) and Jeelabs/Ladyada's (https://github.com/adafruit/RTClib) libraries

Unfortunately true, and has caused confusion before. There are actually dozens of them.

I try to stay safe by using only the Jeelabs or Adafruit fork of that.

1 Like

That is the one I thought you were using...

Look at this example code from the library you posted the link to:

// now.pde
// Prints a snapshot of the current date and time along with the UNIX time
// Modified by Andy Wickert from the JeeLabs / Ladyada RTC library examples
// 5/15/11

#include <Wire.h>
#include <DS3231.h>

RTClib myRTC;

void setup () {
    Serial.begin(57600);
    Wire.begin();
    delay(500);
    Serial.println("Nano Ready!");
}

void loop () {
    
    delay(1000);
    
    DateTime now = myRTC.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
}

It uses DateTime. But you get an error when you try to use DateTime.

Is it possible you have multiple DS3231 libraries installed and the IDE/compiler is choosing the wrong one?

Do you see any compiler warnings when you verify the code? (Please post them inside code tags).

@anon57585045 if there is more than 1 DS3231 library installed, I know that the compiler will make a decision about which one to use. Is there a way to specify/qualify which one you want it to use? (Other than un-installing all but one of them.)

In the case of the Adafruit library, the include file is not named DS3231.h, so if some DS3231.h were installed, the compiler would not even look for it. This is from one of my sketches:

#include "RTClib.h" // https://github.com/adafruit/RTClib

I don't know how to select/deselect identically named library files, I just try to avoid it. I have two main sketch folders, one is a "working" and one is an "archive" folder. If there was a conflict, I would just move one I don't want to use to the archive folder, manually.

1 Like

I have two libraries, one called DS3231.zip, and the others called DS3231-master.zip

When I uninstall the DS3231.zip, this error occured

no matching function for call to 'DS3231::DS3231(const uint8_t&, const uint8_t&)'

Either I uninstall the DS3231-master or have two of them installed, this error occured

'DateTime' does not name a type; did you mean 'Time'?

If I'm not mistaken, I download DS3231-master.zip from the link, but I don't really remembered the DS3231.zip one.

I think you must uninstall the other DS3231 library. This should take care of the error about DateTime.

Which one? because when I uninstall the DS3231.zip, the error wasn't the DateTime but this though:

DS3231  rtc(SDA, SCL);

no matching function for call to 'DS3231::DS3231(const uint8_t&, const uint8_t&)'

I think the other DS3231 library you installed is this one by Rinky-Dink Electronics:

http://www.rinkydinkelectronics.com/library.php?id=73

I think this is the library your sketch was written to use, because, among other clues, it uses a function .getDateString() which that library has but other DS3231 libraries (including the one you posted a link to) do not have.

This Rinky-Dink library's main page says

IMPORTANT:
The library has not been tested in combination with the Wire library and I have no idea if they can share pins. Do not send me any questions about this.

For this reason, I would suggest you stop using this library, unless there is some reason you must use it.

I recommend you uninstall all your DS3231 libraries, then install one of those recommended by @anon57585045 using the Library Manager, if possible.

Have a look at the example sketches that come with the new library and adapt your sketch to use the same functions.

If you get stuck, post your updated code and the error messages you see.