Advertisement
pleasedontcode

"Environmental Control" rev_01

Jun 29th, 2024
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Environmental Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-30 02:31:45
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* sensor DHT11 untuk suhu dan kelembapan, dan LED */
  21.     /* RGB. Gunakan tombol tekan untuk beralih antara */
  22.     /* menampilkan suhu dan kelembapan pada LCD. Kontrol */
  23.     /* LED RGB berdasarkan ambang batas suhu. jika rendah */
  24.     /* berwarna hijau dan jika tinggi warna merah */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <LiquidCrystal_I2C.h>  // https://github.com/marcoschwartz/LiquidCrystal_I2C
  30. #include <DHT.h>  // https://github.com/adafruit/DHT-sensor-library
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void updateOutputs(void);
  36. void handleButtonPress(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t PushButton_PIN_D2 = 2;
  40. const uint8_t DHT11_DOUT_PIN_D5 = 5;
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t led1_LEDRGB_Red_PIN_D3 = 3;
  44. const uint8_t led1_LEDRGB_Green_PIN_D4 = 4;
  45.  
  46. /***** DEFINITION OF I2C PINS *****/
  47. const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  48. const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  49. const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;  // Corrected to 0x27 which is a common I2C address for LCD
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. /***** used to store raw data *****/
  53. bool led1_LEDRGB_Red_PIN_D3_rawData = 0;
  54. bool led1_LEDRGB_Green_PIN_D4_rawData = 0;
  55.  
  56. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  57. /***** used to store data after characteristic curve transformation *****/
  58. float led1_LEDRGB_Red_PIN_D3_phyData = 0.0;
  59. float led1_LEDRGB_Green_PIN_D4_phyData = 0.0;
  60.  
  61. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  62. LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);  // Initialize LCD with address, columns, and rows
  63. DHT dht(DHT11_DOUT_PIN_D5, DHT11);  // Initialize DHT sensor with pin and type
  64.  
  65. /****** DEFINITION OF VARIABLES *****/
  66. bool displayTemperature = true;  // Variable to toggle between temperature and humidity display
  67. unsigned long lastButtonPress = 0;  // Variable to debounce button press
  68.  
  69. void setup(void)
  70. {
  71.   // put your setup code here, to run once:
  72.   pinMode(PushButton_PIN_D2, INPUT_PULLUP);
  73.   pinMode(DHT11_DOUT_PIN_D5, INPUT_PULLUP);
  74.  
  75.   pinMode(led1_LEDRGB_Red_PIN_D3, OUTPUT);
  76.   pinMode(led1_LEDRGB_Green_PIN_D4, OUTPUT);
  77.  
  78.   // Initialize the LCD
  79.   lcd.init();
  80.   lcd.backlight();
  81.   lcd.setCursor(0, 0);
  82.   lcd.print("Initializing...");
  83.  
  84.   // Initialize the DHT sensor
  85.   dht.begin();
  86. }
  87.  
  88. void loop(void)
  89. {
  90.   // put your main code here, to run repeatedly:
  91.   updateOutputs(); // Refresh output data
  92.   handleButtonPress(); // Check for button press to toggle display
  93.  
  94.   // Read data from DHT sensor
  95.   float humidity = dht.readHumidity();
  96.   float temperatureC = dht.readTemperature();
  97.   float temperatureF = dht.readTemperature(true);
  98.  
  99.   // Check if any reads failed and exit early (to try again).
  100.   if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
  101.     lcd.setCursor(0, 1);
  102.     lcd.print("DHT read error");
  103.     return;
  104.   }
  105.  
  106.   // Compute heat index in both Celsius and Fahrenheit
  107.   float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);
  108.   float heatIndexF = dht.computeHeatIndex(temperatureF, humidity);
  109.  
  110.   // Display data on LCD
  111.   lcd.setCursor(0, 0);
  112.   if (displayTemperature) {
  113.     lcd.print("Temp: ");
  114.     lcd.print(temperatureC);
  115.     lcd.print("C");
  116.   } else {
  117.     lcd.print("Humidity: ");
  118.     lcd.print(humidity);
  119.     lcd.print("%");
  120.   }
  121.  
  122.   // Control RGB LED based on temperature thresholds
  123.   if (temperatureC < 25.0) {
  124.     digitalWrite(led1_LEDRGB_Red_PIN_D3, LOW);
  125.     digitalWrite(led1_LEDRGB_Green_PIN_D4, HIGH);
  126.   } else {
  127.     digitalWrite(led1_LEDRGB_Red_PIN_D3, HIGH);
  128.     digitalWrite(led1_LEDRGB_Green_PIN_D4, LOW);
  129.   }
  130. }
  131.  
  132. void updateOutputs()
  133. {
  134.   digitalWrite(led1_LEDRGB_Red_PIN_D3, led1_LEDRGB_Red_PIN_D3_rawData);
  135.   digitalWrite(led1_LEDRGB_Green_PIN_D4, led1_LEDRGB_Green_PIN_D4_rawData);
  136. }
  137.  
  138. void handleButtonPress()
  139. {
  140.   if (digitalRead(PushButton_PIN_D2) == LOW) {
  141.     unsigned long currentMillis = millis();
  142.     if (currentMillis - lastButtonPress > 200) {  // Debounce delay
  143.       displayTemperature = !displayTemperature;  // Toggle display variable
  144.       lastButtonPress = currentMillis;
  145.     }
  146.   }
  147. }
  148.  
  149. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement