Problems with IR sensor and DC motors (Read 6996 times)

Hi guys
I new arduino user and I m making a programm to control DC motor from IR remote controller.
Everything goes well. But I have a problem when I send the command for a directoion to the motors, they don't stop, motors keep wheeling untils I sent another command. But what I want is the motors to wheel jujst for 1 second and then stop.
Here is the programm.
#include "IRremote.h"
#include "SR04.h"

#define ENABLE_RIGHT 5
#define DIRA_RIGHT 3
#define DIRB_RIGHT 4

#define ENABLE_LEFT 6
#define DIRA_LEFT 7
#define DIRB_LEFT 8

#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);

int receiver = 10; // Signal Pin of IR receiver to Arduino Digital Pin 11
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'

long distance;
long MOVE_SIDE_DURATION = 500;
long MOVE_FORWARD_DURATION = 500;
long MOVE_BACKWARD_DURATION = 500;
long COMPUTE_DISTANCE_DURATION = 200;
long STOP_DURATION = 50;

void setup() {
//---set pin direction
pinMode(ENABLE_RIGHT, OUTPUT);
pinMode(DIRA_RIGHT, OUTPUT);
pinMode(DIRB_RIGHT, OUTPUT);

pinMode(ENABLE_LEFT, OUTPUT);
pinMode(DIRA_LEFT, OUTPUT);
pinMode(DIRB_LEFT, OUTPUT);

irrecv.enableIRIn(); // Start the receiver
irrecv.blink13(true);
Serial.begin(9600);
}

void stopMotors() {
Serial.println("Stop motors");

digitalWrite(ENABLE_LEFT, HIGH);
digitalWrite(DIRA_LEFT, LOW);
digitalWrite(DIRB_LEFT, LOW);

digitalWrite(ENABLE_RIGHT, HIGH);
digitalWrite(DIRA_RIGHT, LOW);
digitalWrite(DIRB_RIGHT, LOW);
delay(STOP_DURATION);
}

void left() {
//move to left
Serial.println("Move to the left");
//analogWrite(ENABLE_RIGHT,255); //enable on
digitalWrite(ENABLE_RIGHT, HIGH); // enable on
digitalWrite(DIRA_RIGHT, LOW); //one way
digitalWrite(DIRB_RIGHT, HIGH);

digitalWrite(ENABLE_LEFT, HIGH); // enable on
digitalWrite(DIRA_LEFT, HIGH); //one way
digitalWrite(DIRB_LEFT, LOW);
delay(MOVE_SIDE_DURATION);
}

void right() {
//move to right
Serial.println("Move to the right");
//analogWrite(ENABLE_RIGHT,255); //enable on
digitalWrite(ENABLE_RIGHT, HIGH); // enable on
digitalWrite(DIRA_RIGHT, HIGH); //one way
digitalWrite(DIRB_RIGHT, LOW);

digitalWrite(ENABLE_LEFT, HIGH); // enable on
digitalWrite(DIRA_LEFT, LOW); //one way
digitalWrite(DIRB_LEFT, HIGH);
delay(MOVE_SIDE_DURATION);
}

void forward() {
//move forward
Serial.println("Move forward");
digitalWrite(ENABLE_RIGHT, HIGH); // enable on
digitalWrite(DIRA_RIGHT, LOW); //one way
digitalWrite(DIRB_RIGHT, HIGH);

digitalWrite(ENABLE_LEFT, HIGH); // enable on
digitalWrite(DIRA_LEFT, LOW); //one way
digitalWrite(DIRB_LEFT, HIGH);
delay(MOVE_FORWARD_DURATION);
}

void backward() {
//move backward
Serial.println("Move backward");
digitalWrite(ENABLE_RIGHT, HIGH); // enable on
digitalWrite(DIRA_RIGHT, HIGH); //one way
digitalWrite(DIRB_RIGHT, LOW);

digitalWrite(ENABLE_LEFT, HIGH); // enable on
digitalWrite(DIRA_LEFT, HIGH); //one way
digitalWrite(DIRB_LEFT, LOW);
delay(MOVE_BACKWARD_DURATION);
}

boolean wayIsFree()
{
distance = sr04.Distance();
Serial.println("distance");
Serial.print(distance);
Serial.println("cm");
delay(COMPUTE_DISTANCE_DURATION);
if (distance > 30)
{
return true;
}
else {
return false;
}
}

void loop() {

if (irrecv.decode(&results)) // have we received an IR signal?
{
switch (results.value)
{
case 0xFFA25D: Serial.println("POWER"); {
wayIsFree();
break;
}
case 0xFFE21D: Serial.println("FUNC/STOP"); {
stopMotors();
break;
}
case 0xFF629D: Serial.println("VOL+"); {
forward();
stopMotors();
break;
}
case 0xFF22DD: Serial.println("FAST BACK"); {
left();
stopMotors();
break;
}
case 0xFFC23D: Serial.println("FAST FORWARD"); {
right();
stopMotors();
break;
}
case 0xFFA857: Serial.println("VOL-"); {
backward();
stopMotors();
break;
}
default:
Serial.println(" other button ");
}
delay(500);
irrecv.resume(); // receive the next value
}
}

I have added the method to stop motors because, I was not stopped by himself.
Thanks

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

Why have you got "(Read 6996 times)" in your title?

...R