Advertisement
safwan092

Untitled

Oct 14th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <MPU6050.h>
  3.  
  4. // Define the I2C addresses of the MPU6050 sensors
  5. const uint8_t MPU1_ADDRESS = 0x68;
  6. const uint8_t MPU2_ADDRESS = 0x69;
  7.  
  8. MPU6050 mpu1(MPU1_ADDRESS);
  9. MPU6050 mpu2(MPU2_ADDRESS);
  10. double x_1, y_1, z_1;
  11. double x2, y2, z2;
  12. void setup() {
  13. Wire.begin();
  14.  
  15. // Initialize MPU6050 sensors with their respective addresses
  16. mpu1.initialize();
  17. mpu2.initialize();
  18.  
  19. // Set MPU6050 scale and sensitivity settings if needed
  20. // mpu1.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
  21. // mpu1.setFullScaleGyroRange(MPU6050_GYRO_FS_250);
  22. // mpu2.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
  23. // mpu2.setFullScaleGyroRange(MPU6050_GYRO_FS_250);
  24.  
  25. // Start serial communication
  26. Serial.begin(115200);
  27. }
  28.  
  29. void loop() {
  30. // Read accelerometer and gyroscope data from MPU1
  31. int16_t ax1, ay1, az1, gx1, gy1, gz1;
  32. mpu1.getMotion6(&ax1, &ay1, &az1, &gx1, &gy1, &gz1);
  33.  
  34. // Read accelerometer and gyroscope data from MPU2
  35. int16_t ax2, ay2, az2, gx2, gy2, gz2;
  36. mpu2.getMotion6(&ax2, &ay2, &az2, &gx2, &gy2, &gz2);
  37.  
  38. // Print the data
  39. Serial.print("MPU1: ");
  40. // Serial.print("Accel(XYZ): ");
  41. // Serial.print(ax1); Serial.print(", ");
  42. // Serial.print(ay1); Serial.print(", ");
  43. // Serial.print(az1); Serial.print(" | ");
  44. //Serial.print("Gyro(XYZ): ");
  45. x_1 = gx1 + 171;
  46. y_1 = gy1 - 2379;
  47. z_1 = gz1 + 132;
  48. // Serial.print(gx1); Serial.print(", ");
  49. // Serial.print(gy1); Serial.print(", ");
  50. // Serial.println(gz1);
  51. if (x_1 > -40 && x_1 < 40) {
  52. x_1 = 0;
  53. }
  54. if (y_1 > -40 && y_1 < 40) {
  55. y_1 = 0;
  56. }
  57. if (z_1 > -40 && z_1 < 40) {
  58. z_1 = 0;
  59. }
  60. Serial.print(x_1); Serial.print(", ");
  61. Serial.print(y_1); Serial.print(", ");
  62. Serial.println(z_1);
  63.  
  64. Serial.print("MPU2: ");
  65. // Serial.print("Accel(XYZ): ");
  66. // Serial.print(ax2); Serial.print(", ");
  67. // Serial.print(ay2); Serial.print(", ");
  68. // Serial.print(az2); Serial.print(" | ");
  69. x2 = gx2 + 390;
  70. y2 = gy2 + 80;
  71. z2 = gz2 + 60;
  72. // Serial.print(gx2); Serial.print(", ");
  73. // Serial.print(gy2); Serial.print(", ");
  74. // Serial.println(gz2);
  75. if (x2 > -40 && x2 < 40) {
  76. x2 = 0;
  77. }
  78. if (y2 > -40 && y2 < 40) {
  79. y2 = 0;
  80. }
  81. if (z2 > -40 && z2 < 40) {
  82. z2 = 0;
  83. }
  84. Serial.print(x2); Serial.print(", ");
  85. Serial.print(y2); Serial.print(", ");
  86. Serial.println(z2);
  87. Serial.println("------------------------------------------------------");
  88. delay(500); // Adjust the delay according to your requirements
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement