Pause and resume main loop

Hi

Would appreciate any hint regarding this problem. I'm working on a robot-vehicle sketch at the moment. The vehicle is driven around using two motors and it has a lift and a conveyor belt (each driven by a separate motor) for handling load. There is also a number of proximity and contact sensors to control all of these motors and vehicle movement. The main program is placed in a loop and it consists of a series of motor runs controlled either by a while loop or delay() commands. Here's a sample pseudo code:

void loop () {
while (sensor_A == LOW) {
drive lift motor up;
}
drive LHS_motor;
drive RHS_motor;
delay(3000)
stop LHS_motor;
stop RHS_motor;

etc...

}

The whole loop takes about 90sec which is the time for the vehicle to approach the loading station, pick up the load, move to the unloading station, unload and come back to the starting point.

Here's the problem: The vehicle is equipped with a proximity sensor which is to detect unexpected obstacles along the way. What I want is to implement this in the code in such a way that when this sensor is active (i.e. obstacle detected) the main code would simply pause (all motors to stop, the vehicle stops dead) at whatever point the main loop may then be and then resume when the obstacle is removed. I'm not sure if I should use interrupts for this as I don't want any particular action to be undertaken upon the sensor's signal but simply to pause the program and the stopping time required is not at the order of micro seconds.

It's time to rethink the whole thing with delay()-less, millis()-based timing, methinks.

Start with BlinkWithoutDelay in the IDE Examples > 2. Digital

Pause and resume main loop

This is altogether the wrong mindset

You need to write code that allows loop() to repeat hundreds or thousands of times per second. That means there must be NO use of delay() and NO use of a FOR or WHILE that takes more than a few microseconds to complete.

Have a look at the code in these links

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing. It may help with understanding the technique.

And Planning and Implementing a Program.

...R

Rename your loop() function to something else, for instance: drive_the_car()

Write a new loop() function whose job it is to invoke driive_the_car() when conditions are such that it ought to be invoked.