PID Inverted Pendulum

Working with a mpu6050 and a stepper motor to create a PID controlled response. However im not sure where to apply the PID control. Would i have a constant number of steps, constant speed or a combination of the two? Any feedback is appreciated. The code is listed down below.

#include <Wire.h>

//I/O
const int MPU_addr = 0x68;
int PUL = 7; //define Pulse pin
int DIR = 6; //define Direction pin
int ENA = 5; //define Enable Pin

//VARIABLES
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
int minVal = 265;
int maxVal = 402;
float elapsedTime, time, timePrev;
float SPEED, current_degree, degree_error, degree_previous_error, degree_previous_error_2;
char DIRECTION;
int period = 50;  //Refresh rate period of the loop is 50ms
int Step_Interval = 1000;
int angle_tolerance = 0.5;

//PID
float kp = 8;
float ki = 0.2;
float kd = 3100;
float angle_setpoint = 0;
float PID_p, PID_i, PID_d, PID_t;

void setup() {
  //analogReference(EXTERNAL);
  Serial.begin(115200);
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  pinMode (PUL, OUTPUT);
  pinMode (DIR, OUTPUT);
  pinMode (ENA, OUTPUT);
  time = millis();
}

void loop() {
  if (millis() > time + period) {
    time = millis();

    //ANGLE MEASUREMENT
    Wire.beginTransmission(MPU_addr);
    Wire.write(0x3B);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU_addr, 14, true);
    AcX = Wire.read() << 8 | Wire.read();
    AcY = Wire.read() << 8 | Wire.read();
    AcZ = Wire.read() << 8 | Wire.read();
    int xAng = map(AcX, minVal, maxVal, -90, 90);
    int yAng = map(AcY, minVal, maxVal, -90, 90);
    int zAng = map(AcZ, minVal, maxVal, -90, 90);
    current_degree = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
    if (current_degree > 180) {
      current_degree = current_degree - 360;
    }

    //ERROR COMPARISON
    degree_error = angle_setpoint - current_degree;

    //PID
    //PROPORTIONAL CONTROL
    PID_p = kp * (degree_error - degree_previous_error);
    //INTERGRAL CONTROL
    PID_i = ki * period * degree_error;
    //DERIVATIVE CONTROL
    PID_d = ((degree_error - (2 * degree_previous_error) + degree_previous_error_2) / (period));
    //TOTAL
    PID_t = PID_p + PID_i + PID_d;

    //MOTOR CONTROL
    if (abs(degree_error) > angle_tolerance) {
      SPEED = 25;
      if (degree_error > angle_setpoint) {
        for (int i = 0; i < (Step_Interval); i++) {
          DIRECTION = 'L';
          digitalWrite(DIR, LOW);
          digitalWrite(ENA, HIGH);
          digitalWrite(PUL, HIGH);
          delayMicroseconds(SPEED);
          digitalWrite(PUL, LOW);
          //digitalWrite(ENA, LOW);
        }
      }
      if (degree_error < angle_setpoint) {
        for (int i = 0; i < (Step_Interval); i++) {
          DIRECTION = 'R';
          digitalWrite(DIR, HIGH);
          digitalWrite(ENA, HIGH);
          digitalWrite(PUL, HIGH);
          delayMicroseconds(SPEED);
          digitalWrite(PUL, LOW);
          //digitalWrite(ENA, LOW);
        }
      }
    }

    //SERIAL MONITOR VALUES
    Serial.print("Current Degree:");
    Serial.print(current_degree, 2);
    Serial.print("\t");
    Serial.print("Degree Error:");
    Serial.print(degree_error, 2);
    Serial.print("\t");
    Serial.print("   MOVING:"  );
    Serial.print(DIRECTION);
    Serial.print("\t");
    Serial.print("   Speed:"  );
    Serial.print(SPEED, 2);
    Serial.print("\t");
    Serial.print("   PID_p:"  );
    Serial.print(PID_p, 2);
    Serial.print("\t");
    Serial.print("   PID_i:"  );
    Serial.print(PID_i, 2);
    Serial.print("\t");
    Serial.print("   PID_d:"  );
    Serial.print(PID_d, 2);
    Serial.print("\t");
    Serial.print("   PID_t:"  );
    Serial.print(PID_t, 2);
    Serial.println();

    //PREVIOUS ERRORS
    degree_previous_error_2 = degree_previous_error;
    degree_previous_error = degree_error;
  }
}

Generally one applies the PID control in the manner and direction required to correct the error.

Hard for us to guess what that would be, since you forgot to tell us about your project.

Thanks for the response! Sorry let me clarify. I have an MPU 6050 attached to the top of a free rotating pendulum mounted to a track system. A belt connects to a stepper motor in order to adjust and correct the error. However, there are three things you need to control a stepper motor, direction speed and steps. We have our calculated PID value. Im just not sure where it would go in this system. Usually in a block control form you would multiply it with the systems transfer function to get a corrected response. This is all new to me.

The control theory analysis of your mechanical system model should make it clear which variable to change.

My guess would be the horizontal position of the pendulum pivot.