PID Motor control problem

Hi can anyone help me please I am trying to use PID control to control the position of a dc motor. I am using a potentiometer as feed back and input and sending the signals to drive the motor to a h bridge. I have tried to piece together a code but with no luck. Does anyone Know where I'm going wrong? i have attached the code

POSITION.ino (1.51 KB)

Does anyone Know where I'm going wrong?

Sure. First, fix the encoder feedback issues and get your code to compile.

Next write the setpoint, the input and then output to the serial console in that order every half second or so. Not so fast that you cannot read a line before the next is printed.

Set Kp to 1. Set Ki and Kd to 0. Compile and upload with no voltage on the motor driver.

Rotate the motor shaft both directions. Does the feedback increment and decrement? If not, get that working.

Adjust the setpoint to 50. Turn the motor shaft so the feedback is 50. The pid output should be zero. Turning the shaft in either direction should make the pid output go positive or negative with the output equal to setpoint minus the input. It will max out and +/- 255.

Apply motor power.

If the pid output is non-zero, the motor should be turning. If it's zero, change the setpoint.

Eventually the feedback should match the setpoint and the motor should stop. It might overshoot and stay there, that's okay. That's when the real fun starts and you learn about pid tuning...

Opps, forgot to mention that you cannot use delay() anywhere, it prevents the pid loop from running. You can use this code to achieve the timed print:

unsigned long lastPrintTime;

void loop() {
... snip
  if (millis() - lastPrintTime >= 500UL) {
    lastPrintTime = millis();
    Serial.print(" ");
    Serial.print(" ");