scanning a sensor, with a timeout faul window

I’m having trouble with the fault window, for how long a sensor is allowed to be scan,before the fault window times out, and I’m having trouble getting it to work, when the scan time is exceeded and the FAULTfatal flag needs to be set, where it should be

// **********************************************************************************
//   home z-axis sub routine
//
int HOMEzaxis() {
    P1.writeDiscrete(LOW,ZaxisDIRECTION); //z-axis direction control 0=UP / 1= DOWN
    float dutyCycle = 50.15;  //sets dutyCycle variable to 50.15
    int frequency = 900;  //sets frequency variable to 1000Hz
      delay(50);  //waits 50 mseconds
    P1.writePWM(dutyCycle, frequency, 3, 1);  //The values to be passed in are duty cycle, frequency, slot, channel
  for (x = 0; x < 40; x ++) // fault timer 100*50mS = 5 second window to make sensor or FAULTzone
  {
    ZAXIShasHOMED = P1.readDiscrete(ZaxisHOME);
    if (ZAXIShasHOMED == true) {     // bail out on z-axis home sensor detected
      x = 0;
      FAULTfatal = 1; // set FAULTfatal flag, machine will be in fault
      break; 
      int frequency = 0;  //sets frequency variable to 0Hz stop PWM
      P1.writePWMFreq(frequency, 3, 1); //writePWMFreq can be used to change only the frequency
      P1.writeDiscrete(LOW, GREENlight); //green light control= OFF
      return
      FAULTfatal = 1; // set FAULTfatal flag, machine will be in fault
    }
    delay(50);
    //FAULTfatal = 1; // set FAULTfatal flag, machine will be in fault
  }
} 
// **********************************************************************************

What is it supposed to do?

What do you get (HIGH or LOW) when the limit switch is reached?