Advertisement
Zoltar358

Clock-Temp-Humid

Nov 9th, 2020
2,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.00 KB | None | 0 0
  1. /*
  2.   Weather
  3.  
  4.   Displays current date and time using RTC DS1307 module.
  5.   Measures temperature and humidity using DHT22 sensor.
  6.   Data is then displayed on LCD Display JHD162A-YG.
  7.   Pressing blue button will change to Celcius and humidity.
  8.   Pressing red button will change to Fahrenheit and humidity.
  9.   Pressing green button will change to Kelvin and humidity.
  10.  
  11.   Created:  2020-11-09 by Zoltar358.
  12.   Modified: 2020-11-09 by Zoltar358.
  13.   Tested on Arduino Nano Every.
  14.  
  15.   IMPORTANT:
  16.   Requires editing of ~/Arduino/libraries/DHT_sensor_library/DHT.cpp
  17.   After  #include "DHT.h" insert following line of code:
  18.   #define SystemCoreClock 16000000UL
  19.  
  20.   This code is public domain.
  21. */
  22.  
  23.  
  24. // include the libraries
  25. #include <Wire.h>                              
  26. #include <LiquidCrystal.h>                        // LCD library
  27. #include <DHT.h>                                  // DHT library
  28. #include <SoftwareSerial.h>                       // Serial library
  29. #include <TimeLib.h>                              // Time library
  30. #include <DS1307RTC.h>                            // RTC library
  31.  
  32. const char *monthName[12] = {
  33.   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  34.   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  35. };
  36.  
  37. tmElements_t tm;
  38.  
  39. // Define degree symbol
  40. byte degree_symbol[8] =
  41.               {
  42.                 0b00111,
  43.                 0b00101,
  44.                 0b00111,
  45.                 0b00000,
  46.                 0b00000,
  47.                 0b00000,
  48.                 0b00000,
  49.                 0b00000
  50.               };
  51.  
  52. #define DHTPIN 7                                  // DHT-22 Output Pin connection
  53. #define DHTTYPE DHT22                             // DHT Type is DHT 22 (AM2302)
  54.  
  55. DHT dht(DHTPIN, DHTTYPE);                         // Setup DHT sensor for normal 16mhz Arduino
  56.  
  57. // initialize the library by associating any needed LCD interface pin
  58. // with the arduino pin number it is connected to
  59. const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
  60. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  61.  
  62. // Define Variables
  63. float hum;                                        // Stores humidity value in percent
  64. float tempC;                                      // Stores temperature value in Celcius
  65. float tempF;                                      // Stores temperature value in Fahrenheit
  66. float tempK;                                      // Stores temperature value in Kelvin
  67. int buttonStateBlue;                              // Stores blue button state value
  68. int buttonStateRed;                               // Stores red button state value
  69. int buttonStateGreen;                             // Stores green button state value
  70.  
  71. // Define constants
  72. const int buttonPinBlue = 10;                     // The number of blue button pin
  73. const int buttonPinRed = 9;                       // The number of red button pin
  74. const int buttonPinGreen = 8;                     // The number of green button pin
  75.  
  76. // Setup (runs once)
  77. void setup() {
  78.   bool parse=false;
  79.   bool config=false;
  80.  
  81.   // get the date and time the compiler was run
  82.   if (getDate(__DATE__) && getTime(__TIME__)) {
  83.     parse = true;
  84.     // and configure the RTC with this info
  85.     if (RTC.write(tm)) {
  86.       config = true;
  87.     }
  88.   }
  89.  
  90.   pinMode(buttonPinBlue, INPUT);                  // Initialize blue button pin as an input
  91.   pinMode(buttonPinRed, INPUT);                   // Initialize red button pin as an input
  92.   pinMode(buttonPinGreen, INPUT);                 // Initialize green button pin as an input
  93.   Serial.begin(115200);                           // Initialize serial connection
  94.   lcd.begin(16, 2);                               // Set up the LCD's number of columns and rows
  95.   dht.begin();                                    // Initialize DHT-22
  96.   lcd.createChar(1, degree_symbol);
  97. }
  98.  
  99. // Loop (runs continuously)
  100. void loop() {
  101.   delay(1000);                                    // Delay so DHT-22 sensor can stabalize
  102.   lcd.clear();                                    // Clear the display
  103.   hum = dht.readHumidity();                       // Get humidity value
  104.   tempC = dht.readTemperature();                  // Get temperature value
  105.   tempF=tempC*9/5+32;                             // Converting Celcius to Fahrenheit
  106.   tempK=tempC+273.15;                             // Converting Celcius to Kelvin
  107.   buttonStateBlue = digitalRead(buttonPinBlue);   // Read the state of blue button
  108.   buttonStateRed = digitalRead(buttonPinRed);     // Read the state of red button
  109.   buttonStateGreen = digitalRead(buttonPinGreen); // Read the state of green button
  110.  
  111.   tmElements_t tm;
  112.  
  113.   if (RTC.read(tm)) {
  114.     lcd.setCursor(3,0);                           // Set cursor location
  115.     lcd.print(tmYearToCalendar(tm.Year));         // Print year
  116.     lcd.print('-');                               // Print separator
  117.     print2digits(tm.Month);                       // Print month
  118.     lcd.print('-');                               // Print separator
  119.     print2digits(tm.Day);                         // Print day
  120.  
  121.     lcd.setCursor(4,1);                           // Set cursor location
  122.     print2digits(tm.Hour);                        // Print hour
  123.     lcd.print(':');                               // Print separator
  124.     print2digits(tm.Minute);                      // Print minute
  125.     lcd.print(':');                               // Print separator
  126.     print2digits(tm.Second);                      // Print second
  127.   }
  128.  
  129.   // lcd.setCursor(2,0);                          // Set cursor location
  130.   // lcd.print(__DATE__);                         // Print date
  131.   // lcd.setCursor(4,1);                          // Set cursor location
  132.   // lcd.print(__TIME__);                         // Print time
  133.  
  134.   // Check if blue button is pressed. If it is, the buttonStateBlue is HIGH
  135.   if (buttonStateBlue == HIGH) {
  136.       // Display temperature in Celcius
  137.       lcd.setCursor(1, 0);                        // Set cursor location
  138.       lcd.print("Temp:  ");                       // Print "Temp:  " on top line
  139.       lcd.print(tempC);                           // Print tempC value
  140.       lcd.setCursor(13,0);                        // Set cursor location
  141.       lcd.write(1);                               // Print degree symbol
  142.       lcd.print("C");                             // Print C symbol
  143.       lcd.setCursor(1, 1);                        // Set cursor location
  144.       lcd.print("Humid: ");                       // Print "Humid: " on bottom line
  145.       lcd.print(hum);                             // Print humidity value
  146.       lcd.print(" %");                            // Print % symbol
  147.   }
  148.  
  149.   // Check if red button is pressed. If it is, the buttonStateRed is HIGH
  150.   if (buttonStateRed == HIGH) {
  151.       // Display temperature in Fahrenheit
  152.       lcd.setCursor(1, 0);                        // Set cursor location
  153.       lcd.print("Temp:  ");                       // Print "Temp:  " on top line
  154.       lcd.print(tempF);                           // Print tempF value
  155.       lcd.setCursor(13,0);                        // Print degree symbol in column 13
  156.       lcd.write(1);                               // Print degree symbol
  157.       lcd.print("F");                             // Print F symbol
  158.       lcd.setCursor(1, 1);                        // Move cursor to bottom line
  159.       lcd.print("Humid: ");                       // Print "Humid: " on bottom line
  160.       lcd.print(hum);                             // Print humidity value
  161.       lcd.print(" %");                            // Print % symbol
  162.   }
  163.  
  164.   // Check if green button is pressed. If it is, the buttonStateGreen is HIGH
  165.   if (buttonStateGreen == HIGH) {
  166.       // Display temperature in Kelvin
  167.       lcd.setCursor(1, 0);                        // Set cursor location
  168.       lcd.print("Temp: ");                        // Print "Temp:  " on top line
  169.       lcd.print(tempK);                           // Print tempK value
  170.       lcd.setCursor(13,0);                        // Set cursor location
  171.       lcd.print(" K");                            // Print K symbol
  172.       lcd.setCursor(1, 1);                        // Set cursor location
  173.       lcd.print("Humid: ");                       // Print "Humid: " on bottom line
  174.       lcd.print(hum);                             // Print humidity value
  175.       lcd.print(" %");                            // Print % symbol
  176.   }
  177. }
  178.  
  179. bool getTime(const char *str) {
  180.   int Hour, Min, Sec;
  181.  
  182.   if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  183.   tm.Hour = Hour;
  184.   tm.Minute = Min;
  185.   tm.Second = Sec;
  186.   return true;
  187. }
  188.  
  189. bool getDate(const char *str) {
  190.   char Month[12];
  191.   int Day, Year;
  192.   uint8_t monthIndex;
  193.  
  194.   if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  195.   for (monthIndex = 0; monthIndex < 12; monthIndex++) {
  196.     if (strcmp(Month, monthName[monthIndex]) == 0) break;
  197.   }
  198.   if (monthIndex >= 12) return false;
  199.   tm.Day = Day;
  200.   tm.Month = monthIndex + 1;
  201.   tm.Year = CalendarYrToTm(Year);
  202.   return true;
  203. }
  204.  
  205. void print2digits(int number) {
  206.   if (number >= 0 && number < 10) {
  207.     lcd.print('0');
  208.   }
  209.   lcd.print(number);
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement