CCCPaste

Proximity Indicator 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: Proximity Indicator
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2025-10-24 16:06:14

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* his code will connections | Component | Arduino */
	/* Pin | | ------------------------ | */
	/* ------------------------------------- | | IR */
	/* Sensor OUT | D2 | | Ultrasonic TRIG | D3 | | */
	/* Ultrasonic ECHO | D4 | | Road Green LED | D5 | | */
	/* Footpath Green LED | D6 | | */
/****** SYSTEM REQUIREMENT 2 *****/
	/* connections | Component | Arduino Pin | | */
	/* ------------------------ | */
	/* ------------------------------------- | | IR */
	/* Sensor OUT | D2 | | Ultrasonic TRIG | D3 | | */
	/* Ultrasonic ECHO | D4 | | Road Green LED | D5 | | */
	/* Footpath Green LED | D6 | | Road Red LED | */
/****** END SYSTEM REQUIREMENTS *****/


/* START CODE */

/****** 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 IR_SENSOR_PIN		= 2; // Sensor OUT (IR)

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t VariableControlPotentiometer_Potentiometer_Vout_PIN_A0		= A0; // Potentiometer for threshold tuning

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t ROAD_GREEN_LED_PIN		= 5; // Road Green LED
const uint8_t FOOTPATH_GREEN_LED_PIN	= 6; // Footpath Green LED
const uint8_t ROAD_RED_LED_PIN		= 7; // Road Red LED

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
const uint8_t ULTRASONIC_TRIG_PIN	= 3; // Ultrasonic TRIG
const uint8_t ULTRASONIC_ECHO_PIN	= 4; // Ultrasonic ECHO
Ultrasonic ultrasonic(ULTRASONIC_TRIG_PIN, ULTRASONIC_ECHO_PIN);

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

	pinMode(IR_SENSOR_PIN, INPUT);
	pinMode(ROAD_GREEN_LED_PIN, OUTPUT);
	pinMode(FOOTPATH_GREEN_LED_PIN, OUTPUT);
	pinMode(ROAD_RED_LED_PIN, OUTPUT);
	
	// Potentiometer is read as analog input; no need to pinMode
	// Initialize outputs
	digitalWrite(ROAD_GREEN_LED_PIN, LOW);
	digitalWrite(FOOTPATH_GREEN_LED_PIN, LOW);
	digitalWrite(ROAD_RED_LED_PIN, LOW);

}


void loop(void)
{
	// Read distance in centimeters (default unit CM)
	int distanceCM = ultrasonic.read();
	// Read potentiometer to adjust threshold dynamically (20-100 cm)
	int threshold = map(analogRead(VariableControlPotentiometer_Potentiometer_Vout_PIN_A0), 0, 1023, 20, 100);
	// Read IR sensor (ACTIVE HIGH means object detected)
	bool irDetected = digitalRead(IR_SENSOR_PIN) == HIGH;

	if (irDetected) {
		// If IR detects something (e.g., obstacle), show stop
		digitalWrite(ROAD_GREEN_LED_PIN, LOW);
		digitalWrite(FOOTPATH_GREEN_LED_PIN, LOW);
		digitalWrite(ROAD_RED_LED_PIN, HIGH);
	} else {
		// Otherwise use distance threshold
		if (distanceCM > threshold) {
			digitalWrite(ROAD_GREEN_LED_PIN, HIGH);
			digitalWrite(FOOTPATH_GREEN_LED_PIN, HIGH);
			digitalWrite(ROAD_RED_LED_PIN, LOW);
		} else {
			digitalWrite(ROAD_GREEN_LED_PIN, LOW);
			digitalWrite(FOOTPATH_GREEN_LED_PIN, LOW);
			digitalWrite(ROAD_RED_LED_PIN, HIGH);
		}
	}

	delay(100);
}

/* END CODE */