pleasedontcode

Line Follower rev_01

Oct 13th, 2025
247
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 Follower
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2025-10-13 15:47:35
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* All black stop */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. int readLine();
  32. void moveForward(int leftSpeed, int rightSpeed);
  33. bool allSensorsBlack(void);
  34.  
  35. /****** GLOBAL VARIABLES AND PIN DEFINITIONS *****/
  36. // Sensor pins: RLS08 sensors mapped to Nano pins (including A0A2 as analog inputs)
  37. int sensorPins[8] = {2, 3, 4, 7, 8, A0, A1, A2};
  38. int sensorValues[8];
  39.  
  40. // TB6612FNG motor driver pins mapped to Nano
  41. #define PWMA 5   // Left motor speed (PWM)
  42. #define PWMB 6   // Right motor speed (PWM)
  43. #define AIN1 10
  44. #define AIN2 11
  45. #define BIN1 12
  46. #define BIN2 13
  47. #define STBY 9   // Standby
  48.  
  49. // === Motor Speed Settings ===
  50. int baseSpeed = 150;  // normal forward speed (0255)
  51.  
  52. // === PID Variables ===
  53. float Kp = 25;   // proportional gain
  54. float Ki = 0.0;  // integral gain
  55. float Kd = 15;   // derivative gain
  56.  
  57. int lastError = 0;
  58. float integral = 0;
  59.  
  60. // ----------------------------------------------------------------------
  61. void setup(void)
  62. {
  63.   Serial.begin(9600);
  64.  
  65.   // Sensor pins
  66.   for (int i = 0; i < 8; i++) {
  67.     pinMode(sensorPins[i], INPUT);
  68.   }
  69.  
  70.   // Motor driver pins
  71.   pinMode(PWMA, OUTPUT);
  72.   pinMode(PWMB, OUTPUT);
  73.   pinMode(AIN1, OUTPUT);
  74.   pinMode(AIN2, OUTPUT);
  75.   pinMode(BIN1, OUTPUT);
  76.   pinMode(BIN2, OUTPUT);
  77.   pinMode(STBY, OUTPUT);
  78.  
  79.   digitalWrite(STBY, HIGH); // Enable motor driver
  80.  
  81.   Serial.println("PID Line Follower Ready...");
  82. }
  83.  
  84. // ----------------------------------------------------------------------
  85. void loop(void)
  86. {
  87.   // All black stop condition: if all sensor inputs read LOW (0), stop the robot
  88.   if (allSensorsBlack()) {
  89.     moveForward(0, 0);
  90.     Serial.println("All black stop triggered"); // new: indicate stop condition
  91.     delay(10);
  92.     return;
  93.   }
  94.  
  95.   int position = readLine(); // get error from sensors
  96.   int error = position;
  97.  
  98.   // === PID Calculation ===
  99.   integral += error;
  100.   int derivative = error - lastError;
  101.   int correction = (Kp * error) + (Ki * integral) + (Kd * derivative);
  102.  
  103.   int leftSpeed = baseSpeed - correction;
  104.   int rightSpeed = baseSpeed + correction;
  105.  
  106.   // limit speeds between 0255
  107.   leftSpeed = constrain(leftSpeed, 0, 255);
  108.   rightSpeed = constrain(rightSpeed, 0, 255);
  109.  
  110.   // move motors
  111.   moveForward(leftSpeed, rightSpeed);
  112.  
  113.   lastError = error;
  114.  
  115.   delay(10); // loop delay
  116. }
  117.  
  118. // ----------------------------------------------------------------------
  119. // === Read Line Function ===
  120. int readLine()
  121. {
  122.   int weightedSum = 0;
  123.   int sum = 0;
  124.  
  125.   for (int i = 0; i < 8; i++) {
  126.     sensorValues[i] = digitalRead(sensorPins[i]);
  127.  
  128.     // invert logic if needed (0=black, 1=white  so we flip)
  129.     int value = (sensorValues[i] == 0) ? 1 : 0;
  130.  
  131.     if (value == 1) {
  132.       weightedSum += (i * 100); // position weight (0700)
  133.       sum += 1;
  134.     }
  135.   }
  136.  
  137.   if (sum == 0) {
  138.     // no line detected  keep last error
  139.     return lastError;
  140.   }
  141.  
  142.   int position = weightedSum / sum;   // average position
  143.   int error = position - 350;         // center = 350
  144.   return error;
  145. }
  146.  
  147. // ----------------------------------------------------------------------
  148. // === Motor Control ===
  149.  
  150. // Move Forward
  151. void moveForward(int leftSpeed, int rightSpeed)
  152. {
  153.   digitalWrite(AIN1, HIGH);
  154.   digitalWrite(AIN2, LOW);
  155.   digitalWrite(BIN1, HIGH);
  156.   digitalWrite(BIN2, LOW);
  157.   analogWrite(PWMA, leftSpeed);
  158.   analogWrite(PWMB, rightSpeed);
  159. }/* Baseline Code Empty */
  160.  
  161. // ----------------------------------------------------------------------
  162. // Helper: check if all sensors read black (LOW)
  163. bool allSensorsBlack(void)
  164. {
  165.   for (int i = 0; i < 8; i++) {
  166.     int v = digitalRead(sensorPins[i]);
  167.     if (v != 0) {
  168.       return false; // at least one sensor not on black
  169.     }
  170.   }
  171.   return true; // all sensors are LOW (black)
  172. }
  173.  
  174. /* END CODE */
  175.  
Advertisement
Add Comment
Please, Sign In to add comment