RTC If and else if Statements

I get an error in this code.

dht DHT;
#define DHT11_PIN 4 // use pin 2 on UNO to sample data from DHT module
const int relay1 = 2; //Digital pin that the Relay is connected
const int relay2 = 5; //Digital pin that the Relay is connected
const int OnHour1 = 10;
const int OnMin1 = 23;
const int OffHour1 = 10;
const int OffMin1 = 24;

void setup()
{
  Serial.begin(9600);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("DHT LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Humidity % \tTemperature (C) \tTime \tDate");
  lcd.begin(20, 4); // defines it is a 20 character four line display
  rtc.begin(); // Start the RTC library code
}

void loop()
{
  // READ DATA
  DateTime now = rtc.now();
  int chk = DHT.read11(DHT11_PIN);
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.print("\t");
  Serial.print(DHT.temperature, 1);
  Serial.print(",\t");
  Serial.print("\t");
  Serial.print("\t");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print(' ');
  Serial.print(now.day(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.println(now.year(), DEC);
  lcd.setCursor(0, 0); // start postion of Humidity text on LCD
  lcd.print(DHT.humidity, 0); // 0 creates whole number, 1 two decimal
  lcd.print("% Humidity ");
  lcd.setCursor(14, 0); // start postion of temperature text on LCD
  lcd.print(DHT.temperature * 1.8 + 32, 0); //Farhenheit conversion
  lcd.setCursor(17, 0);
  //lcd.print(”F“);
  //lcd.print(DHT.temperature, 0);
  //lcd.print(" C");
  lcd.setCursor(6, 1); // start postion of time text on LCD
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print('.');
  lcd.print(now.second(), DEC);
  lcd.setCursor(0, 2); // start postion of time text on LCD
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print('.');
  lcd.print(now.second(), DEC);
  // You can display in lcd by changing Serial to lcd I have only used time above not date
  //lcd.print(now.year(), DEC);
  //lcd.print('/');
  //lcd.print(now.month(), DEC);
  //lcd.print('/');
  //lcd.print(now.day(), DEC);
  //lcd.print(' ');
  //lcd.print(now.hour(), DEC);
  //lcd.print(':');
  //lcd.print(now.minute(), DEC);
  //lcd.print(':');
  //lcd.print(now.second(), DEC);
  //lcd.println();
  delay(1000); // screen - sample & LCD refresh time 1 second although DHT say min 2 seconds but works ok.

  if (now.hour == OnHour1 && now.minute == OnMin1) {
    digitalWrite (relay1, LOW);
  }
  else if (now.hour == OffHour1 && now.minute == OffMin1) {
    digitalWrite (relay1, HIGH);
  }
  //
  // END OF FILE

The error message
C:\Users\Documents\Arduino\rtc-lcd-dht\rtc-lcd-dht.ino: In function 'void loop()':

rtc-lcd-dht:101: error: invalid use of member function (did you forget the '()' ?)

if (now.hour == OnHour1) && (now.minute == OnMin1) {

^

rtc-lcd-dht:101: error: expected identifier before '(' token

if (now.hour == OnHour1) && (now.minute == OnMin1) {

What is wrong??

It looks like you need to add the parenthesis to the function call. You also have some of the parenthesis out of place.

if ((now.hour() == OnHour1) && (now.minute() == OnMin1))

The same goes for the else if statment.

else if ((now.hour() == OffHour1) && (now.minute() == OffMin1))

This should fix your compiler error.

Thank You yes that did compile. The sketch also prints out to a LCD hour, min, and sec. That does work but as I am debugging it the seconds will go from 2 digit to 3 digit and back again. This happen when it runs for 30 to 40 mins. It does some crazy counting in seconds but the min and hour is spot on. Any idea what causes that?

It does some crazy counting in seconds but the min and hour is spot on. Any idea what causes that?

Improper accomodation for different width fields. You always start printing at the same place but sometimes the total width is xx.xx.xx and other times are x.x.x You are always overwriting the first 5 locations, so the hours and minutes are always correct whether they are one or two digits

One simple solution is to write spaces over the entire time field before returning to the starting location and printing the time. You can also work out how to always print a fixed time field of 8 spaces. That is, you will always print two digits for h:m:s with leading 0s.(xx.xx.xx).

:slight_smile: :slight_smile: Thank You. It seem to settle down when I put a period on the end of it. Hopefully that fix it. Thanks again

I get a error relay1 is not a function....

if ((now.hour() == OnHour1) && (now.minute() == OnMin1)) {
    digitalWrite (relay1, LOW);
    if (relay1() == LOW) {
      lcd.setCursor(0, 3) && lcd.print("ON");

    }
    else if ((now.hour() == OffHour1) && (now.minute() == OffMin1)) {
      digitalWrite (relay1, HIGH);
      else if (relay1() == HIGH) {
        lcd.setCursor(0, 3) && lcd.print("OFF");
    if (relay1() == LOW) {

Of course you do. You are indeed trying to use relay1 as a function - relay1().

#define DHT11_PIN 4 // use pin 2 on UNO to sample data from DHT module

Good example of bad comments. :wink: That's why it's better not to repeat yourself in the comment but extend it. So

#define DHT11_PIN 4 // pin to sample data from DHT module

Or the saver alternative

const byte DHTPin =  4 // pin to sample data from DHT module

:confused: This code complies but does not put the on and off on display.

if ((now.hour() == OnHour1) && (now.minute() == OnMin1)) {
    digitalWrite (relay1, LOW);
    lcd.setCursor(0, 3);
    lcd.print("ON");

  }
  else if ((now.hour() == OffHour1) && (now.minute() == OffMin1)) {
    digitalWrite (relay1, HIGH);
    lcd.setCursor(3, 3);
    lcd.print("OFF");

I also see what I did with the relay1 Thank You. Again thank you for your help

If I input that into Arduino I'm struck with errors... Tip, post ALL your code if you change stuff :wink:

// Three examples brought together to create a temp / humidity and time display on a 20/4 LCD.
// RTC and LCD use i2c port (i2c Gnd 5V and pins a4 sda, a5 scl) DHT-11 BRICK unit uses Gnd 5V and pin 2
// Released to the public domain
//
/*-----( Import needed libraries )-----*/
#include <Wire.h>              // In standard library
#include <dht.h>               // https://arduino-info.wikispaces.com/TemperatureHumidity
#include <LiquidCrystal_I2C.h> // https://arduino-info.wikispaces.com/LCD-Blue-I2C
#include "RTClib.h"            // https://arduino-info.wikispaces.com/DS1307_RealTime_Clock_Brick
/*-----( Declare Constants )-----*/

/*-----( Declare objects )-----*/
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address Ox3F (Check yours)
RTC_DS1307 rtc;    // Create a RealTimeClock object (I set the time in another sketch)
/*-----( Declare Variables )-----*/
dht DHT;
#define DHT11_PIN 4 // use pin to sample data from DHT module
const int relay1 = 2; //Digital pin that the Relay is connected
const int relay2 = 5; //Digital pin that the Relay is connected
const int relay3 = 6; //Digital pin that the Relay is connected

const int OnHour1 = 0;
const int OnMin1 = 0;
const int OffHour1 = 0;
const int OffMin1 = 0;

const int OnHour2 = 10;
const int OnMin2 = 23;
const int OffHour2 = 10;
const int OffMin2 = 24;

const int OnHour3 = 10;
const int OnMin3 = 23;
const int OffHour3 = 10;
const int OffMin3 = 24;

void setup()
{
  Serial.begin(9600);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("DHT LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Humidity % \tTemperature (C) \tTime \tDate");
  lcd.begin(20, 4); // defines it is a 20 character four line display
  rtc.begin(); // Start the RTC library code
}

void loop()
{
  // READ DATA
  DateTime now = rtc.now();
  int chk = DHT.read11(DHT11_PIN);
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.print("\t");
  Serial.print(DHT.temperature, 1);
  Serial.print(",\t");
  Serial.print("\t");
  Serial.print("\t");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print(' ');
  Serial.print(now.day(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.println(now.year(), DEC);
  lcd.setCursor(0, 0); // start postion of Humidity text on LCD
  lcd.print(DHT.humidity, 0); // 0 creates whole number, 1 two decimal
  lcd.print("% Humidity ");
  lcd.setCursor(14, 0); // start postion of temperature text on LCD
  lcd.print(DHT.temperature * 1.8 + 32, 0); //Farhenheit conversion
  lcd.setCursor(17, 0);
  lcd.print("F");
  //lcd.print(DHT.temperature, 0);
  //lcd.print(" C");
  lcd.setCursor(0, 1); // start postion of time text on LCD
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print('.');
  lcd.print(now.second(), DEC);
  lcd.print('.');
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');
  lcd.print(now.day(), DEC);
  lcd.print(' ');
  lcd.setCursor(0, 2); // start postion of time text on LCD
  lcd.print("ZONE-1 " "ZONE-2 "  "ZONE-3");
  //lcd.print(now.second(), DEC);
  // You can display in lcd by changing Serial to lcd I have only used time above not date
  //lcd.print(now.year(), DEC);
  //lcd.print('/');
  //lcd.print(now.month(), DEC);
  //lcd.print('/');
  //lcd.print(now.day(), DEC);
  //lcd.print(' ');
  //lcd.print(now.hour(), DEC);
  //lcd.print(':');
  //lcd.print(now.minute(), DEC);
  //lcd.print(':');
  //lcd.print(now.second(), DEC);
  //lcd.println();
  delay(1000); // screen - sample & LCD refresh time 1 second although DHT say min 2 seconds but works ok.

  if ((now.hour() == OnHour1) && (now.minute() == OnMin1)) {
    digitalWrite (relay1, LOW);
    lcd.setCursor(0, 3);
    lcd.print("ON");

  }
  else if ((now.hour() == OffHour1) && (now.minute() == OffMin1)) {
    digitalWrite (relay1, HIGH);
    lcd.setCursor(3, 3);
    lcd.print("OFF");

  }

  if ((now.hour() == OnHour2) && (now.minute() == OnMin2)) {
    digitalWrite (relay2, LOW);
  }
  else if ((now.hour() == OffHour2) && (now.minute() == OffMin2)) {
    digitalWrite (relay2, HIGH);
  }

  if ((now.hour() == OnHour3) && (now.minute() == OnMin3)) {
    digitalWrite (relay3, LOW);
  }
  else if ((now.hour() == OffHour3) && (now.minute() == OffMin3)) {
    digitalWrite (relay3, HIGH);
  }
}

//
// END OF FILE
//

sprinkfitter:

// Three examples brought together to create a temp / humidity and time display on a 20/4 LCD.

// RTC and LCD use i2c port (i2c Gnd 5V and pins a4 sda, a5 scl) DHT-11 BRICK unit uses Gnd 5V and pin 2
// Released to the public domain
//
/-----( Import needed libraries )-----/
#include <Wire.h>              // In standard library
#include <dht.h>              // https://arduino-info.wikispaces.com/TemperatureHumidity
#include <LiquidCrystal_I2C.h> // https://arduino-info.wikispaces.com/LCD-Blue-I2C
#include "RTClib.h"            // https://arduino-info.wikispaces.com/DS1307_RealTime_Clock_Brick
/-----( Declare Constants )-----/

/-----( Declare objects )-----/
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address Ox3F (Check yours)
RTC_DS1307 rtc;    // Create a RealTimeClock object (I set the time in another sketch)
/-----( Declare Variables )-----/
dht DHT;
#define DHT11_PIN 4 // use pin to sample data from DHT module
const int relay1 = 2; //Digital pin that the Relay is connected
const int relay2 = 5; //Digital pin that the Relay is connected
const int relay3 = 6; //Digital pin that the Relay is connected

const int OnHour1 = 0;
const int OnMin1 = 0;
const int OffHour1 = 0;
const int OffMin1 = 0;

const int OnHour2 = 10;
const int OnMin2 = 23;
const int OffHour2 = 10;
const int OffMin2 = 24;

const int OnHour3 = 10;
const int OnMin3 = 23;
const int OffHour3 = 10;
const int OffMin3 = 24;

void setup()
{
  Serial.begin(9600);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("DHT LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Humidity % \tTemperature (C) \tTime \tDate");
  lcd.begin(20, 4); // defines it is a 20 character four line display
  rtc.begin(); // Start the RTC library code
}

void loop()
{
  // READ DATA
  DateTime now = rtc.now();
  int chk = DHT.read11(DHT11_PIN);
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.print("\t");
  Serial.print(DHT.temperature, 1);
  Serial.print(",\t");
  Serial.print("\t");
  Serial.print("\t");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print(' ');
  Serial.print(now.day(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.println(now.year(), DEC);
  lcd.setCursor(0, 0); // start postion of Humidity text on LCD
  lcd.print(DHT.humidity, 0); // 0 creates whole number, 1 two decimal
  lcd.print("% Humidity ");
  lcd.setCursor(14, 0); // start postion of temperature text on LCD
  lcd.print(DHT.temperature * 1.8 + 32, 0); //Farhenheit conversion
  lcd.setCursor(17, 0);
  lcd.print("F");
  //lcd.print(DHT.temperature, 0);
  //lcd.print(" C");
  lcd.setCursor(0, 1); // start postion of time text on LCD
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print('.');
  lcd.print(now.second(), DEC);
  lcd.print('.');
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');
  lcd.print(now.day(), DEC);
  lcd.print(' ');
  lcd.setCursor(0, 2); // start postion of time text on LCD
  lcd.print("ZONE-1 " "ZONE-2 "  "ZONE-3");
  //lcd.print(now.second(), DEC);
  // You can display in lcd by changing Serial to lcd I have only used time above not date
  //lcd.print(now.year(), DEC);
  //lcd.print('/');
  //lcd.print(now.month(), DEC);
  //lcd.print('/');
  //lcd.print(now.day(), DEC);
  //lcd.print(' ');
  //lcd.print(now.hour(), DEC);
  //lcd.print(':');
  //lcd.print(now.minute(), DEC);
  //lcd.print(':');
  //lcd.print(now.second(), DEC);
  //lcd.println();
  delay(1000); // screen - sample & LCD refresh time 1 second although DHT say min 2 seconds but works ok.

if ((now.hour() == OnHour1) && (now.minute() == OnMin1)) {
    digitalWrite (relay1, LOW);
    lcd.setCursor(0, 3);
    lcd.print("ON");

}
  else if ((now.hour() == OffHour1) && (now.minute() == OffMin1)) {
    digitalWrite (relay1, HIGH);
    lcd.setCursor(3, 3);
    lcd.print("OFF");

}

if ((now.hour() == OnHour2) && (now.minute() == OnMin2)) {
    digitalWrite (relay2, LOW);
  }
  else if ((now.hour() == OffHour2) && (now.minute() == OffMin2)) {
    digitalWrite (relay2, HIGH);
  }

if ((now.hour() == OnHour3) && (now.minute() == OnMin3)) {
    digitalWrite (relay3, LOW);
  }
  else if ((now.hour() == OffHour3) && (now.minute() == OffMin3)) {
    digitalWrite (relay3, HIGH);
  }
}

//
// END OF FILE
//

Works Thanks All