MPU6050 programming issue for motor control

Hi Guys,

I am a bit stuck here, I need a method of voiding the loop ideally through a switch. Currently the circuit i have running does not integrate this as i am currently testing and calibrating the code.

The problem is that the motor loops between on and off status rapidly as the gx changes due to the second if statements function. I am looking for a way to use the gx to turn off the motor until a reset occurs (Switch flipped on and off again) So am hoping there is a simple function i have missed?

Code and Diagram attatched

#include <Wire.h>
#include<I2Cdev.h>
#include<MPU6050.h>
 
MPU6050 mpu;
 
int16_t ax, ay, az;
int16_t gx, gy, gz;
 
#define pin1 3
#define pin2 5
 
void setup(){
 Serial.begin(9600);
 Serial.println("Initialize MPU");
 mpu.initialize();
 //Serial.println(mpu.testConnection() ? "Connected" : "Connection failed"); pinMode(pin1,OUTPUT);
 pinMode(pin2,OUTPUT);
 
}
 
void loop(){
 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
 
 ax = map(ax, -17000, 17000, -1500, 1500);
 
 //Serial.println(ax);

 if(gx<-200){
  if(gx>-1000){
  Serial.println("go");
  analogWrite(pin1, 255);
 }
 else{
  Serial.println("Stop");
  analogWrite(pin1, 0);
 }
 }
 delay(100);
}

Would this do what you want?

else{
  Serial.println("Stop");
  analogWrite(pin1, 0);
  while (1);
 }

Duane has the perfect solution for the problem. If you want to add an external reset button just change while(1) to while(YourButtonPinReadState != 1)

Cheers guys, knew there would be a really simple way of doing it!