/* Sketch to display humidity and temperature on the desktop Written by R. Jordan Kreindler, July 2016 Uses an LCD Shield to display Humidity, as well as Fahrenheit and Centigrade room Temperatures. With the press of the ‘Left’ button the LCD backlight turns off and with another press of this button the backlight turns back on. The DHT22 sensor provides Centigrade temperature values which can be easily converted to Fahrenheit. */ #include #include // The LCD shield uses digital pins 4 through 9 // and analog pin A0(for the buttons) LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Define a LiquidCrystal object, lcd int DHTData = 3; // The data pin from the DHT22 is digital pin 3 DHT dht(DHTData, DHT22);// Define a DHT object, dht 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; // A 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 void centered(int line, char str1[]) { // Written by R. Jordan Kreindler June 2016 int length1 = strlen(str1); int spaces = (15 - length1) / 2.0; lcd.setCursor(0, line); lcd.print(" "); // Should show 16+ spaces between quotes, but Instructable.com removes spaces lcd.setCursor(0, line); for (i = 0; i <= spaces; i++) { lcd.print(" "); } lcd.print(str1); } void setup() { lcd.begin(16, 2); lcd.clear(); pinMode(10, OUTPUT); // Set pin 10 for output to turn backlight on/off digitalWrite(10, HIGH); // Set LCD backlight on dht.begin(); //Starts the DHT22 sensor lcd.setCursor(0, 0); lcd.print("'Left' is On/Off"); } void loop() { humidity = round(dht.readHumidity()); lcd.setCursor(0, 1); lcd.print("RH:"); lcd.print(humidity); lcd.print(" "); cTemp = dht.readTemperature(); centigradeTemp = round(cTemp); fahrenheitTemp = round(cTemp * 9.0 / 5.0 + 32.0); lcd.print("F:"); lcd.print(fahrenheitTemp); lcd.print(" "); lcd.print("C:"); lcd.print(centigradeTemp); lcd.print(" "); 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 < 4; i++) { lcd.print("."); delay(500); } 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 < 4; i++) { lcd.print("."); delay(500); } backlightOn = true; lcd.clear(); lcd.setCursor(0, 0); lcd.print("'Left' is On/Off"); } }