D1 Mini ESP8266 Wifi and IOT button - help embedding two functions when IOT switch is activated

I am a complete noob to programming and Arduino. I have been trying to setup an W1 ESP2866 mini to the IOT cloud and program it to turn and LED on, while this LED is on i also want a tilt sensor to be on which when it detects movement it will a seperate LED. I have got the code working where i can turn the LED on and off with the IOT dashboard and the tilt sensor can work and flicker the sensor LED separately, but what i really want to do is only have the sensor/led indicator working when the IOT LED is turned on. How to i embed these two pieces of code together, whenever i try it crashes the W1.

This is the code with the two seperate things working together, any advice or help would be much appreciated

/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
Arduino IoT Cloud

Arduino IoT Cloud Variables description

The following variables are automatically generated and updated when changes are made to the Thing

CloudLight button;

Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

const int ledpin = 5;
const int biteled = 14;
const int tilt = 15;

void setup() {
pinMode(ledpin, OUTPUT);
pinMode(tilt, INPUT);
pinMode(biteled, OUTPUT);
digitalWrite(14, LOW);

// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(3500);

// Defined in thingProperties.h
initProperties();

// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);

/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(4);
ArduinoCloud.printDebugInfo();
}

void loop() {
ArduinoCloud.update();
// Your code here

int digitalVal = digitalRead(15);
if(HIGH == digitalVal){//if tilt switch is not breakover}
digitalWrite(biteled,HIGH);
Serial.println(digitalVal); //turn the led off
}
else {
digitalWrite(biteled,LOW);//turn the led on
Serial.println("hi");
}

}

/*
Since Button is READ_WRITE variable, onButtonChange() is
executed every time a new value is received from IoT Cloud.
*/
void onButtonChange() {

if (button == 1)
{
digitalWrite(ledpin, HIGH);

}

else
{
digitalWrite(ledpin, LOW);

}
// Do something
}.

Hi, please edit your post above so that the code is posted correctly, using code tags. Also please read the forum guide so you can avoid breaking any other forum rules! Thanks!

PS. Please click Tools->Auto Format in the IDE to correct the indentation of the code before posting it.