THIS IS THE FULL CODE FOR THE STABILIZER. // Librerias I2C para controlar el mpu6050 // la libreria MPU6050.h necesita I2Cdev.h, I2Cdev.h necesita Wire.h #include "I2Cdev.h" #include "MPU6050.h" #include "Wire.h" #include Servo servoX; // create servo object to control a servo // twelve servo objects can be created on most boards Servo servoY; // create servo object to control a servo // twelve servo objects can be created on most boards Servo servoZ; // create servo object to control a servo // La dirección del MPU6050 puede ser 0x68 o 0x69, dependiendo // del estado de AD0. Si no se especifica, 0x68 estará implicito MPU6050 sensor; // Valores RAW (sin procesar) del acelerometro y giroscopio en los ejes x,y,z int ax, ay, az; int gx, gy, gz; float ang_x = 0; // variable to store the servo position float ang_y = 0; // variable to store the servo position long tiempo_prev; float dt; float ang_x_prev, ang_y_prev; const int buttonPin1 = 2; // the number of the pushbutton pin const int buttonPin2 = 3; // the number of the pushbutton pin int ang_1 = 5; int ang_2 = 175; int e = 90; // variables will change: int buttonState1 = 0; // variable for reading the pushbutton status int buttonState2 = 0; void setup() { servoZ.attach(11); // attaches the servo on pin 9 to the servo object // initialize the pushbutton pin as an input: pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin2, INPUT); Serial.begin(9600); //Iniciando puerto serial Wire.begin(); //Iniciando I2C sensor.initialize(); //Iniciando el sensor if (sensor.testConnection()) Serial.println("Sensor iniciado correctamente"); else Serial.println("Error al iniciar el sensor"); servoX.attach(9); // attaches the servo on pin 9 to the servo object servoY.attach(10); // attaches the servo on pin 9 to the servo object } void loop() { // Leer las aceleraciones y velocidades angulares sensor.getAcceleration(&ax, &ay, &az); sensor.getRotation(&gx, &gy, &gz); dt = (millis() - tiempo_prev) / 1000.0; tiempo_prev = millis(); //Calcular los ángulos con acelerometro float accel_ang_x = atan(ay / sqrt(pow(ax, 2) + pow(az, 2))) * (180.0 / 3.14); float accel_ang_y = atan(-ax / sqrt(pow(ay, 2) + pow(az, 2))) * (180.0 / 3.14); //Calcular angulo de rotación con giroscopio y filtro complemento ang_x = 0.98 * (ang_x_prev + (gx / 131) * dt) + 0.02 * accel_ang_x; ang_y = 0.98 * (ang_y_prev + (gy / 131) * dt) + 0.02 * accel_ang_y; //Mostrar los angulos separadas por un [tab] Serial.print("Rotacion en X: "); Serial.print(ang_x); Serial.print("Rotacion en Y: "); Serial.println(ang_y); servoX.write(ang_x); // tell servo to go to position in variable 'pos' servoY.write(ang_y); // tell servo to go to position in variable 'pos' delay(1); // waits 15ms for the servo to reach the position ang_x_prev = ang_x; ang_y_prev = ang_y; // read the state of the pushbutton value: buttonState1 = digitalRead(buttonPin1); buttonState2 = digitalRead(buttonPin2); // check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState1 == LOW && e >= ang_1) { e = e - 5; if (e >= ang_1) { servoZ.write(e); // tell servo to go to position in variable 'pos' delay(20); } // waits 20ms for the servo to reach the position } else if (buttonState2 == LOW && e <= ang_2) { e = e + 5; if (e <= ang_2) { servoZ.write(e); // tell servo to go to position in variable 'pos' delay(20); } // waits 20ms for the servo to reach the position } }