Arduino Micro accelerometer not working with external power supply?

Hi,

So I wrote a code for my Arduino Micro to do a couple of functions with an RF Receiver. I used a GY-521 MPU 6050 Accelerometer + Gyro to detect motion, and all works fine when the Arduino is powered by a micro USB, but when I tried to power it with an external source (9V Battery), everything works besides the accelerometer. Any help would be greatly appreciated.

Here is my code, forgive me if its a bit messy, as I am really new with Arduino, and coding in general. I know there is probably a lot to be optimized, but my main concern right now is the accelerometer not working with the external.

int signalPin = 1;
int buzzerLightPin = 4;
int buzzerPin = 5;
int armPin = A1;
int disarmPin = A2;
int panicPin = A3;
int arm = 0;
int disarm = 0;
int armLightPin = 6;
int warning = 0;
int alarmCount = 0;
int panic = 0;
#include<Wire.h>
const int MPU = 0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int setAcX = 0;
int setAcY = 0;
int setAcZ = 0;
int setGyX = 0;
int setGyY = 0;
int setGyZ = 0;

void setup ()
{
  pinMode(signalPin, INPUT);
  pinMode(buzzerLightPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(armLightPin, OUTPUT);
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);
  Wire.write(0);     
  Wire.endTransmission(true);
}

