gy-521 Gyroscope + Accelerometer Module issues

i am trying to get RPM of the running motor.
RPM range is 5 to 30.
i put the circuit on the TOP of the motor.

i am using the gy521 and i am getting the gx value from the Gyroscope.

First of all i am not getting the correct value.
Second isssue is some time he showed the 10RPM, Even on the same Speed some Time he showed 60RPM.

Even the correct Value of RPM is 19.

Here is my Code

#include<Wire.h>
#include <SPI.h>
#include <TFT_ILI9163C.h>
#include <Adafruit_GFX.h>
#include "gyro_accel.h"


#define __DC 6
#define __CS 10
#define __RST 12
#define	GREEN   0x07E0
#define	BLACK   0x0000


#define dt 20                       // time difference in milli seconds
#define rad2degree 57.3              // Radian to degree conversion
#define Filter_gain 0.95             // e.g.  angle = angle_gyro*Filter_gain + angle_accel*(1-Filter_gain)


// *********************************************************************
//    Global Variables
// *********************************************************************
unsigned long t=0; // Time Variables
float angle_x_gyro=0,angle_y_gyro=0,angle_z_gyro=0,angle_x_accel=0,angle_y_accel=0,angle_z_accel=0,angle_x=0,angle_y=0,angle_z=0;
// *********************************************************************

TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC, __RST);

int cont = 0;
int Totalx= 0;
int Totaly= 0;
int Totalz= 0;

// Main Code
void setup(){
  Serial.begin(115200);
  Wire.begin();
  MPU6050_ResetWake();
  MPU6050_SetDLPF(6); // Setting the DLPF to inf Bandwidth for calibration
  MPU6050_OffsetCal();
  MPU6050_SetGains(0,0); // Setting the DLPF to lowest Bandwidth
  
  // Welcome screen on LCD

tft.begin();
tft.setCursor(5, 80);
tft.setTextColor(GREEN);
tft.setTextSize(2);
tft.print("WelKommen");
delay(200);
  
  t= millis();
}

void loop(){
  t=millis(); 
  
  MPU6050_ReadData();
 
  angle_x_gyro = (gyro_x_scalled*((float)dt/1000)+angle_x);
  angle_y_gyro = (gyro_y_scalled*((float)dt/1000)+angle_y);
  angle_z_gyro = (gyro_z_scalled*((float)dt/1000)+angle_z);

  angle_z_accel = atan(accel_z_scalled/(sqrt(accel_y_scalled*accel_y_scalled+accel_x_scalled*accel_x_scalled)))*(float)rad2degree;
  angle_y_accel = -atan(accel_y_scalled/(sqrt(accel_y_scalled*accel_y_scalled+accel_z_scalled*accel_z_scalled)))*(float)rad2degree;
  angle_x_accel = atan(accel_x_scalled/(sqrt(accel_x_scalled*accel_x_scalled+accel_z_scalled*accel_z_scalled)))*(float)rad2degree;

  angle_x = Filter_gain*angle_x_gyro+(1-Filter_gain)*angle_x_accel;
  angle_y = Filter_gain*angle_y_gyro+(1-Filter_gain)*angle_y_accel;
  angle_z = Filter_gain*angle_z_gyro+(1-Filter_gain)*angle_z_accel;
  
  
  gyro_x_scalled<0? gyro_x_scalled=gyro_x_scalled *(-1):0;   // Convert Angle into Postive 
  
  double rpmx = (gyro_x_scalled*60 )/(2.0*3.1415);  // Angle to RPM
 
  
//  Serial.print("rpm  =  ");
//  Serial.print(rpmx);
  
  
// Here i am getting average of 10 Value and then i am printing on screen

  if (cont < 10)
  {Totalx = Totalx + rpmx;
   cont += 1;}
  else 
  {tft.fillRect(0,80,170,20,BLACK);
  tft.setCursor(2, 80);
  tft.setTextColor(GREEN);
  tft.setTextSize(2);
  tft.print(Totalx/cont);
  
// Serial.print("   Total  =  ");
//  Serial.println(Totalx/cont);
  
  Totalx =0;
  
  cont = 0;
  }
  
  while((millis()-t) < dt){ // Making sure the cycle time is equal to dt
  // Do nothing
  }
}

TOP of the motor

Which way is up? Is the circuit attached to the spinning shaft or some other part of the motor? Which gyro axis is aligned with the rotation?

I can see lots of problems with the code but explaining the problems needs the above questions answered. Remember a picture is worth a thousand words.

MorganS:
I can see lots of problems with the code but explaining the problems needs the above questions answered. Remember a picture is worth a thousand words.

Here is the Pic

Normally a picture with more than a thousand pixels is required to fulfill the thousand-word promise. That's too tiny!

MorganS:
Normally a picture with more than a thousand pixels is required to fulfill the thousand-word promise. That's too tiny!

i Hope you can get an idea.what i am doing...