Advertisement
pleasedontcode

"Climate Control" rev_03

Jun 29th, 2024
531
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: "Climate Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-30 03:07:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* sensor DHT22 untuk suhu dan kelembapan, dan LED */
  21.     /* RGB. Gunakan LCD untuk menampilkan suhu dan */
  22.     /* kelembapan. Kontrol LED RGB berdasarkan ambang */
  23.     /* batas suhu. jika suhu rendah berwarna hijau dan */
  24.     /* jika suhu 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.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t DHT22_DOUT_PIN_D5 = 5;
  39.  
  40. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  41. const uint8_t led1_LEDRGB_Red_PIN_D3 = 3;
  42. const uint8_t led1_LEDRGB_Green_PIN_D4 = 4;
  43.  
  44. /***** DEFINITION OF I2C PINS *****/
  45. const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  46. const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  47. const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;  // 39 in decimal
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. /***** used to store raw data *****/
  51. bool led1_LEDRGB_Red_PIN_D3_rawData = 0;
  52. bool led1_LEDRGB_Green_PIN_D4_rawData = 0;
  53.  
  54. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  55. /***** used to store data after characteristic curve transformation *****/
  56. float led1_LEDRGB_Red_PIN_D3_phyData = 0.0;
  57. float led1_LEDRGB_Green_PIN_D4_phyData = 0.0;
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60. LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);  // Initialize the LCD with address, columns, and rows
  61.  
  62. #define DHTTYPE DHT22   // DHT 22 (AM2302)
  63. DHT dht(DHT22_DOUT_PIN_D5, DHTTYPE);  // Initialize DHT sensor for DHT22
  64.  
  65. void setup(void)
  66. {
  67.   // put your setup code here, to run once:
  68.  
  69.   pinMode(DHT22_DOUT_PIN_D5, INPUT_PULLUP);
  70.  
  71.   pinMode(led1_LEDRGB_Red_PIN_D3, OUTPUT);
  72.   pinMode(led1_LEDRGB_Green_PIN_D4, OUTPUT);
  73.  
  74.   lcd.init();  // Initialize the LCD
  75.   lcd.backlight();  // Turn on the backlight
  76.   lcd.setCursor(0, 0);  // Set cursor to column 0, row 0
  77.   lcd.print("Hello, world!");  // Print a message to the LCD
  78.  
  79.   dht.begin();  // Initialize the DHT sensor
  80.  
  81.   Serial.begin(9600);  // Initialize serial communication
  82. }
  83.  
  84. void loop(void)
  85. {
  86.   // put your main code here, to run repeatedly:
  87.  
  88.   updateOutputs();  // Refresh output data
  89.  
  90.   delay(2000);  // Wait a few seconds between measurements
  91.  
  92.   float h = dht.readHumidity();  // Read humidity
  93.   float t = dht.readTemperature();  // Read temperature as Celsius
  94.  
  95.   // Check if any reads failed and exit early (to try again).
  96.   if (isnan(h) || isnan(t)) {
  97.     Serial.println(F("Failed to read from DHT sensor!"));
  98.     return;
  99.   }
  100.  
  101.   // Print the results to the Serial Monitor
  102.   Serial.print(F("Humidity: "));
  103.   Serial.print(h);
  104.   Serial.print(F("%  Temperature: "));
  105.   Serial.print(t);
  106.   Serial.println(F("°C"));
  107.  
  108.   // Update LCD display
  109.   lcd.clear();
  110.   lcd.setCursor(0, 0);
  111.   lcd.print("Temp: ");
  112.   lcd.print(t);
  113.   lcd.print(" C");
  114.   lcd.setCursor(0, 1);
  115.   lcd.print("Humidity: ");
  116.   lcd.print(h);
  117.   lcd.print(" %");
  118.  
  119.   // Control RGB LED based on temperature
  120.   if (t < 25.0) {
  121.     // Temperature is low, turn on green LED
  122.     led1_LEDRGB_Green_PIN_D4_rawData = HIGH;
  123.     led1_LEDRGB_Red_PIN_D3_rawData = LOW;
  124.   } else {
  125.     // Temperature is high, turn on red LED
  126.     led1_LEDRGB_Green_PIN_D4_rawData = LOW;
  127.     led1_LEDRGB_Red_PIN_D3_rawData = HIGH;
  128.   }
  129.  
  130.   updateOutputs();  // Refresh output data
  131. }
  132.  
  133. void updateOutputs()
  134. {
  135.   digitalWrite(led1_LEDRGB_Red_PIN_D3, led1_LEDRGB_Red_PIN_D3_rawData);
  136.   digitalWrite(led1_LEDRGB_Green_PIN_D4, led1_LEDRGB_Green_PIN_D4_rawData);
  137. }
  138.  
  139. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement