I haven't got time to look through all that code but in principle you have
loop () {
read_switch_code();
control_motor_code();
}
read_switch_code() {
// code to read a switch
}
control_motor_code() {
// code to control the motor
}
and the switch and motor parts of the code do not interact in any way.
This can be changed to something like this
boolean kill = false;
loop () {
read_switch_code();
control_motor_code();
}
read_switch_code() {
// code to read a switch
if (switch is set)
kill = true;
}
control_motor_code() {
// code to control the motor
if (kill)
stop the motor
}
Now the switch code can tell the motor code what to do.
Rob