This is my first post here, and have been lurking a while. I just picked up the Arduino and have been trying to learn the language and such for a few months.
I'm in the process of building a device with:
RTC
2 Relays
SHT20 Temp and Humidity Sensor
16x2 LCD
THE GOAL will essentially have a heating pad and a humidifier plugged into each relay. The sensor will read the levels and control the relays based on parameters I have set and based on what time of the day it is. I'm trying to get roughly a 75% humidity and 29.5 C level from 8am to 8pm. Then at 8pm to 8am (overnight) I'd like the levels to change to 80% humidity and 24 C temp. I am also trying to implement a 'deadzone' so that the relays aren't constantly cycling around the setpoint (hope that makes sense)
I've gone through several iterations of this sketch and am trying to finalize it. Maybe I'm over-complicating things.
// New Version June 15th
#include <Wire.h>
#include "DFRobot_SHT20.h" // establish sensor library
#include "RTClib.h" // establish RTC library
#include <LiquidCrystal_I2C.h> // establish LCD library
// #include "SD.h" // potential SD card use
DFRobot_SHT20 sht20; // Define SHT20 Temperature and Humidity Sensor
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
RTC_DS1307 RTC; // Define RTC
int humidityRelay = 7;
int tempRelay = 8;
int hourOn1 = 8;
int minOn = 0;
int hourOn2 = 20;
int Secs;
int Minutes;
int Hours;
float RH = sht20.readHumidity();
float Temp = sht20.readTemperature();
float RHsetpoint;
float RHdeadzone;
float TempSetpoint;
float TempDeadzone;
String myTime;
#define Humidifier_ON LOW
#define Humidifier_OFF HIGH
#define Heater_ON LOW
#define Heater_OFF HIGH
void setup() {
// SETUP RTC, SHT20, LCD Screen, and set Relay Pins and start reading the hardware
Serial.begin(9600);
Wire.begin();
RTC.begin(); // initialize RTC 24H format
// Datalogger RTC
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
// SHT20
sht20.initSHT20(); // Init SHT20 Sensor
delay(100); // Think about using this for potential wiggle room zone
sht20.checkSHT20(); // Check SHT20 Sensor
// I2C LCD SCREEN VOID SETUP
lcd.init(); // Initialize LCD
// Format Screen
lcd.backlight(); // Fill Screen with contant variables
lcd.setCursor(1,0);
lcd.print("Time: ");
lcd.setCursor(0,1);
lcd.print("T:");
lcd.setCursor(7,1);
lcd.print("H:");
lcd.setCursor(13,1);
lcd.print("%");
// RELAY VOID SETUP
pinMode(humidityRelay, OUTPUT); // Set relay pins as output
pinMode(tempRelay, OUTPUT);
//digitalWrite(humidityRelay, Humidifier_OFF); // Turn relays off initially.
//digitalWrite(tempRelay, Heater_OFF);
}
void loop() {
// Main void loop which calls the RunEnvironment sub and establishes time parameters
DateTime now = RTC.now();
Secs = now.second();
Hours = now.hour();
Minutes = now.minute();
myTime = myTime + Hours +":"+ Minutes +":"+ Secs ;
if(now.hour() >= hourOn1 && now.minute() == minOn) // RunEnvironment with these settings at 8:00 AM
{
RHsetpoint = 77;
RHdeadzone = 7;
TempSetpoint = 29.5;
TempDeadzone = 2;
RunEnvironment();
}
if(now.hour() == hourOn2 && now.minute() ==minOn) // RunEnvironment with these settings at 8:00 PM
{
RHsetpoint = 80;
RHdeadzone = 5;
TempSetpoint = 24;
TempDeadzone = 2;
RunEnvironment();
}
// LCD SCREEN VOID LOOP
lcd.setCursor(0,0);
lcd.print(myTime);
myTime = "";
lcd.setCursor(2,1); // Display Temperature and Humidity on LCD Screen
lcd.print(Temp,1);
lcd.setCursor(9,1);
lcd.print(RH,1);
delay(5000);;
}
void RunHumidity() // Humidity sub routine
{
if (RH < (RHsetpoint-RHdeadzone)) // compare humidity against the setpoint and deadzone and turn on relay
{
digitalWrite(humidityRelay, Humidifier_ON);
}
if (RH >= RHsetpoint) // if it is at the setpoint then the relay will turn off
{
digitalWrite(humidityRelay, Humidifier_OFF);
}
}
void RunTemperature() // Temperature sub routine
{
if (Temp < (TempSetpoint-TempDeadzone)) // compare temperature against the setpoint and deadzone and turn on relay
{
digitalWrite(tempRelay, Heater_ON);
}
if (Temp >= TempSetpoint) // once temperature is met the relay will turn off
{
digitalWrite(tempRelay, Heater_OFF);
}
}
void RunEnvironment(){ RunTemperature(); RunHumidity(); } // Humidity and Temperature sub routine combined
The main issues I'm running into now:
-
Using sub routines such as RunHumidity and RunTemperature and combining them into RunEnvironment. Right now they seem so close to being able to work but I don't think they're in the right order or something.
-
Trying to clean up my variables and such and put them in the global scope without there being 'not declared in scope' errors.
-
Trying to keep all time related things in the loop() and all relay related things in the subroutines.
This is compiling correctly, but it's glitching my LCD / Arduino out and not working. I just wanted to potentially get an idea of what's going on! Thanks for any help! I hope I'm using this forum correctly.