As the drive calculation is a part of the PID regulator section, why do you want to switch on and off outside the background work ???

IF you had a constant time period for switching ( 2 sec on, 2 off ; 1 sec on, 3 off ...) it would be so easier to drive ...

I do not understand why you want so much difficulties ...

Definitely bye !

Alain