how to replace analog pot to digital ?

this code is to position control a Dc motor with a analog feed back pot

and my question is how can i replace the analog feed back with a digital rotary encoder for a continuous rotation ?

byte motor_p1 = 4;
byte motor_p2 = 5;
byte pwmPin = 6;
int currentAngle;
int requiredAngle;
int errorAmount;
int remappedErrorAmount;
byte acceptableError = 4; //for smoothing

void setup()
{
  Serial.begin(9600);
  pinMode(motor_p1, OUTPUT);
  pinMode(motor_p2, OUTPUT);
  pinMode(pwmPin, OUTPUT);
}

void loop()
{
  readAndConditionAngle();
  readAndConditionRequiredAngle();
  if (currentAngle<requiredAngle){
    digitalWrite(motor_p1, HIGH);                      
    digitalWrite(motor_p2, LOW);
  } if (currentAngle>requiredAngle){
    digitalWrite(motor_p1, LOW);                      
    digitalWrite(motor_p2, HIGH);
  }
  calculateErrorAmount(currentAngle,requiredAngle);
  analogWrite(pwmPin,remappedErrorAmount);
  Serial.println(currentAngle);
 }
 
 void calculateErrorAmount(int currentAngle,int requiredAngle){
  errorAmount=abs(currentAngle-requiredAngle);
  remappedErrorAmount=map(errorAmount, 0, 1000, 0, 255);
  if (remappedErrorAmount<acceptableError){
    remappedErrorAmount=0;
  }
 }
 
void readAndConditionAngle() {
  currentAngle = analogRead(0);
  if (currentAngle<200){
   currentAngle=200; 
  } if (currentAngle>800) {
   currentAngle=800; 
  }
}

void readAndConditionRequiredAngle() {
  requiredAngle = analogRead(1);
  if (requiredAngle<200){
   requiredAngle=200; 
  } if (requiredAngle>800) {
   requiredAngle=800; 
  }
}

code credits goes to