Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. // MPU-6050 Short Example Sketch
  2. // By Arduino User JohnChi
  3. // August 17, 2014
  4. // Public Domain
  5. #include<Wire.h>
  6. const int MPU_addr=0x68 , arraySize = 100, threshold = 3900; // I2C address of the MPU-6050
  7. int16_t AcX,AcY,AcZ, count = 0;
  8. int averagex[arraySize] , averagey[arraySize] , averagez[arraySize];
  9. int sumx = 0, sumy = 0, sumz = 0;
  10. int passedThresholdx = 0, passedThresholdy = 0, passedThresholdz = 0, steps = 0;
  11. bool enterx = false, entery = false, enterz = false;
  12. void setup(){
  13. Wire.begin();
  14. Wire.beginTransmission(MPU_addr);
  15. Wire.write(0x6B); // PWR_MGMT_1 register
  16. Wire.write(0); // set to zero (wakes up the MPU-6050)
  17. Wire.endTransmission(true);
  18. Serial.begin(9600);
  19. Serial.print("fady");
  20. }
  21. void loop(){
  22. Wire.beginTransmission(MPU_addr);
  23. Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  24. Wire.endTransmission(false);
  25. Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
  26. AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  27. AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  28. AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  29. averagex[count % arraySize] = AcX;
  30. averagey[count % arraySize] = AcY;
  31. averagez[count % arraySize] = AcZ;
  32. count++;
  33.  
  34. if (AcX > threshold && passedThresholdx == 0 && !entery && !enterz) {
  35. enterx = true;
  36. passedThresholdy++;
  37. } else if (AcX < threshold && passedThresholdx == 1 && enterx) {
  38. Serial.print (++steps);
  39. Serial.print("\n");
  40. passedThresholdx = 0;
  41. } else
  42. if (AcY > threshold && passedThresholdy == 0 && !enterx && !enterz) {
  43. entery = true;
  44. passedThresholdy++;
  45. } else if (AcY < threshold && passedThresholdy == 1 && entery) {
  46. Serial.print (++steps);
  47. Serial.print("\n");
  48. passedThresholdy = 0;
  49. }
  50. else if (AcZ > threshold && passedThresholdz == 0 && !entery && !enterx) {
  51. enterz = true;
  52. passedThresholdy++;
  53. } else if (AcZ < threshold && passedThresholdz == 1 && enterz) {
  54. Serial.print (++steps);
  55. Serial.print("\n");
  56. passedThresholdz = 0;
  57. }
  58.  
  59. if(count >= arraySize){
  60. for (int i = 0; i < arraySize; i++) {
  61. sumx += averagex[i];
  62. sumy += averagey[i];
  63. sumz += averagez[i];
  64. }
  65. // Serial.print((sumx / arraySize));
  66. // Serial.print(" ");
  67. // Serial.print(sumy / arraySize);
  68. // Serial.print(" ");
  69. // Serial.print((sumz / arraySize));
  70. // Serial.print(" ");
  71. // Serial.print("\n");
  72. sumx = sumy = sumz = 0;
  73. }
  74.  
  75. delay(50);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement