new Pwm code trying to combine the pros of timer and thread