Advertisement
pleasedontcode

Obstacle Detection rev_01

Feb 4th, 2025
25
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: 2025-02-05 05:51:54
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* "I want to implement ultrasonic object detection */
  21.     /* using the HC-SR04 sensor for distance measurement */
  22.     /* and integrate it with a line-following mechanism." */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Ultrasonic.h> // https://github.com/ErickSimoes/Ultrasonic
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t Ultra_HC_SR04_Echo_PIN_D3 = 3;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t Ultra_HC_SR04_Trigger_PIN_D2 = 2;
  39.  
  40. // ============================
  41. // Motor control pins
  42. // ============================
  43. const int motorLeft1 = 7;
  44. const int motorLeft2 = 8;
  45. const int motorRight1 = 9;
  46. const int motorRight2 = 10;
  47. const int motorSpeedLeft = 5;
  48. const int motorSpeedRight = 6;
  49.  
  50. // ============================
  51. // Line-following sensor pins
  52. // ============================
  53. const int lineSensorLeft = A0;
  54. const int lineSensorRight = A2;
  55.  
  56. // ============================
  57. // Front Ultrasonic sensor pins
  58. // ============================
  59. const int trigPin = 11;
  60. const int echoPin = 12;
  61.  
  62. // ============================
  63. // Thresholds and global variables
  64. // ============================
  65. const int lineThreshold = 500; // Increased threshold to 30 cm (adjust as needed)
  66. const int obstacleThreshold = 30;
  67.  
  68. // Create an instance of the Ultrasonic class for the front sensor
  69. Ultrasonic ultrasonic(trigPin, echoPin);
  70.  
  71. // ============================
  72. // Setup
  73. // ============================
  74. void setup() {
  75.     // put your setup code here, to run once:
  76.     pinMode(Ultra_HC_SR04_Echo_PIN_D3, INPUT);
  77.     pinMode(Ultra_HC_SR04_Trigger_PIN_D2, OUTPUT);
  78.  
  79.     // Initialize motor control pins as outputs
  80.     pinMode(motorLeft1, OUTPUT);
  81.     pinMode(motorLeft2, OUTPUT);
  82.     pinMode(motorRight1, OUTPUT);
  83.     pinMode(motorRight2, OUTPUT);
  84.     pinMode(motorSpeedLeft, OUTPUT);
  85.     pinMode(motorSpeedRight, OUTPUT);
  86.  
  87.     // Initialize line-following sensor pins as inputs
  88.     pinMode(lineSensorLeft, INPUT);
  89.     pinMode(lineSensorRight, INPUT);
  90.  
  91.     // Initialize serial communication for debugging
  92.     Serial.begin(9600);
  93.     randomSeed(analogRead(A3));
  94. }
  95.  
  96. void loop(void) {
  97.     // put your main code here, to run repeatedly:
  98.     updateOutputs(); // Refresh output data
  99.  
  100.     // Read line-following sensors
  101.     int leftSensorValue = analogRead(lineSensorLeft);
  102.     int rightSensorValue = analogRead(lineSensorRight);
  103.  
  104.     // Read the front ultrasonic sensor
  105.     int distance = ultrasonic.read(); // Use the Ultrasonic library to get distance
  106.     Serial.print("Front distance: ");
  107.     Serial.println(distance);
  108.  
  109.     // Check for obstacles in front using the front sensor
  110.     if (distance > 0 && distance < obstacleThreshold) {
  111.         Serial.println("Obstacle detected in front!");
  112.         compareDistance();
  113.     } else {
  114.         // No front obstacle: follow the line
  115.         if (leftSensorValue < lineThreshold && rightSensorValue < lineThreshold) {
  116.             // Both sensors on the line, move forward
  117.             moveForward();
  118.         } else if (leftSensorValue < lineThreshold) {
  119.             // Left sensor on the line, turn right to re-align
  120.             turnRight();
  121.         } else if (rightSensorValue < lineThreshold) {
  122.             // Right sensor on the line, turn left to re-align
  123.             turnLeft();
  124.         } else {
  125.             // Neither sensor on the line, stop
  126.             stopMotors();
  127.         }
  128.     }
  129. }
  130.  
  131. // ============================
  132. // New function: compareDistance()
  133. // Uses left/right ultrasonic sensors to decide which way to avoid an obstacle
  134. // ============================
  135. void compareDistance() {
  136.     // Placeholder for left/right distance comparison logic
  137.     // Uncomment and implement if using left/right ultrasonic sensors
  138. }
  139.  
  140. // ============================
  141. // Motor control functions
  142. // ============================
  143. void moveForward() {
  144.     digitalWrite(motorLeft1, HIGH);
  145.     digitalWrite(motorLeft2, LOW);
  146.     digitalWrite(motorRight1, HIGH);
  147.     digitalWrite(motorRight2, LOW);
  148.     analogWrite(motorSpeedLeft, 200);
  149.     analogWrite(motorSpeedRight, 200);
  150. }
  151.  
  152. void moveBackward() {
  153.     digitalWrite(motorLeft1, LOW);
  154.     digitalWrite(motorLeft2, HIGH);
  155.     digitalWrite(motorRight1, LOW);
  156.     digitalWrite(motorRight2, HIGH);
  157.     analogWrite(motorSpeedLeft, 200);
  158.     analogWrite(motorSpeedRight, 200);
  159. }
  160.  
  161. void turnLeft() {
  162.     // To turn left, reverse the left motor and forward the right motor
  163.     digitalWrite(motorLeft1, LOW);
  164.     digitalWrite(motorLeft2, HIGH);
  165.     digitalWrite(motorRight1, HIGH);
  166.     digitalWrite(motorRight2, LOW);
  167.     analogWrite(motorSpeedLeft, 200);
  168.     analogWrite(motorSpeedRight, 200);
  169. }
  170.  
  171. void turnRight() {
  172.     // To turn right, forward the left motor and reverse the right motor
  173.     digitalWrite(motorLeft1, HIGH);
  174.     digitalWrite(motorLeft2, LOW);
  175.     digitalWrite(motorRight1, LOW);
  176.     digitalWrite(motorRight2, HIGH);
  177.     analogWrite(motorSpeedLeft, 200);
  178.     analogWrite(motorSpeedRight, 200);
  179. }
  180.  
  181. void stopMotors() {
  182.     digitalWrite(motorLeft1, LOW);
  183.     digitalWrite(motorLeft2, LOW);
  184.     digitalWrite(motorRight1, LOW);
  185.     digitalWrite(motorRight2, LOW);
  186.     analogWrite(motorSpeedLeft, 0);
  187.     analogWrite(motorSpeedRight, 0);
  188. }
  189.  
  190. void updateOutputs() {
  191.     // Placeholder for updating outputs logic
  192. }
  193.  
  194. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement