CCCPaste

Distance Measurement 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: Distance Measurement
	- Source Code compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-11-19 11:49:18

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* My HC-SR04 is with UART, so use tx, rx. I want to */
	/* use this as a transmitter unit for my water level */
	/* monitoring system, which will send the data to a */
	/* receiver unit with esp32 and other components. So */
	/* make sketch for transmitter now. */
/****** END SYSTEM REQUIREMENTS *****/


/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include   // https://github.com/ErickSimoes/Ultrasonic
#include       // https://github.com/d03n3rfr1tz3/HC-SR04

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t UltraSoundSensor_HC_SR04_Echo_PIN_D13 = 13;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t UltraSoundSensor_HC_SR04_Trigger_PIN_D4 = 4;

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

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Instantiate the Ultrasonic sensor
Ultrasonic ultrasonic(UltraSoundSensor_HC_SR04_Trigger_PIN_D4, UltraSoundSensor_HC_SR04_Echo_PIN_D13);

// Instantiate the HC-SR04 sensor
UltraSonicDistanceSensor distanceSensor(UltraSoundSensor_HC_SR04_Trigger_PIN_D4, UltraSoundSensor_HC_SR04_Echo_PIN_D13);

/****** SETUP FUNCTION *****/
void setup(void)
{
    // Initialize serial communication for debugging
    Serial.begin(9600);

    // Set pin modes
    pinMode(UltraSoundSensor_HC_SR04_Echo_PIN_D13, INPUT);
    pinMode(UltraSoundSensor_HC_SR04_Trigger_PIN_D4, OUTPUT);
}

/****** LOOP FUNCTION *****/
void loop(void)
{
    // Measure distance and update raw data
    UltraSoundSensor_HC_SR04_Trigger_PIN_D4_rawData = distanceSensor.measureDistanceCm() < 100; // Example condition

    // Update outputs
    updateOutputs(); // Refresh output data

    // Print distance for debugging
    Serial.print("Distance: ");
    Serial.print(distanceSensor.measureDistanceCm());
    Serial.println(" cm");

    delay(1000); // Delay for readability
}

/****** UPDATE OUTPUTS FUNCTION *****/
void updateOutputs()
{
    digitalWrite(UltraSoundSensor_HC_SR04_Trigger_PIN_D4, UltraSoundSensor_HC_SR04_Trigger_PIN_D4_rawData);
}

/* END CODE */