/********* 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 NOT compiled for: Arduino Uno
	- Source Code created on: 2024-06-30 07:57:59

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* DHT22 mengukur suhu dan kelembaban, hasilnya */
	/* ditampilkan pada LCD I2C 1602 (SDA: A4, SCL: A5). */
	/* LED hijau pada pin D3 menyala untuk suhu < 25°C. */
/****** 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_Green_PIN_D3 = 3;

/***** 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;  // Corrected to 0x27 based on examples

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

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);  // Initialize with address, columns, and rows

#define DHTTYPE DHT22   // Define the type of DHT sensor
DHT dht(DHT22_DOUT_PIN_D5, DHTTYPE);  // Initialize DHT sensor

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(DHT22_DOUT_PIN_D5, INPUT_PULLUP);
  pinMode(led1_LEDRGB_Green_PIN_D3, OUTPUT);

  // Initialize the LCD
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Initializing...");

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

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

  // Read humidity and temperature from DHT sensor
  float h = dht.readHumidity();
  float t = dht.readTemperature();

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

  // Update LED status based on temperature
  if (t < 25.0) {
    led1_LEDRGB_Green_PIN_D3_rawData = HIGH;  // Turn on LED
  } else {
    led1_LEDRGB_Green_PIN_D3_rawData = LOW;   // Turn off LED
  }

  // Print the results to the LCD
  lcd.setCursor(0, 0);
  lcd.print(F("Humidity: "));
  lcd.print(h);
  lcd.print(F("%   "));  // Clear any leftover characters

  lcd.setCursor(0, 1);
  lcd.print(F("Temp: "));
  lcd.print(t);
  lcd.print(F("C   "));  // Clear any leftover characters

  delay(2000);  // Wait a few seconds between measurements.
}

void updateOutputs()
{
  digitalWrite(led1_LEDRGB_Green_PIN_D3, led1_LEDRGB_Green_PIN_D3_rawData);
}

/* END CODE */