/********* 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: Ultrasonic LED Control
	- Source Code compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-03-14 04:41:46

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Connect two esp8266 in espnow prototype .each */
	/* esp8266 has its own ultrasonic sensor and led.if */
	/* distance in ultrasonic sensor is less than 10cm */
	/* then the led in another esp8266 has to glow */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t HC_SR04_Echo_PIN_D14 = 14;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t Led_LED_PIN_D4 = 4;
const uint8_t HC_SR04_Trigger_PIN_D13 = 13;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool Led_LED_PIN_D4_rawData = 0;
bool HC_SR04_Trigger_PIN_D13_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float Led_LED_PIN_D4_phyData = 0.0;
float HC_SR04_Trigger_PIN_D13_phyData = 0.0;

/****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
Ultrasonic ultrasonic(HC_SR04_Trigger_PIN_D13, HC_SR04_Echo_PIN_D14);

void setup(void)
{
	// put your setup code here, to run once:
	pinMode(HC_SR04_Echo_PIN_D14, INPUT);

	pinMode(Led_LED_PIN_D4, OUTPUT);
	pinMode(HC_SR04_Trigger_PIN_D13, OUTPUT);

	Serial.begin(9600); // Initialize serial communication

}

void loop(void)
{
	// put your main code here, to run repeatedly:
	unsigned int distance = ultrasonic.read(); // Read the distance measured by the ultrasonic sensor

	if (distance < 10)
	{
		Led_LED_PIN_D4_rawData = 1; // Turn on the LED if distance is less than 10 cm
	}
	else
	{
		Led_LED_PIN_D4_rawData = 0; // Turn off the LED if distance is greater than or equal to 10 cm
	}

	updateOutputs(); // Refresh output data

	Serial.print("Distance: ");
	Serial.print(distance);
	Serial.println(" cm");

	delay(1000); // Wait for 1 second before taking the next measurement

}

void updateOutputs()
{
	digitalWrite(Led_LED_PIN_D4, Led_LED_PIN_D4_rawData);
	digitalWrite(HC_SR04_Trigger_PIN_D13, HC_SR04_Trigger_PIN_D13_rawData);
}