CCCPaste

"Display Sensor" rev_02

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Display Sensor"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-06-01 11:06:59

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Build a project with a humidity sensor and LCD */
	/* screen. Get the data from the humidity sensor and */
	/* display it on the LCD monitor as follows:    If */
	/* the humidity is 25%, a symbol of 1 drop should */
	/* appear on the screen, if 50% - a symbol of two */
	/* drops,if 75 */
/****** END SYSTEM REQUIREMENTS *****/


/****** DEFINITION OF LIBRARIES *****/
#include 
#include   //https://github.com/marcoschwartz/LiquidCrystal_I2C
#include   //https://github.com/adafruit/DHT-sensor-library

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void displayHumidityOnLCD(float humidity);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t teni_DHT11_DOUT_PIN_D2 = 2;

/***** DEFINITION OF I2C PINS *****/
const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;  // Corrected to 0x27 based on the example

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4);  // Initialize the LCD with address and dimensions

#define DHTPIN 2     // Pin where the DHT sensor is connected
#define DHTTYPE DHT22   // DHT 22 (AM2302)

DHT dht(DHTPIN, DHTTYPE);  // Initialize DHT sensor

void setup(void)
{
  // put your setup code here, to run once:

  pinMode(teni_DHT11_DOUT_PIN_D2, INPUT_PULLUP);

  lcd.init();  // Initialize the LCD
  lcd.backlight();  // Turn on the backlight

  // Create custom characters for humidity levels
  uint8_t drop1[8] = {0x04, 0x0A, 0x0A, 0x11, 0x11, 0x11, 0x0E, 0x00}; // 1 drop
  uint8_t drop2[8] = {0x04, 0x0A, 0x0A, 0x11, 0x11, 0x1F, 0x0E, 0x00}; // 2 drops
  uint8_t drop3[8] = {0x04, 0x0A, 0x0A, 0x11, 0x1F, 0x1F, 0x0E, 0x00}; // 3 drops

  lcd.createChar(0, drop1);
  lcd.createChar(1, drop2);
  lcd.createChar(2, drop3);

  lcd.home();

  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();  // Initialize the DHT sensor
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  delay(2000);

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));

  displayHumidityOnLCD(h);
}

void displayHumidityOnLCD(float humidity)
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Humidity: ");
  lcd.print(humidity);
  lcd.print("%");

  lcd.setCursor(0, 1);
  if (humidity >= 75) {
    lcd.write(byte(2)); // 3 drops
  } else if (humidity >= 50) {
    lcd.write(byte(1)); // 2 drops
  } else if (humidity >= 25) {
    lcd.write(byte(0)); // 1 drop
  } else {
    lcd.print("Low");
  }
}