Read LISY300AL gyro output

Hi to all!! I am Giovanni from Milan, I am working on a IMU from sparkfun. I am going crazy to get the value from this gyro. The otput is angular velocity and i am triyng to explain integrals to arduino. Very complex.

The code seems work but not precisley, it loose some degrees every 90 degrees.

int sensorPin = 2;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
float angularVelocity = 0;  // variable to store the value coming from the sensor
float previousAngularVelocity = 0;
double orientation = 9000;
double previousOrientation = 9000;
long previousTime = 0;
unsigned long time;

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
  pinMode(sensorPin, INPUT);  
  Serial.begin(9600);
}

void loop() {
  //orientation = previousOrientation;
  for ( int i = 0; i < 11 ; i++) {
   long treshold = 320.999;
   angularVelocity = (analogRead(sensorPin) - treshold) + angularVelocity;
   if (i == 10) {
     angularVelocity = (angularVelocity / 10) * 0.75;
     break;
   }
  }
  if (angularVelocity - previousAngularVelocity > - 1 && angularVelocity - previousAngularVelocity < 1) {
    angularVelocity = previousAngularVelocity;
  }
  time = millis() - previousTime;
  previousTime = millis();  
  orientation = orientation + (angularVelocity * time);   
  if (orientation - previousOrientation > - 1 && orientation - previousOrientation < 1 ) {
    orientation = previousOrientation; 
  } 
  
  Serial.println(orientation / 100); 
}

with this piece:
if (orientation - previousOrientation > - 1 && orientation - previousOrientation < 1 ) {
orientation = previousOrientation;
}
I am triyng to smooth the value and correct the oscillation. Another problem is that i have to regulate the treshold value if energy source changes. I think i have to write a auto-calibration for().
Some one have some advise to me?
This is my first time with gyros.

anyone has some advise? I am going crazy