Advertisement
pleasedontcode

**Obstacle Detection** rev_01

Dec 20th, 2024
32
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: **Obstacle Detection**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-20 13:49:48
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when Ir sensor will detect any obstracle then led */
  21.     /* will blink for 5 seasond */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void updateOutputs();
  32.  
  33. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  34. const uint8_t IRsensorInfraredsensor_LED_PIN_D2 = 2;
  35. const uint8_t LED1 = 13; // Added LED pins
  36. const uint8_t LED2 = 12;
  37. const uint8_t LED3 = 11;
  38. const uint8_t IR1 = 2; // Note: This conflicts with IRsensorInfraredsensor_LED_PIN_D2
  39. const uint8_t IR2 = 3;
  40. const uint8_t IR3 = 4;
  41.  
  42. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  43. /***** used to store raw data *****/
  44. bool IRsensorInfraredsensor_LED_PIN_D2_rawData = 0;
  45.  
  46. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  47. /***** used to store data after characteristic curve transformation *****/
  48. float IRsensorInfraredsensor_LED_PIN_D2_phyData = 0.0;
  49.  
  50. // Variables for obstacle detection
  51. int val1 = 0;
  52. int val2 = 0;
  53. int val3 = 0;
  54. unsigned long previousMillis = 0;  // to track the time for LED blinking
  55. const long interval = 500;  // interval for blinking (500ms)
  56. const long delayTime = 5000;  // Delay for 5 seconds (5000ms)
  57. bool obstacleDetected = false; // Flag for obstacle detection
  58.  
  59. void setup(void)
  60. {
  61.     // put your setup code here, to run once:
  62.     pinMode(IRsensorInfraredsensor_LED_PIN_D2, OUTPUT);
  63.     pinMode(IR1, INPUT); // Setup for IR sensors
  64.     pinMode(IR2, INPUT);
  65.     pinMode(IR3, INPUT);
  66.     pinMode(LED1, OUTPUT); // Setup for LEDs
  67.     pinMode(LED2, OUTPUT);
  68.     pinMode(LED3, OUTPUT);
  69.     Serial.begin(9600); // Initialize serial communication
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     // put your main code here, to run repeatedly:
  75.     updateOutputs(); // Refresh output data
  76.  
  77.     // Read IR sensor values
  78.     val1 = digitalRead(IR1);
  79.     val2 = digitalRead(IR2);
  80.     val3 = digitalRead(IR3);
  81.  
  82.     // Check for obstacle detection
  83.     if (val1 == 0 || val2 == 0 || val3 == 0) {
  84.         obstacleDetected = true; // Set flag if any IR sensor detects an obstacle
  85.         previousMillis = millis(); // Reset the timer for blinking
  86.     }
  87.  
  88.     // Blink LEDs for 5 seconds if an obstacle is detected
  89.     if (obstacleDetected) {
  90.         unsigned long currentMillis = millis();
  91.         if (currentMillis - previousMillis < delayTime) {
  92.             // Blink LED1
  93.             if ((currentMillis / interval) % 2 == 0) {
  94.                 digitalWrite(LED1, HIGH);
  95.                 digitalWrite(LED2, HIGH);
  96.                 digitalWrite(LED3, HIGH);
  97.             } else {
  98.                 digitalWrite(LED1, LOW);
  99.                 digitalWrite(LED2, LOW);
  100.                 digitalWrite(LED3, LOW);
  101.             }
  102.         } else {
  103.             // Reset the flag after 5 seconds
  104.             obstacleDetected = false;
  105.             digitalWrite(LED1, LOW);
  106.             digitalWrite(LED2, LOW);
  107.             digitalWrite(LED3, LOW);
  108.         }
  109.     } else {
  110.         // Turn off LEDs if no obstacle is detected
  111.         digitalWrite(LED1, LOW);
  112.         digitalWrite(LED2, LOW);
  113.         digitalWrite(LED3, LOW);
  114.     }
  115.  
  116.     delay(10);  // Small delay for stability
  117. }
  118.  
  119. void updateOutputs()
  120. {
  121.     digitalWrite(IRsensorInfraredsensor_LED_PIN_D2, IRsensorInfraredsensor_LED_PIN_D2_rawData);
  122. }
  123.  
  124. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement