Hi, everyone I found this code and altered it to read two MPU6050 sensors on serial monitor and it works almost flawlessly. However Iv been stumped for a while on how to get 2 servos to follow two different of the readings on one Arduino board. My goal I to have servo 1 following MPU1 and servo2 following MPU2.
The way I'm doing it is just not working. I am also getting degree readings from the sensors so I am just trying to convert those numbers over to servo degree angles.
It would be great if someone could help.
#include <Wire.h>
#include <MPU6050.h>
#include <Servo.h>
MPU6050 gyro1 (0x68);
MPU6050 gyro2 (0x69);
int16_t ax1, ay1, az1; //creates angle variables 1
int16_t gx1, gy1, gz1;
int16_t ax2, ay2, az2; //creates angle variables 2
int16_t gx2, gy2, gz2;
Servo sholvert;
Servo sholhora;
void setup() {
sholvert.attach (8);
sholhora.attach (9);
Serial.begin(9600);
Wire.begin(); //starts I2C communication
gyro1.initialize();
gyro2.initialize();
}
void loop() {
for (int j = 8; j <= 10; j++) {
for (int k = 8; k <= 10; k++) {
digitalWrite(k, HIGH); //sets all sensors to high address
}
digitalWrite(j, LOW); //sets one sensor to low address
gyro1.getMotion6(&ax1, &ay1, &az1, &gx1, &gy1, &gz1); //calls values from sensor
gyro2.getMotion6(&ax2, &ay2, &az2, &gx2, &gy2, &gz2); //calls values from sensor
ax1 = map(ax1, -17000, 17000, 0, 180); //maps x axis rotation values from 0 to 180
ay1 = map(ay1, -17000, 17000, 0, 180);
az1 = map(az1, -17000, 17000, 0, 180);
ax2 = map(ax2, -17000, 17000, 0, 180); //maps x axis rotation values from 0 to 180
ay2 = map(ay2, -17000, 17000, 0, 180);
az2 = map(az2, -17000, 17000, 0, 180);
Serial.println(" ");
Serial.print("gyro1 ");
Serial.print(ax1);
Serial.print(" ");
Serial.print(ay1);
Serial.print(" ");
Serial.print(az1);
Serial.print(" gyro2");
Serial.print(" ");
Serial.print(ax2);
Serial.print(" ");
Serial.print(ay2);
sholvert.write(ay2); //myservo.write(180-(90+ ypr[2] * 180/M_PI));
Serial.print(" ");
Serial.println(az2);
}
}