How to eliminate Wait and While?

Newbie question: I need my UNO to do a lot of things at the same time such as driving servos, checking digital inputs, waiting for certain analog values, etc. all at once. I don't want it to wait (even a few milliseconds) for a servo to position or wait, for example, for a return echo on the sonic sensor because other important momentary inputs, such as counting encoder pulses, could be missed. Also how do I prioritize functions? Do I use a lot of interrupts? Sample code would be very helpful. Thanks for any replies.

I don't want it to wait (even a few milliseconds) for a servo to position

It's a mechanical device; if it has to move, it may take tens or hundreds of milliseconds to get to where it is told.
If you want faster, you need a bigger energy budget.

Sample code would be very helpful.

For such a vague set of requirements, sample code will be so general as to be virtually useless.

AWOL:

I don't want it to wait (even a few milliseconds) for a servo to position

It's a mechanical device; if it has to move, it may take tens or hundreds of milliseconds to get to where it is told.
If you want faster, you need a bigger energy budget.

He means he doesn't want the program to pause while it moves into position.

Now, for answering the question.

The trick here is to prioritise. First make a list of everything that is going on.

Then, split that list into two sub lists - a list of things that you need to happen "NOW!!!", like reacting to encoders, etc, and one of things that can take time to happen, and you don't need to know about them immediately.

The things you need to happen "NOW!!!" you want to use interrupts for. Everything else you just have running in your main loop() and use polling for inputs, etc.

Then you implement a Finite State Machine. Or, probably more accurately, a number of small Finite State Machines.

Each component, or sub-assembly can have its own FSM, such as a simple little one for a servo, which has the states "IDLE", "MOVING", "ARRIVED", for example. Each FSM will have its own set of variables, for storing the current state, and any parameters for what it is operating.

The interrupts that get triggered when important events occur can do small amounts of processing of the incoming information or stimulus, and if needed modify the state or parameters of one or more of the FSMs.

Edit: I have grown so tired of explaining FSMs that I have written a tutorial all about them: http://hacking.majenko.co.uk/finite-state-machine

Thanks AWOL, but even a newbie understands that a servo is a mechanical device. You said

If you want faster, you need a bigger energy budget

I do not understand what that means. Do you mean I must pay more for electricity? Or use bigger wires? Or use thousands of volts instead of only five volts?

As majenko explained, I don't want to use Wait() functions (as used in most simple example programs) and have the program just wait and do nothing while the servo is moving or waiting for an echo from a sonic sensor and perhaps miss encoder input pulses.

Thanks majenko for the link to your tutorial. These techniques are needed in real-life real-time multitasking programming and I was looking for suggestions as to where I can see example code of these advanced techniques.

Start by understanding the blink without delay example.

Then add a FSM (Finite State Machine) using the case statement.

Then add, if still required, interrupts.

But do take it one step at a time.

Mark

If you want faster, you need a bigger energy budget

Means you need bigger faster servos.
In engineering budget means how things are divided up or how much of a thing you need. So we talk of a power budget like so many milli amps for the processor, so many for the LEDs and so on.

warren631:
I don't want it to wait (even a few milliseconds) for a servo to position or wait, for example, for a return echo on the sonic sensor because other important momentary inputs, such as counting encoder pulses, could be missed.

If you don't want to wait, don't. This may help a bit:

Interrupts have their uses, particularly for counting encoder pulses.

http://hacking.majenko.co.uk/finite-state-machine
This url times out. Is there another place to get this?

How to eliminate Wait and While?

Two simple approaches:

  1. use interrupts: you can think of interrupts as a simple way to prioritize tasks;
  2. use cycle counters: it is essentially a state machine approach;

Do I use a lot of interrupts?

I use interrupts extensively - they make coding so much simpler.