digitalRead same pin, same loop = crash?

I've an arduino yun Shield (Genuino Yun Shield) connected to my MEGA 2560 and having issues with it crashing with certain code.

Sometimes, the connected device on deviceStatePin flickers (It's opto-isolated) so drives the INPUT LOW (on) then within a few MS drives it back HIGH (off) so I need a way of debouncing. I thought the second method should of worked fine but it fails and just starts all over again @ "Ready..." (It fails straight after the final SendMessage)

Runs fine:

void setup() {
  pinMode(deviceStatePin, INPUT);

  Serial3.println("Ready...");

}

int deviceStatePin   = 22;
boolean deviceStatus = false; /* false = OFF | true = ON */
int lastDeviceState  = 1;
int deviceState      = 1;

void loop() {

  deviceState = digitalRead(deviceStatePin);

  checkDeviceState();

  lastDeviceState = deviceState;
}


void checkDeviceState()
{

  if (deviceState != lastDeviceState) {
    if (deviceState == HIGH)
    {
      Serial3.println("Device powered Down");
      deviceStatus = false;
      canLog = false;

      client.stop();
    }
    else
    {
      Serial3.println("Device powered Up");
      deviceStatus = true;
      Serial3.println("Waiting...");

      fileTimestamp = getTimeStamp();
      dataFileName = "/mnt/sda1/xxxxxxx/logs/" + fileTimestamp + ".txt";
      dataFileName.toCharArray(c_dataFileName, 45);

      delay(3000); /* Give device chance to warm up */
      Serial3.println("Ready to send clear data");
      SendMessage((char*)""); /* Clear data */
      delay(1000);
      SendMessage((char*)""); /* Clear data */
    }
  }
}

FAILS (Basically restarts again):

void setup() {
  pinMode(deviceStatePin, INPUT);

  Serial3.println("Ready...");

}

int deviceStatePin   = 22;
boolean deviceStatus = false; /* false = OFF | true = ON */
int lastDeviceState  = 1;
int deviceState      = 1;

void loop() {

  deviceState = digitalRead(deviceStatePin);

  checkDeviceState();

  lastDeviceState = deviceState;
}


void checkDeviceState()
{

  if (deviceState != lastDeviceState) {
    if (deviceState == HIGH)
    {
      Serial3.println("Device powered Down");
      deviceStatus = false;
      canLog = false;

      client.stop();
    }
    else
    {
      Serial3.println("Device powered Up");

      delay(1000); /* Allow to see if it's flickering */
      deviceState = digitalRead(deviceStatePin); /* Reset deviceState encase it's fallen HIGH again */
      if (deviceState == HIGH)
      {
        Serial3.println("Flickered");
      }
      else
      {

          deviceStatus = true;
          Serial3.println("Waiting...");

          fileTimestamp = getTimeStamp();
          dataFileName = "/mnt/sda1/xxxxxxx/logs/" + fileTimestamp + ".txt";
          dataFileName.toCharArray(c_dataFileName, 45);

          delay(3000); /* Give device chance to warm up */
          Serial3.println("Ready to send clear data");
          SendMessage((char*)""); /* Clear data */
          delay(500);
          SendMessage((char*)""); /* Clear data */
        }
    }
  }
}