/*
  Voltaat learn (http://learn.voltaat.com)
  Link for full tutorial:
  DHT11 library:
  Adafruit sensor Library:
  Wire Library:
  LiquidCrystal I2C Library:
  Tutorial: Simple Weather Station with LCD Display
  
  Connections from the Arduino to the breadboard:
  
  • Arduino 5v pin → breadboard 5v line
  • Arduino GND pin → breadboard GND line
  Connection form the DHT11:
  • GND pin → Arduino GND pin
  • Signal pin → Arduino digital pin 2
  • VCC pin → Arduino 5V pin
  Connection from the raindrops sensor:
  • VCC pin → breadboard 5v line
  • GND pin → breadboard GND line
  • Signal pin → Arduino analog pin A0
  Connection from the LCD:
  • VCC pin → breadboard 5v line
  • GND pin→ breadboard GND line
  • SDA pin→ Arduino analog pin A4
  • SCL pin → Arduino analog pin A5
*/
// Define libraries
#include "Wire.h" // include the Wire library for I2C communication
#include "LiquidCrystal_I2C.h" // include the LiquidCrystal_I2C library for the LCD display
#include "DHT.h" // include the DHT library for the DHT11 sensor
const int rainSensorPin = A0; // The analog pin that the rain sensor is connected to
DHT dht (2, DHT11); // Initialize the DHT11 sensor on digital pin 2
const float heavyRainfallThreshold = 2.0; // Threshold for heavy rainfall
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the LCD display
float prevT = -999.0; // Initialize previous temperature to an invalid value
float prevH = -999.0; // Initialize previous humidity to an invalid value
bool prevRain = false; // Initialize previous rain value to false
//Commands inside void setup run once
void setup() {
  Serial.begin(9600); // Initialize serial communication at 9600 bits per second
  lcd.init(); // Initialize the LCD display
  lcd.backlight(); // Turn on the backlight
  dht.begin(); // Start the DHT11 sensor
  pinMode(rainSensorPin, INPUT); // Set the rain sensor pin as an input
  lcd.clear(); // Clear the LCD display
  lcd.setCursor(1, 0); // Set the cursor to the first row, second column
  lcd.print("Voltaat Learn"); // Display a welcome message
  delay(3000); // Wait for 3 seconds
  lcd.clear(); // Clear the LCD display
  lcd.setCursor(3, 0); // Set the cursor to the first row, fourth column
  lcd.print("Welcome to"); // Display a welcome message
  lcd.setCursor(1, 1); // Set the cursor to the second row, second column
  lcd.print("Weather Station"); // Display a welcome message
  delay(3000); // Wait for 3 seconds
}
//Commands inside void loop run forever
void loop() {
  float T = dht.readTemperature(); // Read the temperature from the DHT11 sensor
  float H = dht.readHumidity(); // Read the humidity from the DHT11 sensor
  int rainSensorValue = analogRead(rainSensorPin); // Read the value from the rain sensor
  bool isRaining = rainSensorValue < 500; // Determine if it is currently raining
  // Only update the display if the values have changed
  if (T != prevT || H != prevH || isRaining != prevRain) {
    lcd.setCursor(2, 0); // Set the cursor to the first row, third column
    lcd.print("Temp: "); // Display the temperature label
    lcd.print(T); // Display the temperature
    lcd.print((char)223); // Display the degree symbol
    lcd.print("C"); // Display the Celsius unit
    lcd.setCursor(0, 1); // Set the cursor to the second row, first column
    if (isRaining) { // If it is currently raining
      lcd.print("Raining "); // Display the rain status
    } else { // If it is not currently raining
      lcd.print("No Rain "); // Display the rain status
    }
    lcd.print("H:"); // Display the humidity label
    lcd.print(H); // Display the humidity
    lcd.print("%"); // Display the percentage symbol
    // Store the current values as previous values for the next update
    prevT = T;
    prevH = H;
    prevRain = isRaining;
  }
  delay(2000); // Wait for 2 seconds
}