PID control of motor position

I need to control position of dc motor 24V 5500rpm use serial monitor to input Angle and use Absolute Encoder 12 bit to be sensor but I don’t know how to set PID to control speed of motor.
Thank:)

int CLK = 10; // Blu Pin 10
int DO = 11; // Grn Pin 11
int CSn = 12; // Ylw Pin 12
const int pwm = 2 ;//initializing pin 2 as pwm
const int in_1 = 8 ;//turn right pin 8
const int in_2 = 9 ;//turn left pin 9

void setup() {
// put your setup code here, to run once:
pinMode(CSn, OUTPUT);// Chip select
pinMode(CLK, OUTPUT);// Serial clock
pinMode(DO, INPUT_PULLUP);// Serial data IN/OUT
pinMode(pwm,OUTPUT) ; //we have to set PWM pin as output
pinMode(in_1,OUTPUT) ; //Logic pins are also set as output
pinMode(in_2,OUTPUT) ;
digitalWrite(CSn, HIGH);
digitalWrite(CLK, HIGH);
Serial.begin(115200);
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,LOW) ;
analogWrite(pwm,255) ;
Serial.println();
Serial.begin(9600);//text for start
while (! Serial);
Serial.println(“In put your Angle”);
}

void loop() {
int Data = ReadSSI();
int Degree = map(Data, 0, 4095, 0, 359);
Serial.print("Reading = “); Serial.print(Data);
Serial.print(”, Degree = "); Serial.println(Degree);/// read Data and degree
delay(100);

if (Serial.available())
{ int Angle = Serial.parseInt();
if (Angle>0 && Angle <= 30){
if(Angle == Degree)
{ Serial.println();
Serial.println(“now Degree =”);Serial.println(Angle);
stop();
}
else
{ Serial.println();
Serial.println(“Turn right”);
turnright();
}
}
else if((Angle<0 && Angle >= -20)){
if(Angle == Degree )
{
Serial.println();
Serial.println(“now Degree =”);Serial.println(Angle);
stop();
}
else
{ Serial.println();
Serial.println(“Turn left”);
turnleft();
}
}
else if(Angle < -20| | Angle > 30){
Serial.println(“ERROR!!!”);
Serial.println(“Please input Angle between -20 to 30 Degree”);
stop();
}
}
}

void turnright()
{
digitalWrite(in_1,HIGH); //if Degree not = Angle ;turn right
digitalWrite(in_2,LOW) ;
}

void turnleft()
{
digitalWrite(in_1,LOW) ; //if Degree not = Angle ;turn left
digitalWrite(in_2,HIGH) ;
}

void stop()
{
digitalWrite(in_1,HIGH) ; //if Degree not = Angle ;turn left
digitalWrite(in_2,HIGH) ;
}

int ReadSSI(void)
{ int i, dReading;
char Resolution = 12;
unsigned int bitStart = 0x0800;
dReading = 0;
digitalWrite(CSn, LOW);
delayMicroseconds(5);
digitalWrite(CLK, LOW);
for (i = (Resolution - 1); i >= 0; i–)
{ digitalWrite(CLK, HIGH);
delayMicroseconds(5);
if (digitalRead(DO)) dReading |= bitStart;
digitalWrite(CLK, LOW);
delayMicroseconds(5);
bitStart = bitStart >> 1;
if (i == 0)
{ digitalWrite(CLK, HIGH);
if (digitalRead(DO)) dReading |= bitStart;
}
}
digitalWrite(CSn, HIGH);
return dReading;
}

The motor has some sort of gear box connected to it ?

Please edit your post to add code tags.

hammy: The motor has some sort of gear box connected to it ?

i have plan put it with steering mechanism to control wheel but now only process and PID.

Have a google , there are people who have made big servos using for example windscreen wiper motors - this would make your problem a lot easier to solve .

If you have an absolute encoder, you'll normally be controlling the position of the motor with a PID loop, not the speed.

However you can numerically differentiate the encoder reading to measure speed, and use that as the feedback input to the PID. Output of the PID is then motor drive level, set point is desired speed (in same units as your differentiated encoder signal).

Key points are to sample encoder value at regular timepoints and run the PID at those same timepoints. simple differencing of current and previous reading is probably all you need to get speed measurement.

[ Just noticed you are both saying control speed and control position. You need to be consistent! ]