how can I add limit switch in to HC-SR04 to reverse the motor

Hello, I am making automatic toy car have
HC-SR04 distance sensor (to sense my hand)
2 limit switch (one to reverse the motor, one to stop the motor)

so what I am tying to do is

  1. sense my hand
  2. Car moves back
  3. back limit switch hit the wall
  4. Car moves forward
  5. front limit switch hit the wall
  6. Car stops


Please use bottom link to see the picture
https://drive.google.com/file/d/0B2d99J14zOkrVDZneW92bUc5WlU/

#define trigPin 9
#define echoPin 8
#define led 11
#define led2 10
#define limswitch 12
#define limswitch2 13

void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(2, OUTPUT); // this runs the motor front
pinMode(4, OUTPUT); // this runs the motor back
pinMode(12, OUTPUT);
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH); // delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) /29.1;
if (distance < 30) // This is where the LED On/Off happens
{
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
{
digitalWrite(4,HIGH);
digitalWrite(2,LOW);
delay(2000);
digitalWrite(2,HIGH);
digitalWrite(4,LOW);
delay(2000);
}
}
else
{
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
digitalWrite(2,LOW);
digitalWrite(4,LOW);
}
if (distance >= 500 || distance <= 0)
{
Serial.println("Out of range");
}
else
{
Serial.print(distance);
Serial.println(" cm");
}
}

This code is letting me to run the motor back 2sec and forward 2sec when it sense the hand front of distance sensor.

How can I add limit switch function to this?

TRYING.ino (1.33 KB)