Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. // Based on MPU-6050 Short Example Sketch
  2. // By Arduino User JohnChi
  3. // August 17, 2014
  4. // Public Domain
  5.  
  6. #include <Wire.h>
  7. #include <TimerOne.h>
  8. #include <SD.h>
  9. #include <SPI.h>
  10.  
  11. #define bufferLength 128
  12.  
  13. const int MPU=0x68; // I2C address of the MPU-6050
  14.  
  15. int second = floor(millis() / 1000);
  16. File myFile;
  17.  
  18. volatile byte buf1[bufferLength];
  19. volatile byte buf2[bufferLength];
  20. byte toSend[bufferLength];
  21.  
  22. volatile uint8_t counter1;
  23. volatile uint8_t counter2;
  24.  
  25. volatile bool secondBuffer = false;
  26.  
  27. void setup(){
  28. Wire.begin();
  29. Wire.beginTransmission(MPU);
  30. Wire.write(0x6B); // PWR_MGMT_1 register
  31. Wire.write(0); // set to zero (wakes up the MPU-6050)
  32. Wire.endTransmission(true);
  33. Serial.begin(116400);
  34. while (!Serial) {}
  35. pinMode(10, OUTPUT);
  36. if (!SD.begin(10)) {
  37. Serial.println("initialization failed!");
  38. return;
  39. }
  40. myFile = SD.open("test.txt", FILE_WRITE);
  41. Serial.println("All OK, wait 5s, then GO TIME");
  42. delay(5000);
  43. Serial.print("GO");
  44. Timer1.initialize(1000);
  45. Timer1.attachInterrupt(readData);
  46. }
  47.  
  48. bool dead = false;
  49. volatile uint8_t fucks = 0;
  50. void loop() {
  51. second = floor(millis() / 1000);
  52. if(dead) {
  53. return;
  54. }
  55. if(second > 60 || fucks > 5) {
  56. if(!dead) {
  57. Serial.println("END");
  58. Serial.println(fucks);
  59. myFile.close();
  60. Serial.println("Closed");
  61. dead = true;
  62. }
  63. return;
  64. }
  65.  
  66. if(counter1 >= bufferLength or counter2 >= bufferLength) {
  67. noInterrupts();
  68. if(counter1 >= bufferLength) {
  69. memcpy((char*)toSend, (char*)buf1, bufferLength);
  70. counter1 = 0;
  71. } else {
  72. memcpy((char*)toSend, (char*)buf2, bufferLength);
  73. counter2 = 0;
  74. }
  75. interrupts();
  76. myFile.write(toSend,bufferLength);
  77. }
  78. }
  79.  
  80. void readData(){
  81. interrupts();
  82. Wire.beginTransmission(MPU);
  83. Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  84. Wire.endTransmission(false);
  85. Wire.requestFrom(MPU,2,true);
  86. uint8_t acc_msb = Wire.read();
  87. uint8_t acc_lsb = Wire.read();
  88. noInterrupts();
  89. uint16_t time = micros() % 50000;
  90. uint8_t time_msb = time >> 8;
  91. uint8_t time_lsb = time;
  92.  
  93. if(secondBuffer == false) {
  94. if(counter1 == bufferLength) {
  95. fucks += 1;
  96. } else {
  97. buf1[counter1] = acc_msb;
  98. buf1[counter1 + 1] = acc_lsb;
  99. buf1[counter1 + 2] = time_msb;
  100. buf1[counter1 + 3] = time_lsb;
  101. counter1 += 4;
  102. if(counter1 == bufferLength) {
  103. secondBuffer = true;
  104. }
  105. }
  106. } else {
  107. if(counter2 == bufferLength) {
  108. fucks += 1;
  109. } else {
  110. buf2[counter2] = acc_msb;
  111. buf2[counter2 + 1] = acc_lsb;
  112. buf2[counter2 + 2] = time_msb;
  113. buf2[counter2 + 3] = time_lsb;
  114. counter2 += 4;
  115. if(counter2 == bufferLength) {
  116. secondBuffer = false;
  117. }
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement