Advertisement
pleasedontcode

"Obstacle Navigation" rev_01

Dec 17th, 2024
41
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 Navigation"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-18 03:09:01
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Make me the code of an Arduno Uno line followerand */
  21.     /* obstacle-avoiding robot with ultrasonic sensor and */
  22.     /* ir sensor */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  29.  
  30. // Pin Definitions
  31. #define IR_LEFT A0       // Left IR sensor
  32. #define IR_RIGHT A1      // Right IR sensor
  33. #define TRIG 8           // Ultrasonic trigger pin
  34. #define ECHO 7           // Ultrasonic echo pin
  35. #define MOTOR_LEFT_1 2   // Left motor control pin 1
  36. #define MOTOR_LEFT_2 3   // Left motor control pin 2
  37. #define MOTOR_RIGHT_1 4  // Right motor control pin 1
  38. #define MOTOR_RIGHT_2 5  // Right motor control pin 2
  39. #define ENA 6            // Left motor speed (PWM)
  40. #define ENB 9            // Right motor speed (PWM)
  41.  
  42. // Constants
  43. #define DISTANCE_THRESHOLD 15  // Minimum distance for obstacle avoidance (cm)
  44. #define DEFAULT_SPEED 120      // Default motor speed (0-255)
  45. #define TURN_DELAY 700         // Delay for turning (ms)
  46. #define STOP_DELAY 500         // Delay for stopping (ms)
  47. #define SENSOR_DEBOUNCE 5      // Debounce readings to filter noise
  48.  
  49. // Movement States
  50. enum Movement {
  51.   FORWARD,
  52.   LEFT,
  53.   RIGHT,
  54.   STOP
  55. };
  56.  
  57. // Function to calculate distance using ultrasonic sensor
  58. int getDistance() {
  59.   long total = 0;
  60.   for (int i = 0; i < 5; i++) {  // Average readings for better accuracy
  61.     digitalWrite(TRIG, LOW);
  62.     delayMicroseconds(2);
  63.     digitalWrite(TRIG, HIGH);
  64.     delayMicroseconds(10);
  65.     digitalWrite(TRIG, LOW);
  66.     total += pulseIn(ECHO, HIGH, 30000);  // Timeout after 30ms
  67.   }
  68.   long avgDuration = total / 5;
  69.   return avgDuration * 0.034 / 2;  // Convert duration to distance in cm
  70. }
  71.  
  72. // Function to set motor state
  73. void setMotorState(int left1, int left2, int right1, int right2) {
  74.   digitalWrite(MOTOR_LEFT_1, left1);
  75.   digitalWrite(MOTOR_LEFT_2, left2);
  76.   digitalWrite(MOTOR_RIGHT_1, right1);
  77.   digitalWrite(MOTOR_RIGHT_2, right2);
  78. }
  79.  
  80. // Function to handle robot movement
  81. void setMovement(Movement state) {
  82.   switch (state) {
  83.     case FORWARD:
  84.       Serial.println("Moving Forward");
  85.       setMotorState(HIGH, LOW, HIGH, LOW);
  86.       break;
  87.     case LEFT:
  88.       Serial.println("Turning Left");
  89.       setMotorState(LOW, LOW, HIGH, LOW);
  90.       break;
  91.     case RIGHT:
  92.       Serial.println("Turning Right");
  93.       setMotorState(HIGH, LOW, LOW, LOW);
  94.       break;
  95.     case STOP:
  96.       Serial.println("Stopping");
  97.       setMotorState(LOW, LOW, LOW, LOW);
  98.       break;
  99.   }
  100. }
  101.  
  102. /****** FUNCTION PROTOTYPES *****/
  103. void setup(void);
  104. void loop(void);
  105.  
  106. // ***** DEFINITION OF DIGITAL INPUT PINS *****/
  107. const uint8_t Sensor_HC_SR04_Echo_PIN = ECHO; // Echo pin for ultrasonic sensor
  108.  
  109. // ***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  110. const uint8_t Sensor_HC_SR04_Trigger_PIN = TRIG; // Trigger pin for ultrasonic sensor
  111.  
  112. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  113. // Create an instance of the Ultrasonic class
  114. Ultrasonic ultrasonic(TRIG, ECHO);
  115.  
  116. void setup(void)
  117. {
  118.     // put your setup code here, to run once:
  119.  
  120.     pinMode(Sensor_HC_SR04_Echo_PIN, INPUT);
  121.     pinMode(Sensor_HC_SR04_Trigger_PIN, OUTPUT);
  122.  
  123.     // Initialize pins for USER CODE
  124.     pinMode(IR_LEFT, INPUT);
  125.     pinMode(IR_RIGHT, INPUT);
  126.     pinMode(TRIG, OUTPUT);
  127.     pinMode(ECHO, INPUT);
  128.     pinMode(MOTOR_LEFT_1, OUTPUT);
  129.     pinMode(MOTOR_LEFT_2, OUTPUT);
  130.     pinMode(MOTOR_RIGHT_1, OUTPUT);
  131.     pinMode(MOTOR_RIGHT_2, OUTPUT);
  132.     pinMode(ENA, OUTPUT);
  133.     pinMode(ENB, OUTPUT);
  134.  
  135.     // Set default motor speed
  136.     analogWrite(ENA, DEFAULT_SPEED);
  137.     analogWrite(ENB, DEFAULT_SPEED);
  138.  
  139.     // Start serial communication
  140.     Serial.begin(9600);
  141. }
  142.  
  143. void loop(void)
  144. {
  145.     // put your main code here, to run repeatedly:
  146.  
  147.     // Read and debounce IR sensors
  148.     int leftIR = digitalRead(IR_LEFT);
  149.     int rightIR = digitalRead(IR_RIGHT);
  150.     for (int i = 0; i < SENSOR_DEBOUNCE; i++) {
  151.         leftIR &= digitalRead(IR_LEFT);
  152.         rightIR &= digitalRead(IR_RIGHT);
  153.     }
  154.  
  155.     // Measure distance using ultrasonic sensor
  156.     int distance = getDistance();
  157.     Serial.print("Distance: ");
  158.     Serial.println(distance);
  159.  
  160.     // Obstacle Avoidance Logic
  161.     if (distance < DISTANCE_THRESHOLD) {  // Obstacle detected
  162.         setMovement(STOP);                  // Stop
  163.         delay(STOP_DELAY);                  // Pause
  164.         setMovement(RIGHT);                 // Turn right
  165.         delay(TURN_DELAY);                  // Delay for turning
  166.         setMovement(FORWARD);               // Resume forward movement
  167.         return;                             // Skip further line-following logic
  168.     }
  169.  
  170.     // Line Following Logic
  171.     if (leftIR == LOW && rightIR == LOW) {  // Both sensors on the line
  172.         setMovement(FORWARD);
  173.     } else if (leftIR == LOW) {  // Only left sensor on the line
  174.         setMovement(LEFT);
  175.     } else if (rightIR == LOW) {  // Only right sensor on the line
  176.         setMovement(RIGHT);
  177.     } else {  // Neither sensor on the line
  178.         setMovement(STOP);
  179.     }
  180. }
  181.  
  182. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement