Advertisement
NaroxEG

line follower 2

May 2nd, 2024 (edited)
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. // Define motor pins
  2. #define leftMotorForward 3
  3. #define leftMotorBackward 4
  4. #define leftMotorSpeed 5
  5. #define rightMotorForward 6
  6. #define rightMotorBackward 7
  7. #define rightMotorSpeed 8
  8.  
  9. #define leftSensorPin 10
  10. #define middleSensorPin 11
  11. #define rightSensorPin 12
  12.  
  13. float kp = 1;
  14. int setpoint = 100;
  15. int initialSpeed = 75;
  16.  
  17. void setup() {
  18.   pinMode(leftMotorForward, OUTPUT);
  19.   pinMode(leftMotorBackward, OUTPUT);
  20.   pinMode(leftMotorSpeed, OUTPUT);
  21.   pinMode(rightMotorForward, OUTPUT);
  22.   pinMode(rightMotorBackward, OUTPUT);
  23.   pinMode(rightMotorSpeed, OUTPUT);
  24.  
  25.   pinMode(leftSensorPin, INPUT);
  26.   pinMode(middleSensorPin, INPUT);
  27.   pinMode(rightSensorPin, INPUT);
  28. }
  29.  
  30. void loop() {
  31.   int leftSensorValue = digitalRead(leftSensorPin);
  32.   int middleSensorValue = digitalRead(middleSensorPin);
  33.   int rightSensorValue = digitalRead(rightSensorPin);
  34.  
  35.   float position = (leftSensorValue * 0 + middleSensorValue * 100 + rightSensorValue * 200) / (leftSensorValue + middleSensorValue + rightSensorValue);
  36.  
  37.   float error = position - setpoint;
  38.   float output = kp * error;
  39.  
  40.   // After drawing my chart, I found that having negative error means left is triggered so car needs to go left a bit, and positive error is opposite
  41.   // So here motor speeds depends on if there is an error or not, if so we need to move slower until we handle the error
  42.  
  43.   // لو الخطأ كان سالب يبقى عايز اروح شمال بالشكل ده الموتور الشمال هيكون أبطأ و اليمين أسرع
  44.   // لو الخطأ موجب يبقى عايز اروح يمين ف بالشكل ده الشمال هيكون اسرع و اليمين ابطأ
  45.   int leftMotorSpeedValue = initialSpeed + output;
  46.   int RightMotorSpeedValue = initialSpeed - output;
  47.  
  48.   // If I've added value to left motor so It should be moving forward, so it's greater than 0 for sure, and vice versa
  49.   if (leftMotorSpeedValue >= 0) {
  50.     digitalWrite(leftMotorForward, HIGH);
  51.     digitalWrite(leftMotorBackward, LOW);
  52.   } else {
  53.     digitalWrite(leftMotorForward, LOW);
  54.     digitalWrite(leftMotorBackward, HIGH);
  55.   }
  56.  
  57.   if (RightMotorSpeedValue >= 0) {
  58.     digitalWrite(rightMotorForward, HIGH);
  59.     digitalWrite(rightMotorBackward, LOW);
  60.   } else {
  61.     digitalWrite(rightMotorForward, LOW);
  62.     digitalWrite(rightMotorBackward, HIGH);
  63.   }
  64.  
  65.   // Making sure to take the less value (If motor speed greater than 255 it should take 255 which is highest PWM value)
  66.   analogWrite(leftMotorSpeed, min(abs(leftMotorSpeedValue), 255));
  67.   analogWrite(RightMotorSpeed, min(abs(RightMotorSpeedValue), 255));
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement