this is the loop and functions part of the code
void loop() {
lastLoopUsefulTime = millis()-loopStartTime;
if(lastLoopUsefulTime<STD_LOOP_TIME) delay(STD_LOOP_TIME-lastLoopUsefulTime);
lastLoopTime = millis() - loopStartTime;
loopStartTime = millis();
anglelast = angle;
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
// read pulse from x- and y-axes:
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,HIGH);
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,HIGH);
// convert the pulse width into acceleration
// accelerationX and accelerationY are in milli-g's:
// earth's gravity is 1000 milli-g's, or 1g.
accX = (((pulseX / 10) - 500) *

;
accY = (((pulseY / 10) - 500) *

;
accX = accX - acczeroX;
accY = accY - acczeroY;
Xangle = asin(accX / 1000.0);
Yangle = asin(accY / 1000.0);
Xangle = Xangle * (360 / (2*M_PI));
Yangle = Yangle * (360 / (2*M_PI));
getGyroValues();
Gyrox = Gyrox * 0.00875;
Gyroy = Gyroy * 0.00875;
Gyroz = Gyroz * 0.00875;
float dt = (millis() - lastLoopTime)/1000.0;
balanceg = (Gyroy) * 0.00875 * dt;
balancea = Yangle;
angle = ((1-ACC_GYRO_RATIO) * (angle + balanceg)) + ((ACC_GYRO_RATIO)*(balancea));
// like ANGLE_TORQUE_RATIO
Ki = 2.59; //analogRead(A0) / 1000.0; //0.49
Kp = 25.5; //analogRead(A1) / 10.0; //19.8
Kd = 54.0;//analogRead(A2) / 10.0; //44.0
p = (angle * Kp);
d = (angle - anglelast) * Kd;
i = i + (angle * Ki);
float pid = p + d + i;
pidOutput = pid;
pidOutput = constrain(pid, -255, 255);
if (abs(angle) >= ANGLE_KILL) {
pidOutput = 0; }
Drive_Motor(pidOutput);
currentRaw = analogRead(currentPin);
currentVolts = currentRaw *(5.0/1024.0);
currentAmps = currentVolts/volt_per_amp;
currentRaw2 = analogRead(currentPin2);
currentVolts2 = currentRaw2 *(5.0/1024.0);
currentAmps2 = currentVolts2/volt_per_amp;
Serial.print("angle: ");
Serial.print(angle);
Serial.print(", ");
Serial.print("pidOutput: ");
Serial.print(pidOutput);
Serial.print(", ");
Serial.print("currentAmps: ");
Serial.print(currentAmps);
Serial.print(", ");
Serial.print("currentAmps2: ");
Serial.print(currentAmps2);
Serial.println();
}
void Drive_Motor(int drive) {
if (drive < 0) { // drive motors forward
digitalWrite(BRAKE_A, LOW); //Disengage the Brake for Channel A
digitalWrite(DIR_A, HIGH);
digitalWrite(BRAKE_B, LOW); //Disengage the Brake for Channel A
digitalWrite(DIR_A, HIGH);
analogWrite(PWM_A, abs(drive));
analogWrite(PWM_B, abs(drive)); // motor are not built equal.
} else { // drive motors backward
digitalWrite(BRAKE_A, LOW); //Disengage the Brake for Channel A
digitalWrite(DIR_A, LOW);
digitalWrite(BRAKE_B, LOW); //Disengage the Brake for Channel A
digitalWrite(DIR_B, LOW);
analogWrite(PWM_A, abs(drive));
analogWrite(PWM_B, abs(drive));}
}
void getGyroValues(){
byte MSB, LSB;
MSB = readI2C(0x29);
LSB = readI2C(0x28);
Gyrox = ((MSB <<

| LSB);
MSB = readI2C(0x2B);
LSB = readI2C(0x2A);
Gyroy = ((MSB <<

| LSB);
MSB = readI2C(0x2D);
LSB = readI2C(0x2C);
Gyroz = ((MSB <<

| LSB);
}
int readI2C (byte regAddr) {
Wire.beginTransmission(L3G4200D_Address);
Wire.write(regAddr); // Register address to read
Wire.endTransmission(); // Terminate request
Wire.requestFrom(L3G4200D_Address, 1); // Read a byte
while(!Wire.available()) { }; // Wait for receipt
return(Wire.read()); // Get result
}
void writeI2C (byte regAddr, byte val) {
Wire.beginTransmission(L3G4200D_Address);
Wire.write(regAddr);
Wire.write(val);
Wire.endTransmission();
}