/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Sensor Display"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-06-30 03:20:12

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Sensor DHT22 mengukur suhu dan kelembapan, */
	/* hasilnya ditampilkan pada LCD I2C 1602. LED RGB */
	/* dikontrol melalui pin D3 (merah) dan D4 (hijau): */
	/* hijau untuk suhu rendah, merah untuk suhu tinggi. */
/****** END SYSTEM REQUIREMENTS *****/


/****** DEFINITION OF LIBRARIES *****/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>  //https://github.com/marcoschwartz/LiquidCrystal_I2C
#include <DHT.h>  //https://github.com/adafruit/DHT-sensor-library

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t DHT22_DOUT_PIN_D5 = 5;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t led1_LEDRGB_Red_PIN_D3 = 3;
const uint8_t led1_LEDRGB_Green_PIN_D4 = 4;

/***** DEFINITION OF I2C PINS *****/
const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;  // 0x27 is the hexadecimal representation of 39

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool led1_LEDRGB_Red_PIN_D3_rawData = 0;
bool led1_LEDRGB_Green_PIN_D4_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float led1_LEDRGB_Red_PIN_D3_phyData = 0.0;
float led1_LEDRGB_Green_PIN_D4_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);  // Initialize the LCD with I2C address 0x27, 16 columns, and 2 rows
DHT dht(DHT22_DOUT_PIN_D5, DHT22);  // Initialize the DHT sensor with the specified pin and type (DHT22)

void setup(void)
{
  // put your setup code here, to run once:
  lcd.init();  // Initialize the LCD
  lcd.backlight();  // Turn on the backlight

  pinMode(DHT22_DOUT_PIN_D5, INPUT_PULLUP);
  pinMode(led1_LEDRGB_Red_PIN_D3, OUTPUT);
  pinMode(led1_LEDRGB_Green_PIN_D4, OUTPUT);

  // Initialize the DHT sensor
  dht.begin();

  // Display initial messages on the LCD
  lcd.setCursor(0, 0);
  lcd.print("Hello, world!");
  lcd.setCursor(0, 1);
  lcd.print("Arduino LCD Test");
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  updateOutputs();  // Refresh output data

  // Read humidity and temperature from the DHT sensor
  float humidity = dht.readHumidity();
  float temperatureC = dht.readTemperature();
  float temperatureF = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
    lcd.setCursor(0, 0);
    lcd.print("Failed to read");
    lcd.setCursor(0, 1);
    lcd.print("from DHT sensor!");
    return;
  }

  // Compute heat index
  float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);
  float heatIndexF = dht.computeHeatIndex(temperatureF, humidity);

  // Display the results on the LCD
  lcd.setCursor(0, 0);
  lcd.print("Hum: ");
  lcd.print(humidity);
  lcd.print("%");

  lcd.setCursor(0, 1);
  lcd.print("Temp: ");
  lcd.print(temperatureC);
  lcd.print("C ");
  lcd.print(temperatureF);
  lcd.print("F");

  // Control the RGB LED based on temperature
  if (temperatureC < 25.0) {
    // Temperature is low, turn on green LED
    led1_LEDRGB_Green_PIN_D4_rawData = HIGH;
    led1_LEDRGB_Red_PIN_D3_rawData = LOW;
  } else {
    // Temperature is high, turn on red LED
    led1_LEDRGB_Green_PIN_D4_rawData = LOW;
    led1_LEDRGB_Red_PIN_D3_rawData = HIGH;
  }
}

void updateOutputs()
{
  digitalWrite(led1_LEDRGB_Red_PIN_D3, led1_LEDRGB_Red_PIN_D3_rawData);
  digitalWrite(led1_LEDRGB_Green_PIN_D4, led1_LEDRGB_Green_PIN_D4_rawData);
}

/* END CODE */