pleasedontcode

Proximity Indicator rev_02

Oct 24th, 2025
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Proximity Indicator
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-24 16:06:14
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* his code will connections | Component | Arduino */
  21.     /* Pin | | ------------------------ | */
  22.     /* ------------------------------------- | | IR */
  23.     /* Sensor OUT | D2 | | Ultrasonic TRIG | D3 | | */
  24.     /* Ultrasonic ECHO | D4 | | Road Green LED | D5 | | */
  25.     /* Footpath Green LED | D6 | | */
  26. /****** SYSTEM REQUIREMENT 2 *****/
  27.     /* connections | Component | Arduino Pin | | */
  28.     /* ------------------------ | */
  29.     /* ------------------------------------- | | IR */
  30.     /* Sensor OUT | D2 | | Ultrasonic TRIG | D3 | | */
  31.     /* Ultrasonic ECHO | D4 | | Road Green LED | D5 | | */
  32.     /* Footpath Green LED | D6 | | Road Red LED | */
  33. /****** END SYSTEM REQUIREMENTS *****/
  34.  
  35.  
  36. /* START CODE */
  37.  
  38. /****** DEFINITION OF LIBRARIES *****/
  39. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  40.  
  41. /****** FUNCTION PROTOTYPES *****/
  42. void setup(void);
  43. void loop(void);
  44.  
  45. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  46. const uint8_t IR_SENSOR_PIN     = 2; // Sensor OUT (IR)
  47.  
  48. /***** DEFINITION OF ANALOG INPUT PINS *****/
  49. const uint8_t VariableControlPotentiometer_Potentiometer_Vout_PIN_A0        = A0; // Potentiometer for threshold tuning
  50.  
  51. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  52. const uint8_t ROAD_GREEN_LED_PIN        = 5; // Road Green LED
  53. const uint8_t FOOTPATH_GREEN_LED_PIN    = 6; // Footpath Green LED
  54. const uint8_t ROAD_RED_LED_PIN      = 7; // Road Red LED
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57. const uint8_t ULTRASONIC_TRIG_PIN   = 3; // Ultrasonic TRIG
  58. const uint8_t ULTRASONIC_ECHO_PIN   = 4; // Ultrasonic ECHO
  59. Ultrasonic ultrasonic(ULTRASONIC_TRIG_PIN, ULTRASONIC_ECHO_PIN);
  60.  
  61. void setup(void)
  62. {
  63.     // put your setup code here, to run once:
  64.  
  65.     pinMode(IR_SENSOR_PIN, INPUT);
  66.     pinMode(ROAD_GREEN_LED_PIN, OUTPUT);
  67.     pinMode(FOOTPATH_GREEN_LED_PIN, OUTPUT);
  68.     pinMode(ROAD_RED_LED_PIN, OUTPUT);
  69.    
  70.     // Potentiometer is read as analog input; no need to pinMode
  71.     // Initialize outputs
  72.     digitalWrite(ROAD_GREEN_LED_PIN, LOW);
  73.     digitalWrite(FOOTPATH_GREEN_LED_PIN, LOW);
  74.     digitalWrite(ROAD_RED_LED_PIN, LOW);
  75.  
  76. }
  77.  
  78.  
  79. void loop(void)
  80. {
  81.     // Read distance in centimeters (default unit CM)
  82.     int distanceCM = ultrasonic.read();
  83.     // Read potentiometer to adjust threshold dynamically (20-100 cm)
  84.     int threshold = map(analogRead(VariableControlPotentiometer_Potentiometer_Vout_PIN_A0), 0, 1023, 20, 100);
  85.     // Read IR sensor (ACTIVE HIGH means object detected)
  86.     bool irDetected = digitalRead(IR_SENSOR_PIN) == HIGH;
  87.  
  88.     if (irDetected) {
  89.         // If IR detects something (e.g., obstacle), show stop
  90.         digitalWrite(ROAD_GREEN_LED_PIN, LOW);
  91.         digitalWrite(FOOTPATH_GREEN_LED_PIN, LOW);
  92.         digitalWrite(ROAD_RED_LED_PIN, HIGH);
  93.     } else {
  94.         // Otherwise use distance threshold
  95.         if (distanceCM > threshold) {
  96.             digitalWrite(ROAD_GREEN_LED_PIN, HIGH);
  97.             digitalWrite(FOOTPATH_GREEN_LED_PIN, HIGH);
  98.             digitalWrite(ROAD_RED_LED_PIN, LOW);
  99.         } else {
  100.             digitalWrite(ROAD_GREEN_LED_PIN, LOW);
  101.             digitalWrite(FOOTPATH_GREEN_LED_PIN, LOW);
  102.             digitalWrite(ROAD_RED_LED_PIN, HIGH);
  103.         }
  104.     }
  105.  
  106.     delay(100);
  107. }
  108.  
  109. /* END CODE */
  110.  
Advertisement
Add Comment
Please, Sign In to add comment