Guest User

I need help with my code. I'm trying to make a code with PID control.

a guest
Jul 13th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.78 KB | Source Code | 0 0
  1. // Motor pin definitions
  2. int _output_PD5 = 5;  // rear left motor control
  3. int _output_PD4 = 4;  // front left motor control
  4. int _output_PD7 = 6;  // rear right motor control
  5. int _output_PD6 = 7;  // front right motor control
  6.  
  7. int _output_PWM10 = 10; // right motor speed
  8. int _output_PWM9 = 9;   // left motor speed
  9.  
  10. // Sensor pin definitions
  11. int sensors[5] = {A0, A1, A2, A3, A4}; // Analog sensors
  12. int values[5];  // for reading sensor values
  13.  
  14. // PID variables
  15. float Kp = 10;
  16. float Ki = 0;
  17. float Kd = 5;
  18.  
  19. int baseSpeed = 20; // Base speed
  20.  
  21. long weightedSum = 0;     // Weighted sum of sensor values
  22. long position = 0;        // Position (on X-axis) of the line
  23. int error = 0;            // PID error
  24.  
  25. void setup() {
  26.   // Set pinMode for motor control
  27.   pinMode(_output_PD5, OUTPUT);
  28.   pinMode(_output_PD4, OUTPUT);
  29.   pinMode(_output_PD7, OUTPUT);
  30.   pinMode(_output_PD6, OUTPUT);
  31.  
  32.   pinMode(_output_PWM10, OUTPUT); // Right motor PWM pin
  33.   pinMode(_output_PWM9, OUTPUT);  // Left motor PWM pin
  34.  
  35.   // Set pinMode for analog sensors
  36.   for (int i = 0; i < 5; i++) {
  37.     pinMode(sensors[i], INPUT);
  38.   }
  39.  
  40.   // Start serial communication
  41.   Serial.begin(9600);
  42. }
  43.  
  44. void loop() {
  45.   // Read values from sensors
  46.   for (int i = 0; i < 5; i++) {
  47.     values[i] = 1023 - analogRead(sensors[i]); // Invert sensor readings
  48.   }
  49.  
  50.   weightedSum = 0;
  51.   position = 0;
  52.  
  53.   // Calculate weighted sum for line position
  54.   for (int i = 0; i < 5; i++) {
  55.     weightedSum += values[i];
  56.     position += (long)values[i] * (i * 100); // Weighting the sensors
  57.   }
  58.  
  59.   error = position / weightedSum - 200; // Calculate line position error
  60.  
  61.   // Calculate PID correction (simple example)
  62.   int leftSpeed = baseSpeed - Kp * error - Ki * weightedSum - Kd * (error);
  63.   int rightSpeed = baseSpeed + Kp * error + Ki * weightedSum + Kd * (error);
  64.  
  65.   // Apply correction to motors
  66.   controlMotors(leftSpeed, rightSpeed);
  67.  
  68.   // Debug: Show values on Serial Monitor
  69.   Serial.print("Error: ");
  70.   Serial.print(error);
  71.   Serial.print(" | Left Speed: ");
  72.   Serial.print(leftSpeed);
  73.   Serial.print(" | Right Speed: ");
  74.   Serial.println(rightSpeed);
  75.  
  76.   delay(100); // Small delay for control stability
  77. }
  78.  
  79. void controlMotors(int leftSpeed, int rightSpeed) {
  80.   // Add base speed to calculated speed
  81.   leftSpeed += baseSpeed;
  82.   rightSpeed += baseSpeed;
  83.  
  84.   // Ensure speed values are in 0–255 range
  85.   leftSpeed = constrain(leftSpeed, 0, 255);
  86.   rightSpeed = constrain(rightSpeed, 0, 255);
  87.  
  88.   // Control motors with calculated speed
  89.   digitalWrite(_output_PD4, 1);
  90.   digitalWrite(_output_PD5, 0);
  91.   digitalWrite(_output_PD6, 1);
  92.   digitalWrite(_output_PD7, 0);
  93.  
  94.   analogWrite(_output_PWM9, rightSpeed);  // Left motor uses rightSpeed
  95.   analogWrite(_output_PWM10, leftSpeed);  // Right motor uses leftSpeed
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment