/* Sketch to display day of week, month, day of month, and time,on LCD line 1, and humidity and temperature on LCD line 2. Written by R. Jordan Kreindler, July 2016 NOTE: Uses an LCD Shield with buttons. With the press of the ‘Left’ button the LCD backlight turns off and with another press of this button the backlight turns back on. */ #include #include "RTClib.h" RTC_DS1307 RTC; // Include the real time clock header for the DS3231 // and define a DS3231 object #include // define a DS3231 object rtc; the DS3231 uses I2C interface DS3231 rtc(A4, A5); // SDA to A4, and SCL to A5 //Include the humidity and sensor header #include DHT dht(15, DHT22);// Define a DHT object, dht // Include LiquidCrystal library #include // and define a LiquidCrystal object LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Sets the numbers for the relevant pins connected on the Arduino // The LCD shield uses digital pins 4 through 9, pin 10 is used for backlight // and analog pin A0(for the buttons) int x = 0; int humidity = 0; // Holds humidity results as an integer float cTemp = 0.0; // Holds Centigrade temerature results as a float int centigradeTemp = 0; // The Centigrade temperature after conversion to int int fahrenheitTemp = 0; // Will hold the converted to Fahenheit temperature int i = 0; // For loop index int delay1 = 700; // The time between text displays boolean backlightOn = true; // Holds state of backlight int adcValueRead = 0; // Start with a value in adcValueRead char rr, ss, tt, uu, vv, ww, xx; void setup(){ pinMode(10, OUTPUT); // Set pin 10 for output to turn backlight on/off digitalWrite(10, HIGH); // Start with LCD backlight on dht.begin(); //Starts the DHT22 sensor //The LCD display has 16 columns (0 to 15) and 2 rows (0 and 1) lcd.begin(16, 2); // Identify this // Set parameters for the RTC object to use and start the // real time clock (RTC) rtc.begin(); // Set the current date/time here: year,month,day,hour,minutes,seconds RTC.adjust(DateTime(2016,07,31,19,20,00)); } void loop() { lcd.setCursor(0, 0); // Display DOW string in format Ddd rr = rtc.getDOWStr()[0]; ss = rtc.getDOWStr()[1]; tt = rtc.getDOWStr()[2]; lcd.print(rr); lcd.print(ss); lcd.print(tt); // Display date in mm/dd format // mm format uu = rtc.getDateStr()[3]; vv = rtc.getDateStr()[4]; lcd.setCursor(4, 0); lcd.print(uu); lcd.print(vv); lcd.print("/"); // dd format ww = rtc.getDateStr()[0]; xx = rtc.getDateStr()[1]; lcd.setCursor(7, 0); lcd.print(ww); lcd.print(xx); lcd.print(" "); // Display time in hh:mm:ss format lcd.setCursor(11, 0); lcd.print(rtc.getTimeStr()); // ***** For DHT22 Sensor lcd.setCursor(0, 1); lcd.print(" RH:"); lcd.print(round(dht.readHumidity())); lcd.print("% "); cTemp = dht.readTemperature(); // Uncomment below for centigrade temperature // centigradeTemp = round(cTemp); // Comment out the Line below for centigrade temperature and // Uncomment the lines for Centigrade temperature below those fahrenheitTemp = round(cTemp * 9.0 / 5.0 + 32.0); lcd.print("F:"); lcd.print(fahrenheitTemp); lcd.print((char)223); // lcd.print("C:"); // lcd.print(centigradeTemp); // lcd.print("((char)223"); adcValueRead = analogRead(14); // Check if any buttons pressed if ( (adcValueRead >= 400 && adcValueRead <= 500) && backlightOn == true) { lcd.clear(); lcd.print("Powering off "); for (int i = 1; i <= 3; i++) { lcd.print("."); delay(750); } digitalWrite(10, LOW); // Set LCD backlight off backlightOn = false; adcValueRead = 5000; } if ( (adcValueRead >= 400 && adcValueRead <= 500) && backlightOn == false ) { digitalWrite(10, HIGH); // Set LCD backlight on lcd.clear(); lcd.print("Powering on "); for (int i = 1; i <= 3; i++) { lcd.print("."); delay(750); } backlightOn = true; lcd.clear(); lcd.setCursor(0, 0); } }