Shock sensor is not running in the file I have faced an issue where the shock sensor is not running in the file. My idea is that when the ultrasonic sensor sense that when it is 4cm, the garage door will open. When the car reverses, the obstacle avoidance sensor will check if there is obstacle behind. These two sensors are working fine.

After that, shock sensor will be activated to see if the car has hit the wall.
But from shock sensor part onwards, the shock sensor does not run. I troubleshoot by putting the shock sensor code into another sketch and it works. 

I have no idea where I have done wrong. I appreciate any help

Update
I have modified the code to accommadate interrupts which is advised by CodeGorilla. I need help with how can I let the interrupt know that they can execute when it reaches the if (distance == 4). 

 #include &lt;Wire.h&gt;
#include &lt;LiquidCrystal_I2C.h&gt;

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display


const int trigPin = 9;
const int echoPin = 10;

long duration;
int distance;

const int shockPin = 3; // Use Pin 3 as Input


void setup() {

  Serial.begin(9600);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Distance left:");


  // Ultrasonic Sensor
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);


  // Obstacle Avoidance Sensor
  pinMode(8, INPUT);// set pin as input

  // Shock Sensor
  pinMode (shockPin, INPUT) ; // input from the KY-002

  attachInterrupt(digitalPinToInterrupt(3), detectShock, CHANGE);
  attachInterrupt(digitalPinToInterrupt(8), senseObstacle, CHANGE);
}

void detectShock ()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Detection: ");
  if (digitalRead(shockPin) == LOW) // If we're in an alarm state
  {
    lcd.setCursor(0, 1);
    lcd.print("Shock exists");
  }
  else
  {
    lcd.setCursor(0, 1);
    lcd.print("Shock is absent");
  }
}

void senseObstacle() {
  lcd.clear();
  lcd.setCursor(0, 0);
  //delay(300);
  lcd.print("Detection:");
  if (digitalRead(8) == LOW) {
    lcd.setCursor(0, 1);
    lcd.print("Obstacle Present");
  } else {
    lcd.setCursor(0, 1);
    lcd.print("No Obstacle");
  }


void loop() {

  // Clears the trigPin
  digitalWrite(trigPin , LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin , LOW);

  // Reads the echoPin, returns the sound wave
  // travel time in microseconds
  duration = pulseIn( echoPin , HIGH);

  distance = duration * 0.034 / 2;

  lcd.setCursor(0, 0);
  lcd.print("Distance left: ");

  lcd.setCursor(0, 1);
  lcd.print(distance);
  lcd.print(" CM");


  if (distance == 4) {
    // I am lost at here.
    lcd.setCursor(0, 1);
    lcd.print("Door Opens");
    delay(500);
    // Once the car enters, the obstacle avoidance sensor checks if there are obstacles in the way..
    delay(5000);
    lcd.clear();
  }

}



}
I'm pretty certain that your code is the problem.  You are relying on timing rather than events.  Using delays is OK when you are just interfacing to one sensor but when you start mixing them you end up with issues like this.  

You are reading the vibration sensor once every second if the distance is 4.  So any vibration needs to occur for more than one second, or at exactly the right time.  I think you need to look at a better way of coding the solution without using the big delay statements, maybe look at interrupts.