HELP WITH SEGWAY ROBOT

I need help with my segway robot. I need it to balance. By looking at my code what does it look like will happen. currently my robot will just spin its wheels randomly. I know its not much info but any help is appreciated. i am using a l298n mpu6050 and lego dc motors.

#include <Wire.h>
#include <MPU6050.h>
int16_t ax, ay, az;
int16_t gx, gy, gz;
int val;
int val2;
MPU6050 mpu;
int a1 = 4;
int a2 = 5;
int b1 = 7;
int b2 = 8;

void setup() {

  Wire.begin();
  mpu.initialize();
  pinMode(a1, OUTPUT);
  pinMode(a2, OUTPUT);
  pinMode(b1, OUTPUT);
  pinMode(b2, OUTPUT);
  digitalWrite(a1, HIGH);
  digitalWrite(a2, HIGH);
  digitalWrite(b1, HIGH);
  digitalWrite(b2, HIGH);
  Serial.begin(9600);
}

void loop() {

  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  val = map(ax, -15000, 15000, 0, 500);
  val2 = map(ax, -15000, 15000, 500, 0);
  if (val > 250) {
    digitalWrite(a1, HIGH);
    digitalWrite(a2, LOW);
    digitalWrite(b1, LOW);
    digitalWrite(b2, HIGH);
  } else {
    digitalWrite(a1, LOW);
    digitalWrite(a2, HIGH);
    digitalWrite(b1, HIGH);
    digitalWrite(b2, LOW);
  }
  if (val2 < 250) {
    digitalWrite(a1, LOW);
    digitalWrite(a2, HIGH);
    digitalWrite(b1, HIGH);
    digitalWrite(b2, LOW);
  } else {
    digitalWrite(a1, HIGH);
    digitalWrite(a2, LOW);
    digitalWrite(b1, LOW);
    digitalWrite(b2, HIGH);
  }
  analogWrite(3, val + val2 / 2);
  analogWrite(6, val + val2 / 2);
}

Try putting in some serial print statements so you can see what values you are actually receiving and mapping to.

Also, what you are doing with val2 is nonsense. It's derived directly from ax just like val, and appears to be the inverse. So do the map only once and get val2 from val. Perhaps you don't even need a val2. The name "val2" tells us nothing about what it is supposed to do. Start using meaningful variable names.

Better names for your variables would help in understanding the code.

What is attached to pins 3 and 6 and why aren't they defined as outputs ?