Reverted duty cycle code because I'm a moron. It does work, and does less computation than mine.

(Shouldn't matter but I do realize your code worked after all. Sorry!)