/********* 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 Control"
- Source Code NOT compiled for: Arduino Uno
- Source Code created on: 2024-06-30 02:41:56
********* 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 displayTemperature(float temperature);
void displayHumidity(float humidity);
void controlLED(float temperature);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t PushButton_PIN_D2 = 2;
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; // Corrected to 0x27 based on example
/***** 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 and size
#define DHTTYPE DHT22 // Define the type of DHT sensor
DHT dht(DHT22_DOUT_PIN_D5, DHTTYPE); // Initialize DHT sensor with pin and type
/***** GLOBAL VARIABLES *****/
bool displayTemperatureFlag = true; // Flag to toggle between temperature and humidity display
void setup(void)
{
// put your setup code here, to run once:
pinMode(PushButton_PIN_D2, INPUT_PULLUP);
pinMode(DHT22_DOUT_PIN_D5, INPUT_PULLUP);
pinMode(led1_LEDRGB_Red_PIN_D3, OUTPUT);
pinMode(led1_LEDRGB_Green_PIN_D4, OUTPUT);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
dht.begin(); // Initialize the DHT sensor
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop(void)
{
// put your main code here, to run repeatedly:
updateOutputs(); // Refresh output data
delay(2000); // Delay for 2 seconds
float h = dht.readHumidity(); // Read humidity
float t = dht.readTemperature(); // Read temperature in Celsius
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Check if the push button is pressed
if (digitalRead(PushButton_PIN_D2) == LOW) {
displayTemperatureFlag = !displayTemperatureFlag; // Toggle the display flag
delay(200); // Debounce delay
}
// Display temperature or humidity based on the flag
if (displayTemperatureFlag) {
displayTemperature(t);
} else {
displayHumidity(h);
}
// Control the RGB LED based on temperature
controlLED(t);
}
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 displayTemperature(float temperature)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature:");
lcd.setCursor(0, 1);
lcd.print(temperature);
lcd.print(" C");
}
void displayHumidity(float humidity)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity:");
lcd.setCursor(0, 1);
lcd.print(humidity);
lcd.print(" %");
}
void controlLED(float temperature)
{
if (temperature < 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;
}
}