Advertisement
pleasedontcode

"Display Readings" rev_01

May 21st, 2024
821
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: "Display Readings"
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-05-21 17:08:26
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Monitor and display temperature and humidity data */
  21.     /* using a DHT11 sensor connected to an Arduino. The */
  22.     /* sensor is connected to digital pin 2 (D2). The */
  23.     /* system should initialize the sensor and read data */
  24.     /* continuously. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <DHT.h>  //https://github.com/adafruit/DHT-sensor-library
  30. #include <LiquidCrystal_I2C.h>  //https://github.com/marcoschwartz/LiquidCrystal_I2C
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t TempSensor_DHT11_DOUT_PIN_D2 = 2;  // DHT11 sensor connected to digital pin 2 (D2)
  38.  
  39. /***** DEFINITION OF I2C PINS *****/
  40. const uint8_t myDisplay_LCD1602I2C_I2C_PIN_SDA_A4 = A4;  // I2C SDA pin
  41. const uint8_t myDisplay_LCD1602I2C_I2C_PIN_SCL_A5 = A5;  // I2C SCL pin
  42. const uint8_t myDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;  // I2C address for the LCD
  43.  
  44. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  45. DHT dht(TempSensor_DHT11_DOUT_PIN_D2, DHT11);  // Initialize DHT sensor for DHT11
  46. LiquidCrystal_I2C lcd(myDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);  // Initialize LCD with address 0x27 for 16 chars and 2 lines
  47.  
  48. void setup(void)
  49. {
  50.   // Initialize serial communication at 9600 bits per second
  51.   Serial.begin(9600);
  52.   Serial.println(F("DHTxx test!"));
  53.  
  54.   // Set the DHT11 sensor pin as input with pull-up resistor
  55.   pinMode(TempSensor_DHT11_DOUT_PIN_D2, INPUT_PULLUP);
  56.   dht.begin();  // Initialize the DHT sensor
  57.  
  58.   // Initialize the LCD
  59.   lcd.init();  
  60.   lcd.backlight();  // Turn on the backlight
  61. }
  62.  
  63. void loop(void)
  64. {
  65.   // Wait a few seconds between measurements
  66.   delay(2000);
  67.  
  68.   // Reading temperature or humidity takes about 250 milliseconds!
  69.   float h = dht.readHumidity();  // Read humidity
  70.   float t = dht.readTemperature();  // Read temperature in Celsius
  71.   float f = dht.readTemperature(true);  // Read temperature in Fahrenheit
  72.  
  73.   // Check if any reads failed and exit early (to try again)
  74.   if (isnan(h) || isnan(t) || isnan(f)) {
  75.     Serial.println(F("Failed to read from DHT sensor!"));
  76.     lcd.clear();
  77.     lcd.setCursor(0, 0);
  78.     lcd.print("Sensor error!");
  79.     return;
  80.   }
  81.  
  82.   // Compute heat index in Fahrenheit and Celsius
  83.   float hif = dht.computeHeatIndex(f, h);
  84.   float hic = dht.computeHeatIndex(t, h, false);
  85.  
  86.   // Print the results to the Serial Monitor
  87.   Serial.print(F("Humidity: "));
  88.   Serial.print(h);
  89.   Serial.print(F("%  Temperature: "));
  90.   Serial.print(t);
  91.   Serial.print(F("°C "));
  92.   Serial.print(f);
  93.   Serial.print(F("°F  Heat index: "));
  94.   Serial.print(hic);
  95.   Serial.print(F("°C "));
  96.   Serial.print(hif);
  97.   Serial.println(F("°F"));
  98.  
  99.   // Display the results on the LCD
  100.   lcd.clear();
  101.   lcd.setCursor(0, 0);
  102.   lcd.print("Humidity: ");
  103.   lcd.print(h);
  104.   lcd.print("%");
  105.   lcd.setCursor(0, 1);
  106.   lcd.print("Temp: ");
  107.   lcd.print(t);
  108.   lcd.print("C ");
  109.   lcd.print(f);
  110.   lcd.print("F");
  111. }
  112.  
  113. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement