Advertisement
pleasedontcode

"Sensor Display" rev_06

Jun 29th, 2024
598
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 compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-30 03:20:12
  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.  
  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;  // 0x27 is the hexadecimal representation of 39
  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 I2C address 0x27, 16 columns, and 2 rows
  61. DHT dht(DHT22_DOUT_PIN_D5, DHT22);  // Initialize the DHT sensor with the specified pin and type (DHT22)
  62.  
  63. void setup(void)
  64. {
  65.   // put your setup code here, to run once:
  66.   lcd.init();  // Initialize the LCD
  67.   lcd.backlight();  // Turn on the backlight
  68.  
  69.   pinMode(DHT22_DOUT_PIN_D5, INPUT_PULLUP);
  70.   pinMode(led1_LEDRGB_Red_PIN_D3, OUTPUT);
  71.   pinMode(led1_LEDRGB_Green_PIN_D4, OUTPUT);
  72.  
  73.   // Initialize the DHT sensor
  74.   dht.begin();
  75.  
  76.   // Display initial messages on the LCD
  77.   lcd.setCursor(0, 0);
  78.   lcd.print("Hello, world!");
  79.   lcd.setCursor(0, 1);
  80.   lcd.print("Arduino LCD Test");
  81. }
  82.  
  83. void loop(void)
  84. {
  85.   // put your main code here, to run repeatedly:
  86.   updateOutputs();  // Refresh output data
  87.  
  88.   // Read humidity and temperature from the DHT sensor
  89.   float humidity = dht.readHumidity();
  90.   float temperatureC = dht.readTemperature();
  91.   float temperatureF = dht.readTemperature(true);
  92.  
  93.   // Check if any reads failed and exit early (to try again).
  94.   if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
  95.     lcd.setCursor(0, 0);
  96.     lcd.print("Failed to read");
  97.     lcd.setCursor(0, 1);
  98.     lcd.print("from DHT sensor!");
  99.     return;
  100.   }
  101.  
  102.   // Compute heat index
  103.   float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);
  104.   float heatIndexF = dht.computeHeatIndex(temperatureF, humidity);
  105.  
  106.   // Display the results on the LCD
  107.   lcd.setCursor(0, 0);
  108.   lcd.print("Hum: ");
  109.   lcd.print(humidity);
  110.   lcd.print("%");
  111.  
  112.   lcd.setCursor(0, 1);
  113.   lcd.print("Temp: ");
  114.   lcd.print(temperatureC);
  115.   lcd.print("C ");
  116.   lcd.print(temperatureF);
  117.   lcd.print("F");
  118.  
  119.   // Control the RGB LED based on temperature
  120.   if (temperatureC < 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.  
  131. void updateOutputs()
  132. {
  133.   digitalWrite(led1_LEDRGB_Red_PIN_D3, led1_LEDRGB_Red_PIN_D3_rawData);
  134.   digitalWrite(led1_LEDRGB_Green_PIN_D4, led1_LEDRGB_Green_PIN_D4_rawData);
  135. }
  136.  
  137. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement