Tortoise Terrarium Controller

Hey everyone! I've been on here for help before and I'm making a lot of progress for a beginner. Now I'm stuck on the RTC module and having things on a schedule. I need to have relay 1 activate for 12 hours starting at 0700. The programming says 1900 because I'm actively trying to get it to work. The problem is, I don't think it's recognizing what I'm telling it. I'm honestly not sure how to word the "if" statement. Any and all help is greatly appreciated. Thank you!

/*
Godzilla monitor v1. Monitor temperature and humidity with 2x DHT22's. Temperature parameters are >75 <90*F. 
Humidity parameters are >40 <60%. Humidity is controlled by a submersible waterpump that activates when humidity falls 
below 40%, connected to an IoT relay, should only need 1 relay switch. A UVB light is also connected to an IoT and will 
cycle between 12hrs on / 12hrs off, should also need 1 relay switch here as well. Temperature and humidity will be 
displayed on a 16x2 LCD. A red LED will illuminate when temp or humidity is out of parameters. A blue LED will 
illuminate when the water pump is active. A green LED will illuminate when a relay is activated. 

-Eventually I want to connect it to the internet, possibly a different arduino board.
*/

#include <DHT.h>
#include <LiquidCrystal.h>
#include <I2C_RTC.h>
#include <time.h>

static DS3231 RTC;

//DHT11 signal pin
#define DHT_1 dht_1(2);
#define DHT_2 dht_2(3);


//Type of DHT
DHT dht_1(2, DHT22);
DHT dht_2(3, DHT22);

//LED pins
//RED DHT1-32
//RED DHT2-33
//Yellow DHT1-34
//Yellow DHT2-35
//BLUE-36
//GREEN-31
//GREEN-30



//LCD signal pins
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 13, en = 12, d4 = 5, d5 = 6, d6 = 7, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


void setup() {
  Serial.begin(9600);
//starting various things

//RTC Starting - MUST change time and day if changing program
  RTC.begin();
  RTC.setHourMode(CLOCK_H24);

  RTC.setDate(8,10,24);
  RTC.setTime(19,16,30);

//DHTs Starting
  dht_1.begin();
  dht_2.begin();

//LCD Start
  lcd.begin(16, 1);

// Print a message to the LCD.
  lcd.print("Godzilla's House");
  delay(3000);
  lcd.clear();
  lcd.println("Since Apr30,2024");
  delay(3000);
  lcd.clear();
  
}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(16, 1);

//RTC calling date and time
  switch (RTC.getWeek());
  Serial.print(RTC.getHours());
  Serial.print(":");
  Serial.print(RTC.getMinutes());
  Serial.print(":");
  Serial.print(RTC.getSeconds());
  Serial.print(" ");
  RTC.startClock();

  Serial.println("Monitoring");

//This is DHT_1
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht_1.readHumidity();
// Read temperature as Celsius (the default)
  float t = dht_1.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht_1.readTemperature(true);
// Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT_1!"));
    lcd.println(F("Failed to read from DHT_1!"));
    return;
  }

  Serial.print(F("Hum: "));
  Serial.print(h);
  Serial.print(F("%  Temp: "));
  Serial.print(f);
  Serial.print(F("°F"));
  lcd.print(F("Hum: "));
  lcd.print(F("%  Temp: "));
  lcd.print(F("°F"));


 delay(600);
    
//This is DHT_2
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h2 = dht_2.readHumidity();
// Read temperature as Celsius (the default)
  float t2 = dht_2.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
  float f2 = dht_2.readTemperature(true);

// Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT_2!"));
    lcd.println(F("Failed to read from DHT_2!"));
    return;
  }

  Serial.print(F("Hum: "));
  Serial.print(h2);
  Serial.print(F("%  Temp: "));
  Serial.print(f2);
  Serial.print(F("°F"));


//If temp above 73 on DHT1/2 turn on Red LEDs   
    if (dht_1.readTemperature(); f < 75.00, f > 90.00) digitalWrite(32, HIGH);
    else digitalWrite(32, LOW);
    if (dht_2.readTemperature(); f2 < 75.00, f2 > 90.00) digitalWrite(33, HIGH);
    else digitalWrite(33, LOW);

//added lcd write for temp high
    if (dht_1.readTemperature(); f < 75.00, f > 90.00) 
    Serial.write("Temp High");
    lcd.write("Temp High");
    if (dht_2.readTemperature(); f < 75.00, f > 90.00) 
    Serial.write("Temp High");
    lcd.write("Temp High");
    delay(600);

//IF Humidity below 40% turn on Yellow LEDs
    if (dht_1.readHumidity(); h < 40, h > 60) digitalWrite(34, HIGH);
    else digitalWrite(34, LOW);
    if (dht_2.readHumidity(); h2 < 40, h2 > 60) digitalWrite(35, HIGH);
    else digitalWrite(35, LOW);

//Added lcd write for humidity high
    if (dht_1.readHumidity(); h < 40, f > 60) 
    Serial.write("Humidity High");
    lcd.write("Humidity High");
    if (dht_2.readHumidity(); h < 40, f > 60) 
    Serial.write("Humidity High");
    lcd.write("Humidity High");

//Relay 1 for UVB light, 12hrs on, 12hrs off, 2 green LEDs show when active
//12hrs in milliseconds: 43200000 ms
    if (RTC.getHours() >= 1900) 
    digitalWrite(30, HIGH);
    else if (RTC.getHours() >= 1917)
    digitalWrite(30, LOW);

    //delay(600);
    //digitalWrite(30, HIGH);
    //delay(600);
    //digitalWrite(30, LOW);

//Relay 2 
    digitalWrite(31, HIGH);
    delay(50);
    digitalWrite(31, LOW);
    delay(1000);
    //digitalWrite(31, LOW);

//Waterpump operation - Change parameters before going LIVE, currently set-up for testing
if (h < 40, h2 < 40){
digitalWrite(40, HIGH);
digitalWrite(36, HIGH);
} 
else if (h >= 60, h2 >= 60) {
  (digitalWrite(40, LOW));
  digitalWrite(36, LOW);
return;
}
}
//if (h2 < 3); digitalWrite(40, HIGH);
// }else digitalWrite(40, LOW);
//if (digitalWrite(40,HIGH), digitalWrite(40, HIGH);
// }else digitalWrite(36, LOW);



  

What Arduino re you using?

Please explain this ifs?

RTC.getHours() returns an byte value 0-23.

uint8_t DS3231::getHours()
{

I think that the best way to deal with rtc time and scheduling is to use minutes after midnight = (60 * hours)+ minutes.

1 Like
if (RTC.getHours()*100+RTC.getMinutes() >= 1900)

What is this intended to do?

ATmega2650 from elegoo

Oh boy. Okay. lemme try and explain. I'm a beginner so bear with me, I'm sure there is more lines of code then needed.
if (dht_1.readTemperature(); f < 75.00, f > 90.00) digitalWrite(32, HIGH);
This one if the temperature falls under 75 or above 90 a red LED illuminates.

if (dht_1.readHumidity(); h < 40, h > 60) digitalWrite(34, HIGH);

if (dht_2.readHumidity(); h2 < 40, h2 > 60) digitalWrite(35, HIGH);
else digitalWrite(35, LOW);
These two turns on a yellow LED when its under 40 or above 60% humidity.

if (dht_1.readHumidity(); h < 40, f > 60)

if (dht_2.readHumidity(); h < 40, f > 60)
The above 2 that are nearly identical just write the warning to the screen or serial.

if (h < 40, h2 < 40){

if (dht_2.readTemperature(); f2 < 75.00, f2 > 90.00) digitalWrite(33, HIGH);
This one turns on a RED LED when below or above the temp specified.

After looking at my program thus far last night, I realized, at least I think I can, that I can make a lot of this in the same "IF" statement. I'm not quite there yet and have learned a ton along the way on this one. I ended up figuring out the time thing last night. At least it turns on the relay pin and off when the time reaches the given parameter.

Honestly, I'm not sure. Initially I had the week of the year listed, so it may be a remnants of that. I've since deleted everything but the time and date.

So basically, I'm losing an hour? Just so I understands what happens here: if nothing were to change, since we only get values 0-23, I would lose an hour each day. meaning the lamps would turn on a hour earlier each day? Just trying to understand what's happening and what will happen.

if nothing were to change, since we only get values 0-23, I would lose an hour each day. meaning the lamps would turn on a hour earlier each day?

if (RTC.getHours() >= 1900) 
    digitalWrite(30, HIGH);
    else if (RTC.getHours() >= 1917)
    digitalWrite(30, LOW);

Since ,getHours can only return a value from 0 to 23, neither of the if conditionals can ever be true.

Okay. I did see the recommendations made by you and some others on how to keep track of the time. I do have a few questions though.

In this recommendation, it's RTC.getHours*100+RTC.getMinutes >=1900, What is the "100" here? Is this to get the time into a 24hr format?

In this recommendation ,

[quote="cattledog, post:4, topic:1266256"]
I think that the best way to deal with rtc time and scheduling is to use minutes after midnight = (60 * hours)+ minutes.
[/quote] Its 60*hours +minutes. is the 60 being multiplied to give us a 12 hour format?

Again, Apologies for being new and thanks again for all the help!

Yes.

Its 60*hours +minutes. is the 60 being multiplied to give us a 12 hour format?

Not really. It still depends on a 24 hour clock with out am/pm. It does however simplify the adding and subtracting of times for intervals and a time difference in minutes.

Interesting project..
Strange logic..
simmed..
had to use a different RTC..
changed a few other things as well..

good luck.. ~q

Kind-of.

You wrote 1700 to mean 5pm. But C language simply sees that as a number, one thousand seven hundred. It doesn't understand it as a time.

To take the hours (0 to 23) and minutes (0 to 59) from the RTC and compare it with 1700 in the way you intended, you can do that by multiplying the hours by 100 and adding the minutes. This works, and it's really convenient, but it's not perfect. For example if you wanted to work out the difference between two times like 1730 and 1800, you won't get the 30 minute answer you wanted. C language thinks these are simply two numbers, and the difference is 70.

The suggestion from @cattledog was to multiply the hours by 60 instead of 100. This gives you the number of minutes since midnight. The advantage of doing that is that you can calculate the difference between 2 times and it will work correctly. The disadvantage is that you can't write 1700 to mean 5pm. 1700 would mean 28 hours and 20 minutes, which makes no sense as a time, on this planet! You would need to write 5pm as 1020 which isn't convenient or intuitive.

So each method has advantages and disadvantages.

I should warn you about a "gotcha!".

If you write 0700 to mean 7am, something unexpected will happen. In C language, a number beginning with a leading zero is interpreted as Octal representation, the base 8 number system. 0700 in Octal translates to 448 in decimal, which would result in your Tortoises walking up over 2 hours before you intended!

1 Like

I think I didn't explain my question well.
I'll redo it.
For example, the syntax in this if :

if (dht_1.readTemperature(); f < 75.00, f > 90.00)

Unless I'm missing something, in my opinion the syntax should be:

"&& means logical and"

if (f < 75.00 && f > 90.00) or it can be like this:
if (f < 75.00 and f > 90.00)

and the other ifs have this "different" syntax.

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