CCCPaste

Distance Control rev_01

/********* 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 Control
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-10-21 11:09:18

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* led turn on when object is near */
/****** END SYSTEM REQUIREMENTS *****/

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

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t USSENSOR1_HC-SR04_Echo_PIN_D3		= 3;
const uint8_t USSENSOR1_HC-SR04_Echo_PIN_D5		= 5;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t USSENSOR1_HC-SR04_Trigger_PIN_D2		= 2;
const uint8_t USSENSOR1_HC-SR04_Trigger_PIN_D4		= 4;
const uint8_t led1_LED_PIN_D6		= 6;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool	USSENSOR1_HC-SR04_Trigger_PIN_D2_rawData		= 0;
bool	USSENSOR1_HC-SR04_Trigger_PIN_D4_rawData		= 0;
bool	led1_LED_PIN_D6_rawData		= 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float	USSENSOR1_HC-SR04_Trigger_PIN_D2_phyData		= 0.0;
float	USSENSOR1_HC-SR04_Trigger_PIN_D4_phyData		= 0.0;
float	led1_LED_PIN_D6_phyData		= 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Initialize the Ultrasonic sensor object with Trigger and Echo pins
Ultrasonic ultrasonic1(USSENSOR1_HC-SR04_Trigger_PIN_D2, USSENSOR1_HC-SR04_Echo_PIN_D3);
Ultrasonic ultrasonic2(USSENSOR1_HC-SR04_Trigger_PIN_D4, USSENSOR1_HC-SR04_Echo_PIN_D5);

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

	pinMode(USSENSOR1_HC-SR04_Echo_PIN_D3,	INPUT);
	pinMode(USSENSOR1_HC-SR04_Echo_PIN_D5,	INPUT);

	pinMode(USSENSOR1_HC-SR04_Trigger_PIN_D2,	 OUTPUT);
	pinMode(USSENSOR1_HC-SR04_Trigger_PIN_D4,	 OUTPUT);
	pinMode(led1_LED_PIN_D6,	 OUTPUT);
	
	Serial.begin(9600); // Initialize serial communication for debugging
}

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

	updateOutputs(); // Refresh output data

	// Read distances from the ultrasonic sensors
	int distance1 = ultrasonic1.read(); // Read distance from the first sensor
	int distance2 = ultrasonic2.read(); // Read distance from the second sensor
	
	// Print the distances to the Serial Monitor
	Serial.print("Distance from Sensor 1: ");
	Serial.print(distance1);
	Serial.print(" cm, Distance from Sensor 2: ");
	Serial.println(distance2);
	
	// Check if the distance is less than a certain threshold (e.g., 10 cm)
	if (distance1 < 10 || distance2 < 10) {
		led1_LED_PIN_D6_rawData = HIGH; // Turn on the LED when an object is near
	} else {
		led1_LED_PIN_D6_rawData = LOW; // Turn off the LED
	}
	
	delay(1000); // Wait for a second before the next reading
}

void updateOutputs()
{
	digitalWrite(USSENSOR1_HC-SR04_Trigger_PIN_D2, USSENSOR1_HC-SR04_Trigger_PIN_D2_rawData);
	digitalWrite(USSENSOR1_HC-SR04_Trigger_PIN_D4, USSENSOR1_HC-SR04_Trigger_PIN_D4_rawData);
	digitalWrite(led1_LED_PIN_D6, led1_LED_PIN_D6_rawData); // Update LED state
}

/* END CODE */