If you have hardware interrupts enabled, it will jump out of any code routine, execute whatever code you have in your interrupt handler, then return to where it was prior to the interrupt.

Note I wasn't recommending you use this routine to control your motors, I was just curious if it worked. Once you know what you'll need to do to handle all the other things you need to like light sensing, ultrasonic, etc, then you'll have some idea of how to incorporate all your routines (or most) into a single interrupt handler.

Your particular type of application could also benefit from a few dedicated application specific ICs like maybe some 8-pin PICs programmed to do nothing but generate your motor control signals by just monitoring a few inputs, and using these inputs to determine speed, direction, etc. Then you main controller could control motors by just taking a pin or 2 high or low.

A lot of robotics applications use distributed processing like this to take some of the repetitive tasks off of the primary controller.

Example: Instead of using a sonar sensor, you could build this into your main controller. But then your main controller wastes a ton of time doing what the external sonar sensor does.

So - most people will just buy & use a sonar sensor rather than design it into the main circuit. Just take that a step further, and make your own 8-pin servo controllers.

If you have a lot of things going on, motors, sensors, measurements, etc, etc, you can take a huge burden off the main controller by just creating a few of your own litle application specific PICs with a few I/O-pins on the primary controller telling them what to do.

You could have a whole herd of little 8-pin PICs interfaced & controlled by the primary controller, and it only takes a few pins on the primary to control them, and just a few instruction cycles to make that happen.

Or you could shove it all into just one with some crafty interrupt handler to sort it all out. Just an idea. Sounds like a fun project.

Microchip has a new 8-pin PICs coming shortly with a hardware USART & other goodies that could make some really interesting little peripheral controllers.