RTC if else statement

Hi guys,

I need some help because I have a hard time to find a solution to my problem. I try to set day/night based on an RTC DS3231, and the following code doesn’t work:

if ((rtc.getTimeStr() >= 07) && (rtc.getTimeStr() < 22) {

client.println("day");

} else if ((rtc.getTimeStr() >= 22) && (rtc.getTimeStr() < 7) {

client.println("night");

I have try to use time format like: 7, 07, 0700, “07:00:00” but none of them seems to work.

The code: cl.print(rtc.getTimeStr()); print the time but I cannot use it in “if/else” statements.

Any idea would be awesome.

Thank you

Based on its name, I doubt getTimeStr will return a number. Look at other methods that come with the library that you’re using.

Also there is no need for ‘else if’ , an ‘else’ will do; after all a number can never be bigger than 22 and at thr same time be smaller than 7.

Lastly, please post your complete code.

if ((rtc.getTimeStr() >= 07)

integer literals beginning with a zero are interpreted as octals in C++, FYI.

does your library not have a hour() and minute() member function?

i use:

#include <DS3231.h>

I just want to print day and night… but cannot seem to work.

the code I use:

// DS3231_Serial_Easy
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// A quick demo of how to use my DS3231-library to 
// quickly send time and date information over a serial link
//
// To use the hardware I2C (TWI) interface of the Arduino you must connect
// the pins as follows:
//
// Arduino Uno/2009:
// ----------------------
// DS3231:  SDA pin   -> Arduino Analog 4 or the dedicated SDA pin
//          SCL pin   -> Arduino Analog 5 or the dedicated SCL pin
//
// Arduino Leonardo:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 2 or the dedicated SDA pin
//          SCL pin   -> Arduino Digital 3 or the dedicated SCL pin
//
// Arduino Mega:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 20 (SDA) or the dedicated SDA pin
//          SCL pin   -> Arduino Digital 21 (SCL) or the dedicated SCL pin
//
// Arduino Due:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin
//          SCL pin   -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin
//
// The internal pull-up resistors will be activated when using the 
// hardware I2C interfaces.
//
// You can connect the DS3231 to any available pin but if you use any
// other than what is described above the library will fall back to
// a software-based, TWI-like protocol which will require exclusive access 
// to the pins used, and you will also have to use appropriate, external
// pull-up resistors on the data and clock signals.
//

#include <DS3231.h>

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

void setup()
{
  // Setup Serial connection
  Serial.begin(115200);
  // Uncomment the next line if you are using an Arduino Leonardo
  //while (!Serial) {}
  
  // Initialize the rtc object
  rtc.begin();
  
  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(1, 1, 2014);   // Set the date to January 1st, 2014
}

void loop()
{
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Wait one second before repeating :)
  delay (1000);
}

it’sthere something better? like to write:

if hours 22,23,0,1,2,3,4,5,6,7 write night else write day?

you want to do it like this:

#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>

struct TimeSettings {
  byte hour;
  byte minute;
};

const TimeSettings sunrise = { 6, 30};
const TimeSettings sunset = { 18, 30};

bool lastState = false;


void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  bool isDay = (timerEvaluate(sunrise, sunset));
  if (isDay != lastState)
  {
    if (isDay)
    {
      digitalWrite(13, HIGH);
    }
    else
    {
      digitalWrite(13, LOW);
    }
  }
  lastState = isDay;
}


bool timerEvaluate(struct TimeSettings start_time, struct TimeSettings finish_time)  // comparing time here is easier with Unix timestamps...
{
  int on_time = tmConvert_t(year(), month(), day(), start_time.hour, start_time.minute, 0);
  int off_time = tmConvert_t(year(), month(), day(), finish_time.hour, finish_time.minute, 0);
  int now_time = tmConvert_t(year(), month(), day(), hour(), minute(), second());
  //
  if (on_time < off_time)
  {
    return (now_time > on_time && now_time < off_time);
  }
  else if (off_time < on_time)
  {
    return (now_time > on_time || now_time < off_time);
  }
  else // if both on and off are set to the same time, I'm confused... so let's do nothing
  {
    return false;
  }
}

time_t tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss)
{
  tmElements_t tmSet;
  tmSet.Year = YYYY - 1970;
  tmSet.Month = MM;
  tmSet.Day = DD;
  tmSet.Hour = hh;
  tmSet.Minute = mm;
  tmSet.Second = ss;
  return makeTime(tmSet);
}

but you need to convert to your library…

Thank you very much BulldogLowell

I just want to print a word on a html page that is on sd card, I will try to adapt your code. I tough that will be simpler to make a print with day and night like AM/PM but from 22 to 7.

Thanks again

else if ((rtc.getTimeStr() >= 22) && (rtc.getTimeStr() < 7) {

Exactly how many numbers are BOTH >= 22 AND < 7? I can’t think of very many…

Regards,
Ray L.

Hi,

I am also try to do the same but I add sunMoon.h library (Astronomical-clock ) which can change the sunrise and sunset time day by day in 2X sun tracker with 03 different mode of operation.

second think I am using DS3232RTC.h with DS3231 model with i2c 20*4 LCD and get the same result.

Some comment about librarys

library functions are return the sunrise and sunset times in a form of unsigned 32-bit word - in a UNIX time format (a seconds elapsed from 1st January 1970). And date and time in human readable format using a function similar to printDate(). Another library, timeLib, has a function as day(), month() and so on, that take a UNIX time and return a day, month, year, hour and minutes as a decimal numbers.

If we can fix these day night definition with sunMoon.h library it will help a lot.

My code is attached below I use LDR with RTC and if else statement but it is not good way to handle this.

It is lengthy code and many think are may be useless I apologies for that you can ignore I want help in"case 3://LED_03 on " day night definition when time is argument.

Thanks

case 3://LED_03 on

      lcd.setCursor(4, 1);
      lcd.print("Cloud ");
      digitalWrite(LED_01, LOW);
      digitalWrite(LED_02, LOW);
      digitalWrite(LED_03, HIGH);

              int tol = 20;
        int lt = analogRead(ldrlt); // top left
        int rt = analogRead(ldrrt); // top right
        int ld = analogRead(ldrld); // down left
        int rd = analogRead(ldrrd); // down rigt

  /* 
        lt = map(lt, 0, 1023, 0, 255);
        rt = map(rt, 0, 1023, 0, 255);
        ld = map(ld, 0, 1023, 0, 255);
        rd = map(rd, 0, 1023, 0, 255);
        delay(1);

        int difflt = abs(lt - oldlt);
        int diffrt = abs(rt - oldrt);
        int diffld = abs(ld - oldld);
        int diffrd = abs(rd - oldrd);


     if (difflt > TOLERANCE) {
          oldlt = lt;
        }   // only save if the val has changed enough to avoid slowly drifting}
        if (diffrt > TOLERANCE) {
          oldrt = rt;
        }   // only save if the val has changed enough to avoid slowly drifting}
        if (diffld > TOLERANCE) {
          oldld = ld;
        }  // only save if the val has changed enough to avoid slowly drifting}
        if (diffrd > TOLERANCE) {
          oldrd = rd;
        }  // only save if the val has changed enough to avoid slowly drifting}

        int avt = (lt + rt) / 2; // average value top
        int avd = (ld + rd) / 2; // average value down
        int avl = (lt + ld) / 2; // average value left
        int avr = (rt + rd) / 2; // average value right

        int dvert = avt - avd; // check the diffirence of up and down
        int dhoriz = avl - avr;// check the diffirence og left and rigt
*/

 time_t sRise = sm.sunRise();
 time_t sSet  = sm.sunSet();

if (RTC.get() > sRise && RTC.get() < sSet && lt > 680 )
  {
    Serial.println("Day");
    lcd.setCursor(4, 3);
    lcd.print("Day");  
  }
  else if (RTC.get() > sSet && lt <590 || RTC.get() > sSet && lt <590)
  {
    Serial.println("Night ");
    lcd.setCursor(4, 3);
    lcd.print("Night");
  }
  else
  {
    Serial.println("Error ");
    lcd.setCursor(4, 3);
    lcd.print("Error");
  }
      break;

1.1_Equation_based_lcd_and_temp_23-3-18.ino (22.2 KB)