Advertisement
pleasedontcode

"Sensor Display" rev_07

Jun 30th, 2024
582
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: "Sensor Display"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-30 07:57:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* DHT22 mengukur suhu dan kelembaban, hasilnya */
  21.     /* ditampilkan pada LCD I2C 1602 (SDA: A4, SCL: A5). */
  22.     /* LED hijau pada pin D3 menyala untuk suhu < 25°C. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>
  27. #include <LiquidCrystal_I2C.h>  // https://github.com/marcoschwartz/LiquidCrystal_I2C
  28. #include <DHT.h>  // https://github.com/adafruit/DHT-sensor-library
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateOutputs(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t DHT22_DOUT_PIN_D5 = 5;
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t led1_LEDRGB_Green_PIN_D3 = 3;
  40.  
  41. /***** DEFINITION OF I2C PINS *****/
  42. const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  43. const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  44. const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;  // Corrected to 0x27 based on examples
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. /***** used to store raw data *****/
  48. bool led1_LEDRGB_Green_PIN_D3_rawData = 0;
  49.  
  50. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  51. /***** used to store data after characteristic curve transformation *****/
  52. float led1_LEDRGB_Green_PIN_D3_phyData = 0.0;
  53.  
  54. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  55. LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);  // Initialize with address, columns, and rows
  56.  
  57. #define DHTTYPE DHT22   // Define the type of DHT sensor
  58. DHT dht(DHT22_DOUT_PIN_D5, DHTTYPE);  // Initialize DHT sensor
  59.  
  60. void setup(void)
  61. {
  62.   // put your setup code here, to run once:
  63.   pinMode(DHT22_DOUT_PIN_D5, INPUT_PULLUP);
  64.   pinMode(led1_LEDRGB_Green_PIN_D3, OUTPUT);
  65.  
  66.   // Initialize the LCD
  67.   lcd.init();
  68.   lcd.backlight();
  69.   lcd.setCursor(0, 0);
  70.   lcd.print("Initializing...");
  71.  
  72.   // Initialize the DHT sensor
  73.   dht.begin();
  74. }
  75.  
  76. void loop(void)
  77. {
  78.   // put your main code here, to run repeatedly:
  79.   updateOutputs(); // Refresh output data
  80.  
  81.   // Read humidity and temperature from DHT sensor
  82.   float h = dht.readHumidity();
  83.   float t = dht.readTemperature();
  84.  
  85.   // Check if any reads failed and exit early (to try again).
  86.   if (isnan(h) || isnan(t)) {
  87.     lcd.setCursor(0, 0);
  88.     lcd.print(F("Failed to read"));
  89.     lcd.setCursor(0, 1);
  90.     lcd.print(F("from DHT sensor"));
  91.     return;
  92.   }
  93.  
  94.   // Update LED status based on temperature
  95.   if (t < 25.0) {
  96.     led1_LEDRGB_Green_PIN_D3_rawData = HIGH;  // Turn on LED
  97.   } else {
  98.     led1_LEDRGB_Green_PIN_D3_rawData = LOW;   // Turn off LED
  99.   }
  100.  
  101.   // Print the results to the LCD
  102.   lcd.setCursor(0, 0);
  103.   lcd.print(F("Humidity: "));
  104.   lcd.print(h);
  105.   lcd.print(F("%   "));  // Clear any leftover characters
  106.  
  107.   lcd.setCursor(0, 1);
  108.   lcd.print(F("Temp: "));
  109.   lcd.print(t);
  110.   lcd.print(F("C   "));  // Clear any leftover characters
  111.  
  112.   delay(2000);  // Wait a few seconds between measurements.
  113. }
  114.  
  115. void updateOutputs()
  116. {
  117.   digitalWrite(led1_LEDRGB_Green_PIN_D3, led1_LEDRGB_Green_PIN_D3_rawData);
  118. }
  119.  
  120. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement