Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Weather
- Displays current date and time using RTC DS1307 module.
- Measures temperature and humidity using DHT22 sensor.
- Data is then displayed on LCD Display JHD162A-YG.
- Pressing blue button will change to Celcius and humidity.
- Pressing red button will change to Fahrenheit and humidity.
- Pressing green button will change to Kelvin and humidity.
- Created: 2020-11-09 by Zoltar358.
- Modified: 2020-11-09 by Zoltar358.
- Tested on Arduino Nano Every.
- IMPORTANT:
- Requires editing of ~/Arduino/libraries/DHT_sensor_library/DHT.cpp
- After #include "DHT.h" insert following line of code:
- #define SystemCoreClock 16000000UL
- This code is public domain.
- */
- // include the libraries
- #include <Wire.h>
- #include <LiquidCrystal.h> // LCD library
- #include <DHT.h> // DHT library
- #include <SoftwareSerial.h> // Serial library
- #include <TimeLib.h> // Time library
- #include <DS1307RTC.h> // RTC library
- const char *monthName[12] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
- };
- tmElements_t tm;
- // Define degree symbol
- byte degree_symbol[8] =
- {
- 0b00111,
- 0b00101,
- 0b00111,
- 0b00000,
- 0b00000,
- 0b00000,
- 0b00000,
- 0b00000
- };
- #define DHTPIN 7 // DHT-22 Output Pin connection
- #define DHTTYPE DHT22 // DHT Type is DHT 22 (AM2302)
- DHT dht(DHTPIN, DHTTYPE); // Setup DHT sensor for normal 16mhz Arduino
- // initialize the library by associating any needed LCD interface pin
- // with the arduino pin number it is connected to
- const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- // Define Variables
- float hum; // Stores humidity value in percent
- float tempC; // Stores temperature value in Celcius
- float tempF; // Stores temperature value in Fahrenheit
- float tempK; // Stores temperature value in Kelvin
- int buttonStateBlue; // Stores blue button state value
- int buttonStateRed; // Stores red button state value
- int buttonStateGreen; // Stores green button state value
- // Define constants
- const int buttonPinBlue = 10; // The number of blue button pin
- const int buttonPinRed = 9; // The number of red button pin
- const int buttonPinGreen = 8; // The number of green button pin
- // Setup (runs once)
- void setup() {
- bool parse=false;
- bool config=false;
- // get the date and time the compiler was run
- if (getDate(__DATE__) && getTime(__TIME__)) {
- parse = true;
- // and configure the RTC with this info
- if (RTC.write(tm)) {
- config = true;
- }
- }
- pinMode(buttonPinBlue, INPUT); // Initialize blue button pin as an input
- pinMode(buttonPinRed, INPUT); // Initialize red button pin as an input
- pinMode(buttonPinGreen, INPUT); // Initialize green button pin as an input
- Serial.begin(115200); // Initialize serial connection
- lcd.begin(16, 2); // Set up the LCD's number of columns and rows
- dht.begin(); // Initialize DHT-22
- lcd.createChar(1, degree_symbol);
- }
- // Loop (runs continuously)
- void loop() {
- delay(1000); // Delay so DHT-22 sensor can stabalize
- lcd.clear(); // Clear the display
- hum = dht.readHumidity(); // Get humidity value
- tempC = dht.readTemperature(); // Get temperature value
- tempF=tempC*9/5+32; // Converting Celcius to Fahrenheit
- tempK=tempC+273.15; // Converting Celcius to Kelvin
- buttonStateBlue = digitalRead(buttonPinBlue); // Read the state of blue button
- buttonStateRed = digitalRead(buttonPinRed); // Read the state of red button
- buttonStateGreen = digitalRead(buttonPinGreen); // Read the state of green button
- tmElements_t tm;
- if (RTC.read(tm)) {
- lcd.setCursor(3,0); // Set cursor location
- lcd.print(tmYearToCalendar(tm.Year)); // Print year
- lcd.print('-'); // Print separator
- print2digits(tm.Month); // Print month
- lcd.print('-'); // Print separator
- print2digits(tm.Day); // Print day
- lcd.setCursor(4,1); // Set cursor location
- print2digits(tm.Hour); // Print hour
- lcd.print(':'); // Print separator
- print2digits(tm.Minute); // Print minute
- lcd.print(':'); // Print separator
- print2digits(tm.Second); // Print second
- }
- // lcd.setCursor(2,0); // Set cursor location
- // lcd.print(__DATE__); // Print date
- // lcd.setCursor(4,1); // Set cursor location
- // lcd.print(__TIME__); // Print time
- // Check if blue button is pressed. If it is, the buttonStateBlue is HIGH
- if (buttonStateBlue == HIGH) {
- // Display temperature in Celcius
- lcd.setCursor(1, 0); // Set cursor location
- lcd.print("Temp: "); // Print "Temp: " on top line
- lcd.print(tempC); // Print tempC value
- lcd.setCursor(13,0); // Set cursor location
- lcd.write(1); // Print degree symbol
- lcd.print("C"); // Print C symbol
- lcd.setCursor(1, 1); // Set cursor location
- lcd.print("Humid: "); // Print "Humid: " on bottom line
- lcd.print(hum); // Print humidity value
- lcd.print(" %"); // Print % symbol
- }
- // Check if red button is pressed. If it is, the buttonStateRed is HIGH
- if (buttonStateRed == HIGH) {
- // Display temperature in Fahrenheit
- lcd.setCursor(1, 0); // Set cursor location
- lcd.print("Temp: "); // Print "Temp: " on top line
- lcd.print(tempF); // Print tempF value
- lcd.setCursor(13,0); // Print degree symbol in column 13
- lcd.write(1); // Print degree symbol
- lcd.print("F"); // Print F symbol
- lcd.setCursor(1, 1); // Move cursor to bottom line
- lcd.print("Humid: "); // Print "Humid: " on bottom line
- lcd.print(hum); // Print humidity value
- lcd.print(" %"); // Print % symbol
- }
- // Check if green button is pressed. If it is, the buttonStateGreen is HIGH
- if (buttonStateGreen == HIGH) {
- // Display temperature in Kelvin
- lcd.setCursor(1, 0); // Set cursor location
- lcd.print("Temp: "); // Print "Temp: " on top line
- lcd.print(tempK); // Print tempK value
- lcd.setCursor(13,0); // Set cursor location
- lcd.print(" K"); // Print K symbol
- lcd.setCursor(1, 1); // Set cursor location
- lcd.print("Humid: "); // Print "Humid: " on bottom line
- lcd.print(hum); // Print humidity value
- lcd.print(" %"); // Print % symbol
- }
- }
- bool getTime(const char *str) {
- int Hour, Min, Sec;
- if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
- tm.Hour = Hour;
- tm.Minute = Min;
- tm.Second = Sec;
- return true;
- }
- bool getDate(const char *str) {
- char Month[12];
- int Day, Year;
- uint8_t monthIndex;
- if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
- for (monthIndex = 0; monthIndex < 12; monthIndex++) {
- if (strcmp(Month, monthName[monthIndex]) == 0) break;
- }
- if (monthIndex >= 12) return false;
- tm.Day = Day;
- tm.Month = monthIndex + 1;
- tm.Year = CalendarYrToTm(Year);
- return true;
- }
- void print2digits(int number) {
- if (number >= 0 && number < 10) {
- lcd.print('0');
- }
- lcd.print(number);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement