Hello,
I am having issues getting my switch case values to print on the LCD and on the Serial Monitor when the LCD portion of the code is present. (If all the "lcd.print" are not there, the switch cases print on the serial monitor)
Additionally, I was wondering if there was a way to continuously print on the lcd while the void loop still runs in the background.
As of right now it the code works as follows:
- the void loop runs and prints on the serial monitor
- the lcd goes through the 3 screens with 3sec delay (time to read the screen)
- then it waits 5 seconds (LCD has a cleared screen) and runs again
I don't want to match the lcd delay to the void loop delay because eventually I want the void loop to run every 5–10 mins and I still want the lcd to print out a status report at the current pace.
Thanks!
My code is as follows:
// Include Libraries
#include <dht.h> //humidity and temperature sensor
#include <virtuabotixRTC.h> //real clock
#include <LiquidCrystal.h> // lcd
//Initialize and Define Pins
virtuabotixRTC myRTC(7, 6, 5);
dht DHT;
#define DHT11_PIN 22
const int sensorMin = 120;
const int sensorMax = 800;
int lightsensorPin = A0;
int soilsensorPin = A1;
int soilsensorValue = 0;
int soilpercent = 0;
int fanPin = 24;
int relay1 = 31; //humidifier
int relay2 = 33; //grow lamp
int relay3 = 35; //heating pad
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
LiquidCrystal lcd(51, 49, 47, 45, 43, 41);
void setup() {
Serial.begin(9600);
lcd.begin(16,2); // initializes lcd
pinMode(fanPin,OUTPUT);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
//Start with all relays off
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
}
void loop() {
// Read Values
int chk = DHT.read11(DHT11_PIN);
myRTC.updateTime();
int analogLight;
int rangeLight;
analogLight = analogRead(lightsensorPin);
rangeLight = map(analogLight,sensorMin,sensorMax,0,3);
soilsensorValue = analogRead(soilsensorPin);
soilpercent = convertTosoilpercent(soilsensorValue);
printValuesToSerial();
// If Humidity is high OR Temp is high (>80F): turn fans on
if (DHT.humidity >= 80 || DHT.temperature > 30){
digitalWrite(fanPin,HIGH);
} else {
digitalWrite(fanPin,LOW);
}
// If Temp is low (<68F): turn on heating pad
if (DHT.temperature < 20) {
digitalWrite(relay3,HIGH);
} else {
digitalWrite(relay3,LOW);
}
// If Time of Day is between 8am and 3pm AND the light sensor detects dim or below: turn relay 2 (the lamp) on
while (myRTC.hours > 8 && myRTC.hours < 15){
if (rangeLight < 2){
//(myRTC.hours > 8 && myRTC.hours < 15 && rangeLight < 2)
digitalWrite(relay2,HIGH);
} else {
digitalWrite(relay2,LOW);
}
}
//If the humidity is low: turn on humidifier
if (DHT.humidity < 40) {
digitalWrite(relay1,HIGH);
} else {
digitalWrite(relay1,LOW);
}
//Reflect Soil Moisture with LED: if high = green, if med = blue, if low = red
if (soilsensorValue > 465){
digitalWrite(greenPin,HIGH); // green
} else {
digitalWrite(greenPin,LOW); }
if (soilsensorValue > 257 && soilsensorValue < 464){
digitalWrite(bluePin,HIGH); // blue
} else
{
digitalWrite(bluePin,LOW); }
if (soilsensorValue < 256){
digitalWrite(redPin,HIGH); // red
} else {digitalWrite(redPin,LOW); }
switch (rangeLight)
{
case 0:
Serial.println("dark");
break;
case 1:
Serial.println("dim");
break;
case 2:
Serial.println("medium");
break;
case 3:
Serial.println("bright");
break;
}
// The following is commented out because otherwise the code doesn't run
//switch (rangeLight)
// {
// case 0:
// lcd.print("dark");
// break;
//
// case 1:
// lcd.print("dim");
// break;
//
// case 2:
// lcd.print("medium");
// break;
//
// case 3:
// lcd.print("bright");
// break;
delay(5000);
}
int convertTosoilpercent(int value)
{
int soilpercentValue = 0;
soilpercentValue = map(value, 0, 582, 0, 100);
return soilpercentValue;
}
void printValuesToSerial()
{
Serial.print("\nCurrent Date / Time: ");
Serial.print(myRTC.dayofmonth); //You can switch between day and month if you're using American system
Serial.print("/");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);
Serial.print("Soil Analog Value: ");
Serial.print(soilsensorValue);
Serial.print("\nSoil Humidity: ");
Serial.print(soilpercent);
Serial.print("%");
Serial.print("\nTemperature F = ");
Serial.println((DHT.temperature*1.8)+32);
Serial.print("Temperature C = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
lcd.print("Date: ");
lcd.print(myRTC.dayofmonth); //You can switch between day and month if you're using American system
lcd.print("/");
lcd.print(myRTC.month);
lcd.print("/");
lcd.print(myRTC.year);
lcd.setCursor(0,1);
lcd.print("Time: ");
lcd.print(myRTC.hours);
lcd.print(":");
lcd.print(myRTC.minutes);
lcd.print(":");
lcd.print(myRTC.seconds);
delay(3000);
lcd.clear();
lcd.print("Soil Hum: ");
lcd.print(soilpercent);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print("Air Hum: ");
lcd.print(DHT.humidity);
delay(3000);
lcd.clear();
lcd.print("Temp F = ");
lcd.print((DHT.temperature*1.8)+32);
lcd.setCursor(0,1);
lcd.print("Temp C = ");
lcd.print(DHT.temperature);
delay(3000);
lcd.clear();
}