// Libraries
#include <dht.h>
#include <LiquidCrystal_I2C.h>

dht DHT;
#define DHT22_PIN 2               // DHT22 connected to pin 2
#define RED_PIN 3
#define GREEN_PIN 5
#define BLUE_PIN 6
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD address 0x27, 20x4 display

// Variables
float RH;      // Relative humidity (%)
float c_temp;   // Temperature in Celcius
float c_HI;     // Heat index in Celcius

// Calculations
float calculateHeatIndex(float c_temp, float RH){
    // Celcius to Fahrenheit
    float f_temp = (c_temp * 9/5) + 32;

    // Initial Heat Index calculation using simple formula
    float f_HI = 0.5 * (f_temp + 61.0 + ((f_temp - 68.0) * 1.2) + (RH * 0.094));

    // Only apply complex heat index formula if temperature is 80°F or higher
    if (f_HI >= 80) {
        // Apply Rothfusz regretion
        f_HI = -42.379 + 2.04901523*f_temp + 10.14333127*RH
              - 0.22475541*f_temp*RH - 0.00683783*f_temp*f_temp 
              - 0.05481717*RH*RH + 0.00122874*f_temp*f_temp*RH
              + 0.00085282*f_temp*RH*RH - 0.00000199*f_temp*f_temp*RH*RH;
                     
      // Adjustment for low humidity
        if (RH < 13 && f_temp >= 80 && f_temp <= 112) {
          float adjustment = ((13 - RH) / 4) * sqrt((17 - abs(f_temp - 95)) / 17);
          f_HI -= adjustment;
        }

        if (RH > 85 && f_temp >= 80 && f_temp <= 87) {
          float adjustment = ((RH - 85) / 10) * ((87 - f_temp) / 5);
          f_HI += adjustment;
        }
    }      
    return (f_HI - 32) * 5 / 9;
}

void setup(){
    Serial.begin(9600);
    lcd.init();                      // Initialize the LCD
    lcd.backlight();                 // Turn on the backlight
    pinMode(RED_PIN, OUTPUT);
    pinMode(GREEN_PIN, OUTPUT);
    pinMode(BLUE_PIN, OUTPUT);
}

void loop()
{
    int chk = DHT.read22(DHT22_PIN); // Read the DHT sensor data
    RH = DHT.humidity;
    c_temp = DHT.temperature;

    c_HI = calculateHeatIndex(c_temp, RH);

    // Display temperature on the first line
    lcd.setCursor(0, 0);
    lcd.print("Temp: ");
    lcd.print(c_temp, 1);              // 1 decimal place
    lcd.print(" C ");

    // Display humidity on the second line
    lcd.setCursor(0, 1);
    lcd.print("Humidity: ");
    lcd.print(RH, 1);               // 1 decimal place
    lcd.print(" %");

    // Display Heat Index on the third line
    lcd.setCursor(0,2);
    lcd.print("Heat Index: ");
    lcd.print(c_HI, 1);           // 1 decimal place
    lcd.print(" C");


    // Display Heat Index status on the third line
    lcd.setCursor(0, 3);
    if (c_HI >= 27.0 && c_HI <= 32.0) {
    lcd.print("Caution           ");  // 20 characters
    analogWrite(RED_PIN, 0);
    analogWrite(GREEN_PIN, 0);
    analogWrite(BLUE_PIN, 153);
} else if (c_HI > 32.0 && c_HI <= 41.0) {
    lcd.print("Extreme caution   ");
    analogWrite(RED_PIN, 0);
    analogWrite(GREEN_PIN, 40);
    analogWrite(BLUE_PIN, 255);
} else if (c_HI > 41.0 && c_HI <= 54.0) {
    lcd.print("Danger            ");
    analogWrite(RED_PIN, 0);
    analogWrite(GREEN_PIN, 115);
    analogWrite(BLUE_PIN, 255);
} else if (c_HI > 54.0) {
    lcd.print("Extreme danger    ");
    analogWrite(RED_PIN, 0);
    analogWrite(GREEN_PIN, 255);
    analogWrite(BLUE_PIN, 255);
} else {
    lcd.print("Safe              ");
    analogWrite(RED_PIN, 255);
    analogWrite(GREEN_PIN, 0);
    analogWrite(BLUE_PIN, 255);
}

    delay(50); // Delay between readings
}
$abcdeabcde151015202530fghijfghij
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
rgb1:R
rgb1:COM
rgb1:G
rgb1:B
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND