How to create a 38 Khz pulse with arduino using timer or PWM?

My code above is a bit dodgy at the edge conditions (pot at zero or 1023). They should really be detected and translated into some other output. After all, modulating with 100% duty cycle (or 0%) doesn't make any sense. That's because 100% on or 0% on isn't actually modulating at all.