While(1) Work around for MD10C

Hi Guys,

I was unsure if this should go into programming or project guidance so sorry if it is in the wrong place.

I am running the below script to control a motor through the use of data from an IMU (MPU6050)

I was using the while(1) statement to allow for ease of resetting the script as this is crucial to the application. However i have ran into some issues.

Is there any obvious work around? In order to have the switch wired into the arduino as well i would have to add a regulator. (Which i had but i may have fried it due to a personal mistake)

#include <Wire.h>
#include<I2Cdev.h>
#include<MPU6050.h>
 
MPU6050 mpu;
 
int16_t ax, ay, az;
int16_t gx, gy, gz;

//define pin name
//#define diy_pwm  A2
#define pwm      2
#define dir      3
//#define pot      3



void setup(){
  Serial.begin(9600);
  Serial.println("Initialize MPU");
  mpu.initialize();
  pinMode(pwm,OUTPUT);
  pinMode(dir,OUTPUT);
  //pinMode(diy_pwm,OUTPUT);
  //pinMode(pot,INPUT);

}

void loop()
{
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  ax = map(ax, -17000, 17000, -1500, 1500);

//Serial.println(ax);
  
  // LOCKED ANTI-PHASE MODE
  //control motor with program set
  
  if (ax<0){
  if(ax>-300){
   //int i=200; // i acts as the speed
   Serial.println(ax);
   digitalWrite(pwm,HIGH);		// always set PWM to HIGH
   analogWrite(dir,200); //dir is defined incorrectly this acts as the speed, reset as "i" if not functional

  }
   else{
   Serial.println(ax);
   digitalWrite(pwm,HIGH);
   analogWrite(dir,50); //This should slow motor enough to see the difference
   while(1);
  }
  }   
   delay(50);
   }

Note: The supply voltage is actually 18V

Any help is always appreciated guys!

Is this related to this post?

Yes it is, the previous was an initial proof of concept for a university project. The components in the above diagram are the final components chosen

I was using the while(1) statement to allow for ease of resetting the script as this is crucial to the application.

You are going to have to explain how using an infinite loop makes pressing the reset button easier. And, you are going to have to explain how resetting the Arduino is "crucial to the application".

In order to have the switch wired into the arduino as well i would have to add a regulator.

What switch? Why does it need a regulator?

PaulS:
You are going to have to explain how using an infinite loop makes pressing the reset button easier. And, you are going to have to explain how resetting the Arduino is “crucial to the application”.

I didn’t say anything about the button being easier to press :S

In relation to your other point:

Simply it works by the IMU stopping the motor when the acceleration data exceeds a value, the problem is that when the motor stops so to will the IMU as they are to be in a housing together.

When they both stop, without the while(1) function it will automatically start back up again. <— I don’t want this.

Rcarter93:
I didn't say anything about the button being easier to press :S

Why don't you just explain what you did mean? If multiple people can't understand what you mean, it's on you, not them.