Conditional Interruption in a predefined path

Hi,
I am a total dummy to arduino and programming. I am working on a project where I have 2 motors (one for each wheel of the cart) and a PING))) ultrasonic sensor connected to Arduino.
I cant figure out how I wold be able to make a program that soes the following.

The cart starts and follows a certain route. for example moves forward for 1 minute, pauses, turns left moves in reverse for 2 minutes etc etc. This part I was able to accomplish.

Now, what I would like is that, whenever the PING sensor detects a hurdle, the cart pauses in its route and when the hurdle goes away, the cart RESUMES from its last position.

What I end up with is the cart starts from the beginning instead of resuming its operation.

Thanks.

The problem is on line 76 of the code you didn't show.

Sorry. This is what I have at the moment. There are two problems here ( in my eyes):

  1. The ping test is performed only after a movement set is complete.
  2. When the there is no hurdle, the cart starts from the beginning of the movement cycle.

Thanks.

const int pingPin = 5;
unsigned int duration, inches;

int MLA = 7;
int MLB = 8;

int MRA = 2;
int MRB = 4;

int MRPWM = 9;
int MLPWM = 10;

int MREN = 6;
int MLEN = 12;

//unsigned char MRCS = A0;
//unsigned char MLCS = A1;

void setup(){
Serial.begin(115200);
pinMode (MRA, OUTPUT);
pinMode (MRB, OUTPUT);
pinMode (MRPWM, OUTPUT);
pinMode(MREN, OUTPUT);
pinMode (MLA, OUTPUT);
pinMode (MLB, OUTPUT);
pinMode (MLPWM, OUTPUT);
pinMode(MLEN, OUTPUT);


}
void loop() {
pingTest();

if (inches < 15)   {
  pause();
//  exit(0);
  }
else {
  forward();
  delay(3000);
  pause();
  delay(1000);
  left();
  delay(2000);
  pause();
  delay(1000);
  reverse();
  delay(3000);
  pause();
  delay(1000);
  }

}

void forward() {
digitalWrite(MREN,HIGH);
digitalWrite(MLEN,HIGH);

digitalWrite(MRA,HIGH);
digitalWrite(MRB,LOW);
digitalWrite(MLA,HIGH);
digitalWrite(MLB,LOW);
analogWrite(MRPWM,255); //Right Motor at full speed
analogWrite(MLPWM,255); //Left Motor at full speed
}

void reverse () {
digitalWrite(MREN,HIGH);
digitalWrite(MLEN,HIGH);

digitalWrite(MRA,LOW);
digitalWrite(MRB,HIGH);
digitalWrite(MLA,LOW);
digitalWrite(MLB,HIGH);
analogWrite(MRPWM,255); //Right Motor at full speed
analogWrite(MLPWM,255); // Left Motor at full speed
}



void left() {
digitalWrite(MREN,HIGH);
digitalWrite(MLEN,HIGH);

digitalWrite(MREN,HIGH);
digitalWrite(MLEN,HIGH);
digitalWrite(MRA,HIGH);
digitalWrite(MRB,LOW);
digitalWrite(MLA,HIGH); 
digitalWrite(MLB,LOW);
analogWrite(MRPWM,255); //Right Motor at full speed
analogWrite(MLPWM,0); // Left Motor stopped
}

void right() {
digitalWrite(MREN,HIGH);
digitalWrite(MLEN,HIGH);

digitalWrite(MRA,HIGH); 
digitalWrite(MRB,LOW);
digitalWrite(MLA,HIGH);
digitalWrite(MLB,LOW); 
analogWrite(MRPWM,0); // Right Motor stopped
analogWrite(MLPWM,255); // Left Motor at full speed
}

void pause() {
digitalWrite(MREN,LOW);
digitalWrite(MLEN,LOW);
}

void pingTest() {
  pinMode(pingPin, OUTPUT);          // Set pin to OUTPUT
  digitalWrite(pingPin, LOW);        // Ensure pin is low
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       // Start ranging
  delayMicroseconds(5);              //   with 5 microsecond burst
  digitalWrite(pingPin, LOW);        // End ranging
  pinMode(pingPin, INPUT);           // Set pin to INPUT
  duration = pulseIn(pingPin, HIGH); // Read echo pulse
  inches = duration / 148;        // Convert to inches
  Serial.print(inches);// Display result
  Serial.print(" INCHES");
  Serial.println();  
  delay(1000);
}

The problem is your liberal use of the delay() function. The Arduino can do nothing during a delay. Get rid of ALL the delay()s and use millis() to manage timing without blocking as illustrated in Several Things at a Time

...R

Hmm. That's awesome. Now allow me a few days to go through the sketch to understand it. Thanks a lot.