nila1:
In order not to repeat the command analogWrite (), can I put an if statement that updates the pwm provided for reference only when the value changes, right?
Yes. But I guess doing the analogWrite continuously is OK if it is working now. I suggested you avoid doing that in case it was adding to the problem.