Using two outputs

Does "write to pin 10" also turn off pin 11 if it was previously on?

We need to see more of the code. That code executes in nanoseconds so it is not the source of your problem.

Note that 40kHz is kind of fast for a 16000kHz processor. That does not give you a lot of time to do calculations which may take a few thousand clock cycles. It depends on exactly what you are doing.