#include <TimeLib.h> #include <TimeAlarms.h> #include <SD.h> #include <EEPROM.h> #include <RTClib.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <LiquidMenu.h> #include <DHT.h> #include "Button.h"
#define dht1_apin A0 //Analog sensor pin is connected to. #define DHT1TYPE DHT11 #define Light1Pin 7 #define Light2Pin 6 const int chipSelect = 53;
DHT dht1(dht1_apin, DHT1TYPE); LiquidCrystal_I2C lcd(0x27, 20, 4); RTC_DS1307 rtc; File dataFile;
// Button objects instantiation const bool pullup = true; Button left(8, pullup); Button right(9, pullup); Button up(10, pullup); Button down(11, pullup); Button enter(12, pullup);
// The analog reading sample period in seconds. // It is later overwritten by it's EEPROM value. unsigned short sample_period = 2;
// Text used for indication for the save lines. char* input_saved; char* output_saved; int printTime(); const char* light1State(); const char* light2State(); int lightOn; int lightOff; int humOn; int relayPins[] = { 6,7,5,4 };
enum FunctionTypes { increase = 1, decrease = 2, };
//temp/hum variables float t1; float h1;
// A LiquidLine object can be used more that once. LiquidLine back_line(11, 1, "/BACK"); LiquidLine timeC(0, 0, printTime);
LiquidLine welcome_line1(1, 1, " Arduino ", LIQUIDMENU_VERSION); LiquidLine welcome_line2(1, 2, "Garden Automater"); LiquidScreen welcome_screen(welcome_line1, welcome_line2);
// These lines direct to other menus. LiquidLine Lights(0, 1, "Lights>"); LiquidLine TempHum(0, 2, "Temp/Hum>"); LiquidScreen io_screen(Lights, TempHum, timeC);
// This is the first menu. LiquidMenu main_menu(lcd, welcome_screen, io_screen, 1);
LiquidLine Light1(0, 1, "Light1: ", light1State); LiquidLine Light2(0, 2, "Light2: ", light2State); LiquidScreen light_screen(Light1, Light2, timeC);
LiquidLine lightOn_line(0, 1, "Time ON: ", lightOn); LiquidLine lightOff_line(0, 2, "Time OFF: ", lightOff); LiquidScreen oSecondary_screen(timeC, lightOn_line, lightOff_line, back_line);
// This is the second menu. LiquidMenu Lights_menu(lcd, light_screen, oSecondary_screen);
LiquidLine temp1_line(0, 0, "Temp1: ", t1); LiquidLine hum1_line(0, 1, "Hum1: ", h1); LiquidScreen temp_screen(temp1_line, hum1_line);
LiquidLine hum_line(0, 0, "HumOn: ", humOn, "%"); LiquidLine iSample_line(0, 1, "Sample: ", sample_period, "s"); LiquidLine iSave_line(0, 2, "Save", input_saved); LiquidScreen iSecondary_screen(hum_line, iSample_line, iSave_line, back_line);
// And this is the final third menu. LiquidMenu TempHum_menu(lcd, temp_screen, iSecondary_screen);
/* * LiquidSystem object combines the LiquidMenu objects to form * a menu system. It provides the same functions as LiquidMenu * with the addition of add_menu() and change_menu(). */ LiquidSystem menu_system(main_menu, Lights_menu, TempHum_menu);
// Checks all the buttons. void buttonsCheck() { if (right.check() == LOW) { menu_system.next_screen(); } if (left.check() == LOW) { menu_system.previous_screen(); } if (up.check() == LOW) { menu_system.call_function(increase); } if (down.check() == LOW) { menu_system.call_function(decrease); } if (enter.check() == LOW) { menu_system.switch_focus(); } }
// Callback function that will be attached to back_line. void go_back() { // This function takes reference to the wanted menu. menu_system.change_menu(main_menu); }
void goto_Lights_menu() { menu_system.change_menu(Lights_menu); }
void goto_TempHum_menu() { menu_system.change_menu(TempHum_menu); }
void on_light1() { if (digitalRead(Light1Pin)) { digitalWrite(Light1Pin, LOW); } else { digitalWrite(Light1Pin, HIGH); } output_saved = (char*)" "; }
void on_light2() { if (digitalRead(Light2Pin)) { digitalWrite(Light2Pin, LOW); } else { digitalWrite(Light2Pin, HIGH); } output_saved = (char*)" "; }
void off_light1() { if (digitalRead(Light1Pin)) { digitalWrite(Light1Pin, HIGH); } else { digitalWrite(Light1Pin, LOW); } output_saved = (char*)" "; }
void off_light2() { if (digitalRead(Light2Pin)) { digitalWrite(Light2Pin, HIGH); } else { digitalWrite(Light2Pin, LOW); } output_saved = (char*)" "; }
void save_input() { EEPROM.put(11, sample_period); input_saved = (char*)" *"; }
void increase_lightsOn() { if (lightOn <= 24) { lightOn++; } else { lightOn = 24; } }
void decrease_lightsOn() { if (lightOn > 0) { lightOn--; } else { lightOn = 0; } }
void increase_lightsOff() { if (lightOff <= 24) { lightOff++; } else { lightOff = 24; } }
void decrease_lightsOff() { if (lightOff > 0) { lightOff--; } else { lightOff = 0; } }
void increase_samplePeriod() { if (sample_period <= 100) { sample_period++; input_saved = (char*)" "; } else { sample_period = 100; } }
void decrease_samplePeriod() { if (sample_period > 0) { sample_period--; input_saved = (char*)" "; } else { sample_period = 0; } }
void increase_hum() { if (humOn <= 100) { humOn++; } else { humOn = 100; } }
void decrease_hum() { if (humOn > 0) { humOn--; } else { humOn = 0; } }
void setup() { Serial.begin(9600); dht1.begin();
for (int r = 0; r < 4; r++) { pinMode(relayPins[r], OUTPUT); digitalWrite(relayPins[r], HIGH); } // Reads the values recorded in the EEPROM EEPROM.get(11, sample_period); lcd.begin(20, 4); lcd.init(); lcd.backlight();
back_line.set_focusPosition(Position::RIGHT);
back_line.attach_function(1, go_back); back_line.attach_function(2, go_back);
Lights.attach_function(1, goto_Lights_menu); Lights.attach_function(2, goto_Lights_menu); TempHum.attach_function(1, goto_TempHum_menu); TempHum.attach_function(2, goto_TempHum_menu);
Light1.attach_function(increase, on_light1); Light1.attach_function(decrease, off_light1); Light2.attach_function(increase, on_light2); Light2.attach_function(decrease, off_light2);
lightOn_line.attach_function(increase, increase_lightsOn); lightOn_line.attach_function(decrease, decrease_lightsOn); lightOff_line.attach_function(increase, increase_lightsOff); lightOff_line.attach_function(decrease, decrease_lightsOff); hum_line.attach_function(increase, increase_hum); hum_line.attach_function(decrease, decrease_hum); iSave_line.attach_function(1, save_input); iSave_line.attach_function(2, save_input); iSample_line.attach_function(increase, increase_samplePeriod); iSample_line.attach_function(decrease, decrease_samplePeriod);
input_saved = (char*)" *"; output_saved = (char*)" *";
if (!rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); }
if (!rtc.isrunning()) { Serial.println("RTC lost power, lets set the time!");
// Comment out below lines once you set the date & time. // Following line sets the RTC to the date & time this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Following line sets the RTC with an explicit date & time // for example to set January 27 2017 at 12:56 you would call: // rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0)); } setSyncProvider(rtc.now); if (timeStatus() != timeSet) Serial.println("Unable to sync with the RTC");
menu_system.update(); }
void loop() { buttonsCheck(); lightProgram(); tempSensor(); waterProgram(); Initialize_SDcard();
static unsigned long lastMillis_sample = 0; if (millis() - lastMillis_sample > (sample_period * 1000)) { lastMillis_sample = millis(); menu_system.update(); } dataRec(); }
int printTime() { DateTime now = rtc.now(); lcd.setCursor(0, 0); lcd.print(now.month(), DEC); lcd.print('/'); lcd.print(now.day(), DEC); lcd.print('/'); lcd.print(now.year(), DEC); lcd.print(' '); lcd.print(now.hour(), DEC); lcd.print(':'); lcd.print(now.minute(), DEC); lcd.print(':'); lcd.print(now.second(), DEC); }
const char* light1State() { if (digitalRead(Light1Pin) == HIGH) { return "ON"; } else { return "OFF"; }
}
const char* light2State() { if (digitalRead(Light2Pin) == HIGH) { return "ON"; } else { return "OFF"; }
} void lightProgram() { DateTime now = rtc.now(); for (int a = 0; a < 2; a++) { if (now.hour() >= lightOn) { Serial.print("Lights on!"); digitalWrite(relayPins[a], LOW); } else if (now.hour() >= lightOff) { Serial.print("Lights off!"); digitalWrite(relayPins[a], HIGH); } } } void waterProgram() { for (int a = 2; a <= 3; a++) { if (h1 >= humOn) { Serial.print("Water On"); digitalWrite(relayPins[a], LOW); } else { Serial.print("Water Off"); digitalWrite(relayPins[a], HIGH); } } } void tempSensor() { h1 = dht1.readHumidity(); t1 = dht1.readTemperature(true);
// check if returns are valid, if they are NaN (not a number) then something went wrong! if (isnan(t1) || isnan(h1)) { Serial.println("Failed to read from DHT #1"); } }
void Initialize_SDcard() { pinMode(chipSelect, OUTPUT); //pinMode(SS, OUTPUT); // see if the card is present and can be initialized: if (!SD.begin(chipSelect)) { Serial.println("Card failed, or not present"); // don't do anything more: return; } Serial.println("SD initialized."); Serial.println(); }
void dataRec() { DateTime now = rtc.now(); static unsigned long lastMillis_sample = 0; h1 = dht1.readHumidity(); t1 = dht1.readTemperature(true); File dataFile = SD.open("data.txt", FILE_WRITE); if (millis() - lastMillis_sample > (sample_period * 1000)) { lastMillis_sample = millis(); if (dataFile) { dataFile.print(now.month(), DEC); dataFile.print('/'); dataFile.print(now.day(), DEC); dataFile.print('/'); dataFile.print(now.year(), DEC); dataFile.print(' '); dataFile.print(now.hour(), DEC); dataFile.print(':'); dataFile.print(now.minute(), DEC); dataFile.print(':'); dataFile.print(now.second(), DEC); dataFile.print(" "); dataFile.print("Temp:"); dataFile.print(t1); dataFile.print(" "); dataFile.print("Hum:"); dataFile.print(h1); dataFile.close(); //delay(10000); } }
} ####You can edit how fast you want the screen to update and log date,time,temp,hum by adjusting the sample rate from 100 seconds to 0seconds.###
Edited by K3yBoardC0WBoy (10/15/19 03:41 PM)
|