Advertisement
safwan092

Untitled

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