An interrupt generated by a timer every 1 ms, calls the speed control function. When the speed control function is called, it checks the tachometer timer to see if a falling edge of Hall sensor 1 is detected. The time stamp for the change in the sensor is compared to the previous time and the current speed of rotation of the motor is calculated. To add damping to the speed control for noise filtering, the speed variable (speed) is adjusted by only one step towards the measured speed.
The ADC samples the speed setting voltage (on analog pin SPEEDSET). If the setting has changed, it steps the speed command by one value towards the new target for noise filtering. Every fifty iterations through the speed control loop (50 ms), the difference between the measured speed and the speed command is used to calculate the step adjustment to the PWM duty cycle using Equation 4. The step based adjustments to the PWM, measured speed, and speed setting dampen the instantaneous changes and produce smooth transitions in speed in response to changes in setting or rotation.