Advertisement
pleasedontcode

"Line Following" rev_01

Dec 17th, 2024
52
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: "Line Following"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-18 03:08:36
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* make a line follower and obstacle avoiding code */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. long getDistance(); // Added function prototype for distance measurement
  32. void lineFollow(); // Added function prototype for line following
  33. void moveForward(); // Added function prototype for moving forward
  34. void stopMotors(); // Added function prototype for stopping motors
  35. void turnRight(); // Added function prototype for turning right
  36. void turnLeft(); // Added function prototype for turning left
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t more_HC-SR04_Echo_PIN_D3 = 3;
  40. const int irLeftPin = 2;    // IR sensor left
  41. const int irRightPin = 3;   // IR sensor right
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t more_HC-SR04_Trigger_PIN_D2 = 2;
  45. const int trigPin = 4;      // Ultrasonic Trigger Pin
  46. const int echoPin = 5;      // Ultrasonic Echo Pin
  47. const int motorLeftPin1 = 6; // Motor driver left motor control 1
  48. const int motorLeftPin2 = 7; // Motor driver left motor control 2
  49. const int motorRightPin1 = 8; // Motor driver right motor control 1
  50. const int motorRightPin2 = 9; // Motor driver right motor control 2
  51.  
  52. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  53. bool more_HC-SR04_Trigger_PIN_D2_rawData = 0;
  54.  
  55. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  56. float more_HC-SR04_Trigger_PIN_D2_phyData = 0.0;
  57.  
  58. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  59. // Create an instance of the Ultrasonic class
  60. Ultrasonic ultrasonic(trigPin, echoPin);
  61.  
  62. void setup(void)
  63. {
  64.     // put your setup code here, to run once:
  65.     pinMode(more_HC-SR04_Echo_PIN_D3, INPUT);
  66.     pinMode(more_HC-SR04_Trigger_PIN_D2, OUTPUT);
  67.    
  68.     // Initialize the pins for the IR sensors and motors
  69.     pinMode(irLeftPin, INPUT);
  70.     pinMode(irRightPin, INPUT);
  71.     pinMode(trigPin, OUTPUT);
  72.     pinMode(echoPin, INPUT);
  73.     pinMode(motorLeftPin1, OUTPUT);
  74.     pinMode(motorLeftPin2, OUTPUT);
  75.     pinMode(motorRightPin1, OUTPUT);
  76.     pinMode(motorRightPin2, OUTPUT);
  77.    
  78.     Serial.begin(9600); // For debugging
  79. }
  80.  
  81. void loop(void)
  82. {
  83.     // put your main code here, to run repeatedly:
  84.     updateOutputs(); // Refresh output data
  85.    
  86.     // Get distance from ultrasonic sensor
  87.     long distance = getDistance();
  88.    
  89.     // Check for obstacles using ultrasonic sensor
  90.     if (distance < 10) {
  91.         // Obstacle detected, stop and turn
  92.         stopMotors();
  93.         delay(500);  // Wait for a moment
  94.         turnRight(); // Turn to avoid obstacle
  95.     } else {
  96.         // No obstacle detected, follow the line
  97.         lineFollow();
  98.     }
  99. }
  100.  
  101. void updateOutputs()
  102. {
  103.     digitalWrite(more_HC-SR04_Trigger_PIN_D2, more_HC-SR04_Trigger_PIN_D2_rawData);
  104. }
  105.  
  106. // Function to measure the distance using ultrasonic sensor
  107. long getDistance() {
  108.     digitalWrite(trigPin, LOW);  // Clear the Trigger pin
  109.     delayMicroseconds(2);
  110.     digitalWrite(trigPin, HIGH); // Send trigger pulse
  111.     delayMicroseconds(10);
  112.     digitalWrite(trigPin, LOW);
  113.    
  114.     long duration = pulseIn(echoPin, HIGH);  // Measure the duration of echo pulse
  115.     long distance = (duration / 2) / 29.1;  // Calculate the distance in cm
  116.     return distance;
  117. }
  118.  
  119. // Function for line following
  120. void lineFollow() {
  121.     int leftSensor = digitalRead(irLeftPin);
  122.     int rightSensor = digitalRead(irRightPin);
  123.    
  124.     if (leftSensor == LOW && rightSensor == LOW) {
  125.         // Both sensors on the line, move forward
  126.         moveForward();
  127.     } else if (leftSensor == LOW && rightSensor == HIGH) {
  128.         // Left sensor on the line, turn left
  129.         turnLeft();
  130.     } else if (leftSensor == HIGH && rightSensor == LOW) {
  131.         // Right sensor on the line, turn right
  132.         turnRight();
  133.     } else {
  134.         // Both sensors off the line, turn right or left to re-align
  135.         turnRight();
  136.     }
  137. }
  138.  
  139. // Move forward
  140. void moveForward() {
  141.     digitalWrite(motorLeftPin1, HIGH);
  142.     digitalWrite(motorLeftPin2, LOW);
  143.     digitalWrite(motorRightPin1, HIGH);
  144.     digitalWrite(motorRightPin2, LOW);
  145. }
  146.  
  147. // Stop motors
  148. void stopMotors() {
  149.     digitalWrite(motorLeftPin1, LOW);
  150.     digitalWrite(motorLeftPin2, LOW);
  151.     digitalWrite(motorRightPin1, LOW);
  152.     digitalWrite(motorRightPin2, LOW);
  153. }
  154.  
  155. // Turn right
  156. void turnRight() {
  157.     digitalWrite(motorLeftPin1, HIGH);
  158.     digitalWrite(motorLeftPin2, LOW);
  159.     digitalWrite(motorRightPin1, LOW);
  160.     digitalWrite(motorRightPin2, LOW);
  161.     delay(300); // Turn for 300ms
  162.     moveForward(); // Resume moving forward
  163. }
  164.  
  165. // Turn left
  166. void turnLeft() {
  167.     digitalWrite(motorLeftPin1, LOW);
  168.     digitalWrite(motorLeftPin2, LOW);
  169.     digitalWrite(motorRightPin1, HIGH);
  170.     digitalWrite(motorRightPin2, LOW);
  171.     delay(300); // Turn for 300ms
  172.     moveForward(); // Resume moving forward
  173.  
  174. }
  175.  
  176. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement