pleasedontcode

Line-Following Robot rev_01

Dec 2nd, 2025
22
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 Robot
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-12-03 03:24:49
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* line followin robot with 2 ir sensor */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Ultrasonic.h>
  28. #include <Arduino.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t obstacle_detection_sensor_HC-SR04_Echo_PIN_D13 = 13;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t obstacle_detection_sensor_HC-SR04_Trigger_PIN_D4 = 4;
  39.  
  40. // Create Ultrasonic object
  41. Ultrasonic ultrasonicTrigger(obstacle_detection_sensor_HC-SR04_Trigger_PIN_D4, obstacle_detection_sensor_HC-SR04_Echo_PIN_D13);
  42.  
  43. // IR sensors for line follow
  44. const uint8_t IR_left_SENSOR_PIN = 14;
  45. const uint8_t IR_right_SENSOR_PIN = 15;
  46.  
  47. void setup(void) {
  48.     pinMode(IR_left_SENSOR_PIN, INPUT);
  49.     pinMode(IR_right_SENSOR_PIN, INPUT);
  50.     Serial.begin(9600);
  51. }
  52.  
  53. void loop(void) {
  54.     int IR_left_state = digitalRead(IR_left_SENSOR_PIN);
  55.     int IR_right_state = digitalRead(IR_right_SENSOR_PIN);
  56.  
  57.     int distanceCM = ultrasonicTrigger.read();
  58.     int distanceIN = ultrasonicTrigger.read(INC);
  59.  
  60.     // Determine movement based on IR sensors
  61.     if (IR_left_state == LOW && IR_right_state == LOW) {
  62.         // Both sensors on line, go forward
  63.         Serial.println("Moving forward");
  64.     } else if (IR_left_state == LOW && IR_right_state == HIGH) {
  65.         // Turn right
  66.         Serial.println("Turning right");
  67.     } else if (IR_left_state == HIGH && IR_right_state == LOW) {
  68.         // Turn left
  69.         Serial.println("Turning left");
  70.     } else {
  71.         // No line detected, stop or search
  72.         Serial.println("Searching for line");
  73.     }
  74.  
  75.     // Print ultrasonic distances
  76.     Serial.print("Distance CM: ");
  77.     Serial.println(distanceCM);
  78.     Serial.print("Distance IN: ");
  79.     Serial.println(distanceIN);
  80.  
  81.     delay(100);
  82. }
  83.  
  84. /* END CODE */
  85.  
Advertisement
Add Comment
Please, Sign In to add comment