Trouble with using encoder and pid library together

Thank you for replying. Could you suggest an established library? To be very frank, I didn't write most of the code, ChatGPT played a heavy part in it. I'd love to be able to make the code simpler and more robust.