I agree that DT's interrupts or ASM's are the way to go. I gave the ON INTERRUPT code above as an example to get started. Ken will still need to know how pre-scalers and such work no matter what.

Ken seems to want to know the nuts and bolts of things. In my opinion getting a simple code to work with ON INTERRUPTS is a crawl/walk thingy.

I am not real clear on how the motor driver works. I understand it as PWM greater than 127 turns one way and less than 127 turns the other way??

I just drove home trying to simulate Ken's car. Pulsed the same degrees left or right, more pulses left or right for turns but always the same, say 5 degrees off center, and after each pulse returned to center.

What about a two chip solution. The main chip will do nothing more than
center, pulse left, center, pulse right, center...

Second chip deals with the sonar. If a correction needs made a given pin is toggled.

The main chip is using an interrupt on change and completely interrupt driven. The main chip can deal with the pulse left, center, pulse right while the other chip is busy with the next calculation.

Just a half baked thought.