Advertisement
pleasedontcode

"Sensor Display" rev_05

Jun 29th, 2024
570
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 03:12:01
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Sensor DHT22 mengukur suhu dan kelembapan, */
  21.     /* hasilnya ditampilkan pada LCD I2C 1602. LED RGB */
  22.     /* dikontrol melalui pin D3 (merah) dan D4 (hijau): */
  23.     /* hijau untuk suhu rendah, merah untuk suhu tinggi. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Wire.h>
  28. #include <LiquidCrystal_I2C.h>  //https://github.com/marcoschwartz/LiquidCrystal_I2C
  29. #include <DHT.h>  //https://github.com/adafruit/DHT-sensor-library
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t DHT22_DOUT_PIN_D5 = 5;
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t led1_LEDRGB_Red_PIN_D3 = 3;
  41. const uint8_t led1_LEDRGB_Green_PIN_D4 = 4;
  42.  
  43. /***** DEFINITION OF I2C PINS *****/
  44. const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  45. const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  46. const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;  // 0x27 is the hexadecimal representation of 39
  47.  
  48. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  49. /***** used to store raw data *****/
  50. bool led1_LEDRGB_Red_PIN_D3_rawData = 0;
  51. bool led1_LEDRGB_Green_PIN_D4_rawData = 0;
  52.  
  53. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  54. /***** used to store data after characteristic curve transformation *****/
  55. float led1_LEDRGB_Red_PIN_D3_phyData = 0.0;
  56. float led1_LEDRGB_Green_PIN_D4_phyData = 0.0;
  57.  
  58. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  59. LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);  // Initialize the LCD with I2C address 0x27, 16 columns, and 2 rows
  60. DHT dht(DHT22_DOUT_PIN_D5, DHT22);  // Initialize the DHT sensor with the specified pin and type (DHT22)
  61.  
  62. void setup(void)
  63. {
  64.   // put your setup code here, to run once:
  65.   lcd.init();  // Initialize the LCD
  66.   lcd.backlight();  // Turn on the backlight
  67.  
  68.   pinMode(DHT22_DOUT_PIN_D5, INPUT_PULLUP);
  69.   pinMode(led1_LEDRGB_Red_PIN_D3, OUTPUT);
  70.   pinMode(led1_LEDRGB_Green_PIN_D4, OUTPUT);
  71.  
  72.   // Initialize the DHT sensor
  73.   dht.begin();
  74.  
  75.   // Display initial messages on the LCD
  76.   lcd.setCursor(0, 0);
  77.   lcd.print("Hello, world!");
  78.   lcd.setCursor(0, 1);
  79.   lcd.print("Arduino LCD Test");
  80. }
  81.  
  82. void loop(void)
  83. {
  84.   // put your main code here, to run repeatedly:
  85.   updateOutputs();  // Refresh output data
  86.  
  87.   // Read humidity and temperature from the DHT sensor
  88.   float humidity = dht.readHumidity();
  89.   float temperatureC = dht.readTemperature();
  90.   float temperatureF = dht.readTemperature(true);
  91.  
  92.   // Check if any reads failed and exit early (to try again).
  93.   if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
  94.     lcd.setCursor(0, 0);
  95.     lcd.print("Failed to read");
  96.     lcd.setCursor(0, 1);
  97.     lcd.print("from DHT sensor!");
  98.     return;
  99.   }
  100.  
  101.   // Compute heat index
  102.   float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);
  103.   float heatIndexF = dht.computeHeatIndex(temperatureF, humidity);
  104.  
  105.   // Display the results on the LCD
  106.   lcd.setCursor(0, 0);
  107.   lcd.print("Hum: ");
  108.   lcd.print(humidity);
  109.   lcd.print("%");
  110.  
  111.   lcd.setCursor(0, 1);
  112.   lcd.print("Temp: ");
  113.   lcd.print(temperatureC);
  114.   lcd.print("C ");
  115.   lcd.print(temperatureF);
  116.   lcd.print("F");
  117.  
  118.   // Control the RGB LED based on temperature
  119.   if (temperatureC < 25.0) {
  120.     // Temperature is low, turn on green LED
  121.     led1_LEDRGB_Green_PIN_D4_rawData = HIGH;
  122.     led1_LEDRGB_Red_PIN_D3_rawData = LOW;
  123.   } else {
  124.     // Temperature is high, turn on red LED
  125.     led1_LEDRGB_Green_PIN_D4_rawData = LOW;
  126.     led1_LEDRGB_Red_PIN_D3_rawData = HIGH;
  127.   }
  128. }
  129.  
  130. void updateOutputs()
  131. {
  132.   digitalWrite(led1_LEDRGB_Red_PIN_D3, led1_LEDRGB_Red_PIN_D3_rawData);
  133.   digitalWrite(led1_LEDRGB_Green_PIN_D4, led1_LEDRGB_Green_PIN_D4_rawData);
  134. }
  135.  
  136. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement