Implementation of D (differentiation) in PID
I have 3 points to share :

1-I will tell you why people mess with D term interms of sign , PID= P + I + D but , D = kd* (error -olderror) , and in position control if you are approaching the set point , always olderror is &lt; error so D term will be negative , and thus it decreases the output and prevent the overshoot.

2-Regarding time , donot divide or use time . since you are using embedded microcontroller use just make a time constant and cancel it from all equations : for example use a timer interrupt to generate 1ms time constant and call your function inside .

3-Why you are using PID for velocity control , 90% of the time PI is enough in speed control , since D here is more like accelaration control .

Hope that helps