CCCPaste

"Environmental Control" rev_01

/********* 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: "Environmental Control"
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-06-30 02:31:45

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* sensor DHT11 untuk suhu dan kelembapan, dan LED */
	/* RGB. Gunakan tombol tekan untuk beralih antara */
	/* menampilkan suhu dan kelembapan pada LCD. Kontrol */
	/* LED RGB berdasarkan ambang batas suhu. jika rendah */
	/* berwarna hijau dan jika tinggi warna merah */
/****** END SYSTEM REQUIREMENTS *****/

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

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t PushButton_PIN_D2 = 2;
const uint8_t DHT11_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;  // Corrected to 0x27 which is a common I2C address for LCD

/***** 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 LCD with address, columns, and rows
DHT dht(DHT11_DOUT_PIN_D5, DHT11);  // Initialize DHT sensor with pin and type

/****** DEFINITION OF VARIABLES *****/
bool displayTemperature = true;  // Variable to toggle between temperature and humidity display
unsigned long lastButtonPress = 0;  // Variable to debounce button press

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(PushButton_PIN_D2, INPUT_PULLUP);
  pinMode(DHT11_DOUT_PIN_D5, INPUT_PULLUP);

  pinMode(led1_LEDRGB_Red_PIN_D3, OUTPUT);
  pinMode(led1_LEDRGB_Green_PIN_D4, 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
  handleButtonPress(); // Check for button press to toggle display

  // Read data from 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, 1);
    lcd.print("DHT read error");
    return;
  }

  // Compute heat index in both Celsius and Fahrenheit
  float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);
  float heatIndexF = dht.computeHeatIndex(temperatureF, humidity);

  // Display data on LCD
  lcd.setCursor(0, 0);
  if (displayTemperature) {
    lcd.print("Temp: ");
    lcd.print(temperatureC);
    lcd.print("C");
  } else {
    lcd.print("Humidity: ");
    lcd.print(humidity);
    lcd.print("%");
  }

  // Control RGB LED based on temperature thresholds
  if (temperatureC < 25.0) {
    digitalWrite(led1_LEDRGB_Red_PIN_D3, LOW);
    digitalWrite(led1_LEDRGB_Green_PIN_D4, HIGH);
  } else {
    digitalWrite(led1_LEDRGB_Red_PIN_D3, HIGH);
    digitalWrite(led1_LEDRGB_Green_PIN_D4, LOW);
  }
}

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);
}

void handleButtonPress()
{
  if (digitalRead(PushButton_PIN_D2) == LOW) {
    unsigned long currentMillis = millis();
    if (currentMillis - lastButtonPress > 200) {  // Debounce delay
      displayTemperature = !displayTemperature;  // Toggle display variable
      lastButtonPress = currentMillis;
    }
  }
}

/* END CODE */