Advertisement
pleasedontcode

"Climate Controller" rev_04

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