#include <dht.h>
#include <LiquidCrystal.h>
#include <DS3231.h>
//Instantiate library objects
DS3231 rtc(SDA, SCL);
LiquidCrystal lcd(8, 10, 7, 6, 5, 4); // RS,
Time t;
dht DHT;
#define DHT11_PIN 9
//Light timer constants
int startTime = 6; //Integer hour in 24hr format to start lights
int endTime = 22; //Integer hour in 24hr format to cut the lights
int lights = 11; //light digital output pin
//Soil sensor constants
int lcdDelay = 3000; //delay time to refresh the lcd screen with new info
int analogDelay = 150; //delay time to resist EMF interference on analogRead() values
int sensors[] = {A0, A1, A2, A3, A4, A5}; //array of analog soil sensor pins
int sensorLen = 6; //integer length of sensors array
int drySoil = 400; //average sensor output value of known dried soil
//Arduino setup function
void setup() {
lcd.begin(16, 2); //Start lcd in 16x2 mode
Serial.begin(115200);//start serial output at 115200 baud rate
rtc.begin(); //init real time clock module
pinMode(lights,OUTPUT);//set light controller pin to output mode
//set analog pins to INPUT
for (int i = 0; i < sensorLen; i++) {
pinMode(sensors[i],INPUT);
}
}
//main loop
void loop() {
//Declare local loop variables
String(hours);
String rightNow;
int temp_humi = DHT.read11(DHT11_PIN);
//reads in sensor values once per main loop iteration
int sensorVals[6]; //array to store sensor values
for (int i; i < sensorLen; i++){
sensorVals[i] = analogRead(sensors[i]);
delay(analogDelay);
}
//Get current time in HH:MM format
rightNow = rtc.getTimeStr(FORMAT_SHORT);
//Grabs hours then converts to integer for basic hour resolution light timing
hours = rightNow.substring(0,2);
int hours_int = hours.toInt();
//Checks time and controls lights
if (hours_int > startTime and hours_int < endTime)
{
digitalWrite(lights,HIGH);
}
else
{
digitalWrite(lights,LOW);
}
//output to serial monitor for debugging
Serial.println("sensor values: ");
for (int i=0; i < sensorLen; i++) {
Serial.println(sensorVals[i]);
}
//LCD panel
//loop through sensor values and warn if particular plant needs watering.
for (int i=0; i < sensorLen; i++){
if (sensorVals[i] < 400){
lcd.setCursor(0, 0);
lcd.print("Moisture");
lcd.setCursor(8, 0);
lcd.print(i);
lcd.setCursor(9, 0);
lcd.print(": ");
lcd.setCursor(11,0);
lcd.print(sensorVals[i]);
lcd.setCursor(0, 1);
lcd.print("Needs Water!");
delay(lcdDelay);
}
else {
lcd.setCursor(0, 0);
lcd.print("Moisture");
lcd.setCursor(8, 0);
lcd.print(i);
lcd.setCursor(9, 0);
lcd.print(": ");
lcd.setCursor(11,0);
lcd.print(sensorVals[i]);
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.setCursor(6, 1);
lcd.print(rightNow);
delay(lcdDelay);
lcd.clear();
}
}
//Display temp and humidity after outputting sensor vals
lcd.setCursor(0, 0);
lcd.print("Temperature: ");
lcd.setCursor(12, 0);
lcd.print(DHT.temperature);
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.setCursor(10, 1);
lcd.print(DHT.humidity);
delay(lcdDelay);
lcd.clear();
}
Comments