Help needed with DC motor

I have been using a proportional-integral-derivative (PID) controller to give my robot smooth and reliable motion. However when active my motor is making noises.My code is attached below. Would appreciate some help.

ECED3901_MotorPID.ino (10.6 KB)

Motors always make noise. Why do you think there is a code problem?

Typically you will here the noise due to the differential term amplifying the noise from the position sensor.

The only way to reduce the noise is turn down the D-term (reducing performance), or increasing the resolution
of the encoder, or smoothing the estimate of the encoders rate-of-change.

Old style servomotor PID loops used the output of a tachogenerator to provide the speed signal independently
of an encoder, giving a continuous signal rather than a discrete, stepped signal to the differential term of the PID.

This performs way better.