void loop ()
{
  arm = analogRead(armPin);
  panic = analogRead(panicPin);
  if (panic == 1023)
  {delay(700);
   panic = analogRead(panicPin);
   if (panic == 1023)
   {delay(700);
    panic = analogRead(panicPin);
    if (panic == 1023)
    {while ((alarmCount < 20) && (disarm != 1023))
         {
           disarm = analogRead(disarmPin);
           alarmCount = (alarmCount + 1);
           digitalWrite(buzzerLightPin, HIGH);
           digitalWrite(buzzerPin, HIGH);
           delay(1000);    
           digitalWrite(buzzerLightPin, LOW);
           digitalWrite(buzzerPin, LOW);
           delay(1000); 
         }
         alarmCount = 0;
         disarm = 0;
    }
   }
  }
  if (digitalRead(signalPin = HIGH))
  {
    digitalWrite(buzzerLightPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
    delay(100);
    digitalWrite(buzzerLightPin, LOW);
    digitalWrite(buzzerPin, LOW);
    delay(200);    
    digitalWrite(buzzerLightPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
    delay(100);    
    digitalWrite(buzzerLightPin, LOW);
    digitalWrite(buzzerPin, LOW);
    delay(200);   
  }
   if (arm == 1023)
  {
    digitalWrite(buzzerLightPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
    delay(50);
    digitalWrite(buzzerLightPin, LOW);
    digitalWrite(buzzerPin, LOW);
    delay(50);    
    digitalWrite(buzzerLightPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
    delay(50);    
    digitalWrite(buzzerLightPin, LOW);
    digitalWrite(buzzerPin, LOW);
    delay(50);   
    digitalWrite(buzzerLightPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
    delay(50);    
    digitalWrite(buzzerLightPin, LOW);
    digitalWrite(buzzerPin, LOW);
    delay(50);
    setAcX = AcX;
    setAcY = AcY;
    setAcZ = AcZ;
    setGyX = GyX;
    setGyY = GyY;
    setGyZ = GyZ;
    do
    {
    digitalWrite(armLightPin, HIGH);
    disarm = analogRead(disarmPin);
    Wire.beginTransmission(MPU);
    Wire.write(0x3B);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU,14,true);
    AcX=Wire.read()<<8|Wire.read();  
    AcY=Wire.read()<<8|Wire.read();
    AcZ=Wire.read()<<8|Wire.read();
    Tmp=Wire.read()<<8|Wire.read();
    GyX=Wire.read()<<8|Wire.read();
    GyY=Wire.read()<<8|Wire.read();
    GyZ=Wire.read()<<8|Wire.read();
    delay(300);
    digitalWrite(armLightPin, LOW);
    delay(300);
    panic = analogRead(panicPin);
  if (panic == 1023)
  {delay(700);
   panic = analogRead(panicPin);
   if (panic == 1023)
   {delay(700);
    panic = analogRead(panicPin);
    if (panic == 1023)
    {while ((alarmCount < 20) && (disarm != 1023))
         {
           disarm = analogRead(disarmPin);
           alarmCount = (alarmCount + 1);
           digitalWrite(buzzerLightPin, HIGH);
           digitalWrite(buzzerPin, HIGH);
           delay(1000);    
           digitalWrite(buzzerLightPin, LOW);
           digitalWrite(buzzerPin, LOW);
           delay(1000); 
         }
         alarmCount = 0;
         disarm = 0;
    }
   }
  }
    if (((AcX - setAcX) + (AcY - setAcY) + (AcZ - setAcZ) + (GyX - setGyX) + (GyY - setGyY) + (GyZ - setGyZ)) >= 5000)
      {
       if (warning < 3)
       {
       digitalWrite(buzzerLightPin, HIGH);
       digitalWrite(buzzerPin, HIGH);
       delay(1000);
       digitalWrite(buzzerLightPin, LOW);
       digitalWrite(buzzerPin, LOW);
       delay(1000);    
       digitalWrite(buzzerLightPin, HIGH);
       digitalWrite(buzzerPin, HIGH);
       delay(1000);    
       digitalWrite(buzzerLightPin, LOW);
       digitalWrite(buzzerPin, LOW);
       delay(1000);   
       digitalWrite(buzzerLightPin, HIGH);
       digitalWrite(buzzerPin, HIGH);
       delay(1000);    
       digitalWrite(buzzerLightPin, LOW);
       digitalWrite(buzzerPin, LOW);
       delay(1000); 
       warning = (warning + 1);
       }
       if (warning >= 3)
       {
         while ((alarmCount < 20) && (disarm != 1023))
         {
           disarm = analogRead(disarmPin);
           alarmCount = (alarmCount + 1);
           digitalWrite(buzzerLightPin, HIGH);
           digitalWrite(buzzerPin, HIGH);
           delay(1000);    
           digitalWrite(buzzerLightPin, LOW);
           digitalWrite(buzzerPin, LOW);
           delay(1000); 
         }
         alarmCount = 0;
       }
      }
    setAcX = AcX;
    setAcY = AcY;
    setAcZ = AcZ;
    setGyX = GyX;
    setGyY = GyY;
    setGyZ = GyZ;
    } while (disarm != 1023);
    warning = 0;
    digitalWrite(buzzerLightPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
    delay(50);
    digitalWrite(buzzerLightPin, LOW);
    digitalWrite(buzzerPin, LOW);
    delay(50);    
    digitalWrite(buzzerLightPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
    delay(50);    
    digitalWrite(buzzerLightPin, LOW);
    digitalWrite(buzzerPin, LOW);
    delay(50);   
    digitalWrite(buzzerLightPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
    delay(50);    
    digitalWrite(buzzerLightPin, LOW);
    digitalWrite(buzzerPin, LOW);
    delay(50);[/b]
  }
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();   
  AcY=Wire.read()<<8|Wire.read(); 
  AcZ=Wire.read()<<8|Wire.read(); 
  Tmp=Wire.read()<<8|Wire.read();  
  GyX=Wire.read()<<8|Wire.read();  
  GyY=Wire.read()<<8|Wire.read();  
  GyZ=Wire.read()<<8|Wire.read(); 
  delay(333);
}

This is the part of the code that doesn’t work with the external:

   if (((AcX - setAcX) + (AcY - setAcY) + (AcZ - setAcZ) + (GyX - setGyX) + (GyY - setGyY) + (GyZ - setGyZ)) >= 5000)
      {
       if (warning < 3)
       {
       digitalWrite(buzzerLightPin, HIGH);
       digitalWrite(buzzerPin, HIGH);
       delay(1000);
       digitalWrite(buzzerLightPin, LOW);
       digitalWrite(buzzerPin, LOW);
       delay(1000);    
       digitalWrite(buzzerLightPin, HIGH);
       digitalWrite(buzzerPin, HIGH);
       delay(1000);    
       digitalWrite(buzzerLightPin, LOW);
       digitalWrite(buzzerPin, LOW);
       delay(1000);   
       digitalWrite(buzzerLightPin, HIGH);
       digitalWrite(buzzerPin, HIGH);
       delay(1000);    
       digitalWrite(buzzerLightPin, LOW);
       digitalWrite(buzzerPin, LOW);
       delay(1000); 
       warning = (warning + 1);
       }
       if (warning >= 3)
       {
         while ((alarmCount < 20) && (disarm != 1023))
         {
           disarm = analogRead(disarmPin);
           alarmCount = (alarmCount + 1);
           digitalWrite(buzzerLightPin, HIGH);
           digitalWrite(buzzerPin, HIGH);
           delay(1000);    
           digitalWrite(buzzerLightPin, LOW);
           digitalWrite(buzzerPin, LOW);
           delay(1000); 
         }
         alarmCount = 0;
       }
      }
    setAcX = AcX;
    setAcY = AcY;
    setAcZ = AcZ;
    setGyX = GyX;
    setGyY = GyY;
    setGyZ = GyZ;

Sorry this whole thing is kind of long, but please do let me know if any more information is required, I would be glad to give it here. I just didn’t want this first post to be too long and boring. Thanks so much for your time.