rtc values to a string

Hello,
once again I have another problem :slight_smile: this time I'm using an rtc (real time clock) but I want to make an if statment with the time but I don't know how. This is what I tried so far but I got an error.

#include <DS3231.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
DS3231 rtc(SDA, SCL);
int lastTime;
int currentTime;
String time;
void setup() {
  lcd.init();
  rtc.begin();
  rtc.setDOW(FRIDAY);
  rtc.setTime(20, 37, 0);
  rtc.setDate(4, 2, 2021);
  lcd.setCursor(0, 0);
  lcd.backlight();
}
void loop() {
  time = rtc.getTimeStr();
  lcd.clear();
  lcd.print(rtc.getDOWStr());
  lcd.print(" ");
  lcd.print(rtc.getDateStr());
  lcd.print(" -- ");
  lcd.setCursor(0, 1);
  lcd.print(rtc.getTimeStr());
  if(time == 12, 00, 00){
    lastTime = 12;
  }
  if (time >= 13, 00, 00 && lastTime == 12){
    time = time - 12;
  }
  delay(1000);
}
Invalid operands to binary expression ('String' and 'int')

(I'm using arduino IDE 20 beta)

This is not a String comparison (look up the "comma operator")

if (time >= 13, 00, 00)

My favorite way of keeping track of day time for alarms, timed events, etc. is to use seconds or minutes past midnight. Then a simple integer if statement works well.

Here is a simple demo that uses that technique to print the date and time every five minutes:

//time library demo: 5 minute interval timer
#include "TimeLib.h"

time_t unixTime; // a time stamp

void setup() {
  Serial.begin(9600);

  // set internal clock time and date. 
  // setTime(hr,min,sec,day,mnth,yr);

  setTime(23,0,0,6,11,2020);
  Serial.print("Starting clock. now() = ");
  Serial.println(now());
  // print as date_time
  print_date_time();
  
  // adjust for DST
  unixTime = now();  //get the unix timestamp
  unixTime += 3600UL; //add DST in spring
  setTime(unixTime);
  Serial.println("After adding DST offset");
  print_date_time();
  Serial.println("Starting loop()");
}

void loop() {
  static int minutes_past_midnight, last_print_time = 0;

  // check the clock
  minutes_past_midnight = hour() * 60 + minute();
  
  // every 5 minutes, print the data and time
  if (minutes_past_midnight != last_print_time && minutes_past_midnight % 5 == 0) {
    last_print_time = minutes_past_midnight;
    print_date_time();
  }
}

void print_date_time() { //easy way to print date and time
  char buf[20];
  snprintf(buf,sizeof(buf),"%02d/%02d/%4d %02d:%02d:%02d", day(), month(), year(), hour(), minute(), second());
  Serial.println(buf);
}

Hello,
I don’t really understand that code is there one for the library I’m using?

You will have to do some learning one way or another.

If you want to compare Strings or C-strings (which are not the same thing at all), there are tutorials on line explaining how. Search for "C/C++ compare strings". But you will find that the approach does not work well.

The approach I posted can easily be modified to use the functions built into DS3231.h (like getHour() and getMinute(), which return integer values), and are documented here.

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