Advertisement
Guest User

AceleraSP

a guest
Sep 12th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. // Watch video here: https://www.youtube.com/watch?v=o09-9n5WrxE
  4.  
  5. //Connection pins provided in the diagram at the beginning of video
  6.  
  7. // library provided and code based on: https://github.com/Seeed-Studio/IMU_10DOF
  8. #include "Wire.h"
  9. #include "I2Cdev.h"
  10. #include "MPU9250.h"
  11.  
  12. // specific I2C addresses may be passed as a parameter here
  13. // AD0 low = 0x68 (default for InvenSense evaluation board)
  14. // AD0 high = 0x69
  15. MPU9250 accelgyro;
  16. I2Cdev   I2C_M;
  17.  
  18. int16_t ax, ay, az;
  19. int16_t gx, gy, gz;
  20. int16_t mx, my, mz;
  21. float Axyz[3];
  22. int rightbutton = 8;
  23. int leftbutton = 4;
  24.  
  25. void setup()
  26. {
  27.   // join I2C bus (I2Cdev library doesn't do this automatically)
  28.   Wire.begin();
  29.   Serial.begin(9600);
  30.   Serial.println("Initializing I2C devices...");
  31.   accelgyro.initialize();
  32.   Serial.println("Testing device connections...");
  33.   Serial.println(accelgyro.testConnection() ? "MPU9250 connection successful" : "MPU9250 connection failed");
  34.   delay(1000);
  35.   pinMode(leftbutton, OUTPUT);
  36.   pinMode(rightbutton, OUTPUT);
  37. //  Mouse.begin();
  38. }
  39.  
  40. void loop()
  41. {
  42.   getAccel_Data();
  43.  
  44.   float pitchrad = atan(Axyz[0] / sqrt(Axyz[1] * Axyz[1] + Axyz[2] * Axyz[2])); // radians
  45.   float rollrad = atan(Axyz[1] / sqrt(Axyz[0] * Axyz[0] + Axyz[2] * Axyz[2])); // radians
  46.  
  47.   float rolldeg = 180 * (atan(Axyz[1] / sqrt(Axyz[0] * Axyz[0] + Axyz[2] * Axyz[2]))) / PI; // degrees
  48.   float pitchdeg = 180 * (atan(Axyz[0] / sqrt(Axyz[1] * Axyz[1] + Axyz[2] * Axyz[2]))) / PI; // degrees
  49.  
  50.   float Min = -15;//-30, -45, -15
  51.   float Max = 15;// 30, 45, 15
  52.   int mapX = map(pitchdeg, Min, Max, -6, 6);
  53.   int mapY = map(rolldeg, Min, Max, -6, 6);
  54. //e  Mouse.move(-mapX, mapY, 0);
  55.   Serial.print(pitchdeg);
  56.   Serial.print(",");
  57.   Serial.print(rolldeg);
  58.   Serial.print(" - ");
  59.   int leftstate = digitalRead(leftbutton);
  60.   int rightstate = digitalRead(rightbutton);
  61.   //Serial.print(leftstate); Serial.print(rightstate);
  62.   if (leftstate == HIGH) {
  63.     Serial.print(" Left Click! ");
  64.    // Mouse.press(MOUSE_LEFT);
  65.   }
  66.   if (leftstate == LOW) {
  67.    // Mouse.release(MOUSE_LEFT);
  68.   }
  69.   if (rightstate == HIGH) {
  70.     Serial.print(" Right Click! ");
  71.    
  72.     //Mouse.click(MOUSE_RIGHT);
  73.   }
  74.   Serial.println();
  75. }
  76.  
  77. void getAccel_Data(void)
  78. {
  79.   accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
  80.   Axyz[0] = ((double) ax / 256) - 1.6;
  81.   Axyz[1] = ((double) ay / 256) - 2.1;
  82.   Axyz[2] = (double) az / 256;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement