/********* 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: Arduino Sensor
	- Source Code compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-02-14 14:57:44

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* As a user, I want to be able to measure distance */
	/* using the HC-SR04 ultrasonic sensor and display */
	/* the results on an LCD screen. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <Wire.h>        // Add the Wire library for I2C communication
#include <LiquidCrystal_I2C.h>  // Add the LiquidCrystal_I2C library for LCD display
#include <Ultrasonic.h>  // Add the Ultrasonic library for distance measurement
#include <DHT.h>         // Add the DHT library for temperature and humidity measurement

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void displayData(float distance, float temperature, float humidity);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t DistanceBuddy_HC_SR04_Echo_PIN = 14;
const uint8_t DHT11_DHT11_DOUT_PIN = 4;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t DistanceBuddy_HC_SR04_Trigger_PIN = 13;
const uint8_t HelloRGB_LEDRGB_Red_PIN = 19;
const uint8_t HelloRGB_LEDRGB_Green_PIN = 20;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool DistanceBuddy_HC_SR04_Trigger_PIN_rawData = 0;
bool HelloRGB_LEDRGB_Red_PIN_rawData = 0;
bool HelloRGB_LEDRGB_Green_PIN_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
float DistanceBuddy_HC_SR04_Trigger_PIN_phyData = 0.0;
float HelloRGB_LEDRGB_Red_PIN_phyData = 0.0;
float HelloRGB_LEDRGB_Green_PIN_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
Ultrasonic ultrasonic(DistanceBuddy_HC_SR04_Trigger_PIN, DistanceBuddy_HC_SR04_Echo_PIN); // Create an instance of the Ultrasonic class
DHT dht(DHT11_DHT11_DOUT_PIN, DHT11); // Create an instance of the DHT class
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Create an instance of the LiquidCrystal_I2C class for LCD display

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(DistanceBuddy_HC_SR04_Echo_PIN, INPUT);
  pinMode(DistanceBuddy_HC_SR04_Trigger_PIN, OUTPUT);
  pinMode(HelloRGB_LEDRGB_Red_PIN, OUTPUT);
  pinMode(HelloRGB_LEDRGB_Green_PIN, OUTPUT);

  ultrasonic.setTimeout(2000); // Set the timeout for Ultrasonic sensor
  dht.begin(); // Initialize the DHT sensor
  lcd.begin(16, 2);  // Initialize the LCD display
  lcd.backlight();  // Turn on the LCD backlight
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  updateOutputs(); // Refresh output data

  float distance = ultrasonic.read(); // Read distance from Ultrasonic sensor
  float temperature = dht.readTemperature(); // Read temperature from DHT sensor
  float humidity = dht.readHumidity(); // Read humidity from DHT sensor

  displayData(distance, temperature, humidity); // Display the data on the LCD
}

void updateOutputs()
{
  digitalWrite(DistanceBuddy_HC_SR04_Trigger_PIN, DistanceBuddy_HC_SR04_Trigger_PIN_rawData);
  digitalWrite(HelloRGB_LEDRGB_Red_PIN, HelloRGB_LEDRGB_Red_PIN_rawData);
  digitalWrite(HelloRGB_LEDRGB_Green_PIN, HelloRGB_LEDRGB_Green_PIN_rawData);
}

void displayData(float distance, float temperature, float humidity)
{
  lcd.clear(); // Clear the LCD display
  lcd.setCursor(0, 0);  // Set the cursor to the first line

  // Display the distance on the LCD
  lcd.print("Distance: ");
  lcd.print(distance);
  lcd.print(" cm");

  lcd.setCursor(0, 1);  // Set the cursor to the second line

  // Display the temperature on the LCD
  lcd.print("Temp: ");
  lcd.print(temperature);
  lcd.print(" C");

  // Display the humidity on the LCD
  lcd.print(" Humidity: ");
  lcd.print(humidity);
  lcd.print(" %");
}
