looking for working code to drive motor by encoder

Hello everyone;

I am professional newbie in PID controlling. I have a "rotary inverted pendulum" project given to do.

I have a YUMO encoder, DC motor, Arduino uno and Arduino motor shield R3. I wanna implement a code which is sensitive to drive my motor according to the movements of my encoder. I have a sample code to drive the motor according to encoder.

#define encoder0PinA 2

#define encoder0PinB 4

int encoder0Pos = 0;
int motorSpeed = 0;

void setup() {
  
  pinMode(encoder0PinA, INPUT);
  pinMode(encoder0PinB, INPUT);
pinMode(13,OUTPUT);
pinMode(11,OUTPUT);
  // encoder pin on interrupt 0 (pin 2)
  
  attachInterrupt(0, doEncoder, RISING);
  Serial.begin(9600);
  }
  void loop(){
  //motorSpeed = round(abs(-((encoder0Pos*360/1024)%360)*255.0/360));
  motorSpeed = abs((encoder0Pos)/4);
  if (motorSpeed>255)
  motorSpeed= 255;

  if(encoder0Pos>0){
 
    digitalWrite(13,HIGH);
  analogWrite(11,motorSpeed);
  } else if (encoder0Pos<0){

    digitalWrite (13,LOW);
    analogWrite(11,motorSpeed);
    } else 
    motorSpeed=0;
 Serial.println(motorSpeed);
}

void doEncoder() {
  // look for a low-to-high on channel A
  if (digitalRead(encoder0PinB) == LOW) {
    encoder0Pos = encoder0Pos + 1; //CW
  }else { 
    encoder0Pos = encoder0Pos - 1; //CCW
    
  }

  
}

At the end of my code, I wanna get a result like in the video. (starting from 1:44-2:17). 1.Enkoder nedir? Nasıl çalışır? (Bağlantısı, şeması, plc, arızaları, ayarı nasıl yapılır? a b z ) - YouTube

What should be the possible outlook of the code?

I would be glad if you can help me. If I can jump this step up, I will try to develop the code to keep the antenna in balance by using the encoder.

Thank you for reading.

What is your code doing now?

I am trying this but it stilldoes not work do you know what is wrong about this?

#define encoder0PinA 2

#define encoder0PinB 3

int encoder0Pos = 0;
int motorSpeed = 0;
int preEncoder;
int preEncoderReadTime;

void setup() {
  
  pinMode(encoder0PinA, INPUT);
  pinMode(encoder0PinB, INPUT);
pinMode(13,OUTPUT);
pinMode(11,OUTPUT);
  // encoder pin on interrupt 0 (pin 2)
  
  attachInterrupt(0, doEncoder, RISING);
  preEncoder = 0;
  preEncoderReadTime = 0;
  Serial.begin(9600);
  }
  
  void loop(){
    int currentTime = millis();
    int deltaTime =currentTime - preEncoderReadTime ;
    preEncoderReadTime = currentTime;
   float encoderPosition = ((encoder0Pos*360/1024)%360);
  //motorSpeed = round(abs(-*255.0/360));
   int deltaEncoder = encoderPosition - preEncoder;
   preEncoder = encoderPosition;
  motorSpeed = abs((deltaEncoder)/deltaTime);
  if (motorSpeed>255)
  motorSpeed= 255;
delay(1000);
  if(encoderPosition>0){
 
    digitalWrite(13,HIGH);
  analogWrite(11,motorSpeed);
  } else if (encoderPosition<0){

    digitalWrite (13,LOW);
    analogWrite(11,motorSpeed);
    } else 
    motorSpeed=0;
 Serial.println(motorSpeed);
}

void doEncoder() {
  // look for a low-to-high on channel A
  if (digitalRead(encoder0PinB) == LOW) {
    encoder0Pos = encoder0Pos + 1; //CW
  }else { 
    encoder0Pos = encoder0Pos - 1; //CCW
    
  }

  
}

Try getting your encoder to work with one of the sample programs from here:

http://playground.arduino.cc/Main/RotaryEncoders

then come back to yours and see what's missing.