Advertisement
Guest User

saber

a guest
Jan 7th, 2016
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <DFPlayer_Mini_Mp3.h>
  3. #include <Wire.h>
  4. #include <I2Cdev.h>
  5. #include <MPU6050.h>
  6.  
  7. MPU6050 accelgyro;
  8. int16_t ax, ay, az; // define accel as ax,ay,az
  9. int16_t gx, gy, gz; // define gyro as gx,gy,gz
  10. int16_t px, py, pz; // previous accel settings
  11. long x, y, z;
  12. long accel; // calc accel
  13.  
  14. int inPin = 9; // the number of the input pin
  15. int outPin = 10; // the number of the output pin
  16. int ledPin = 11;
  17.  
  18. int reading; // the current reading from the input pin
  19. int previous = LOW; // the previous reading from the input pin
  20.  
  21. int brightness = 0; // how bright the LED is
  22. int fadeAmount = 5; // how many points to fade the LED by
  23.  
  24. int track = 20;
  25. int prev = 20;
  26.  
  27. boolean state = false; // the current state of the circuit
  28.  
  29. // the follow variables are long's because the time, measured in miliseconds,
  30. // will quickly become a bigger number than can be stored in an int.
  31. long time = 0; // the last time the output pin was toggled
  32. long debounce = 500; // the debounce time, increase if the output flickers
  33. long looptime = 0; // the last time the loop was played
  34. long gtime = 0;
  35.  
  36. SoftwareSerial mySerial(0, 1); // RX, TX
  37.  
  38. void setup()
  39. {
  40. Wire.begin(); // join I2C bus
  41. //Serial.begin(38400); // initialize serial communication
  42. //while (!Serial); // wait for Leonardo enumeration, others continue immediately
  43. Serial.println("INIT");
  44. accelgyro.initialize();
  45.  
  46. Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  47.  
  48. mySerial.begin(9600);
  49. mp3_set_serial(mySerial); //set Serial for DFPlayer-mini mp3 module
  50.  
  51. pinMode(inPin, INPUT);
  52. pinMode(outPin, OUTPUT);
  53. pinMode(ledPin, OUTPUT);
  54. }
  55.  
  56. void loop()
  57. {
  58. reading = digitalRead(inPin);
  59. accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // read measurements from device
  60.  
  61. if(millis() - gtime > 500) {
  62. x=px-ax; y=py-ay; z=pz-az;
  63. x=abs(x); y=abs(y); z=abs(z);
  64. accel = (x+y+z);
  65. px = ax; py = ay; pz = az;
  66.  
  67. if(state && accel > 15000) {
  68. mp3_play(random(31,34));
  69. //analogWrite(outPin, 255); delay(100);
  70. //analogWrite(outPin, 50); delay(100);
  71. //analogWrite(outPin, 255); delay(100);
  72. //analogWrite(outPin, 200); delay(100);
  73. looptime = millis()-2000;
  74. }
  75.  
  76. Serial.print(gx);Serial.print(" ");Serial.print(gy);Serial.print(" ");Serial.println(gz);
  77. Serial.println(accel);
  78.  
  79. gtime = millis();
  80. }
  81.  
  82. if (reading == HIGH && previous == LOW && millis() - time > debounce) {
  83. if (!state) {
  84. digitalWrite(ledPin, HIGH);
  85. looptime = millis();
  86. mp3_play(1);
  87. for(brightness = 0; brightness < 200; brightness++) {
  88. analogWrite(outPin, brightness);
  89. delay(16);
  90. }
  91. state = true;
  92. } else {
  93. mp3_play(2);
  94. for(brightness = 200; brightness > -1; brightness--) {
  95. analogWrite(outPin, brightness);
  96. delay(7);
  97. }
  98. digitalWrite(ledPin, LOW);
  99. state = false;
  100. }
  101. time = millis();
  102. }
  103.  
  104. // if (state && millis() - looptime > 4000) {
  105. // while (track == prev) {
  106. // track = random(21,24);
  107. // }
  108. // mp3_play(track);
  109. // prev = track;
  110. // looptime = millis();
  111. // }
  112.  
  113. previous = reading;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement