FreeFallDetect.ino 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*****************************************************************************/
  2. // FreeFallDetect.ino
  3. // Hardware: Grove - 6-Axis Accelerometer&Gyroscope
  4. // Arduino IDE: Arduino-1.65
  5. // Author: Lambor
  6. // Date: Nov,2015
  7. // Version: v1.0
  8. //
  9. // Modified by:
  10. // Data:
  11. // Description:
  12. //
  13. // by www.seeedstudio.com
  14. //
  15. // This library is free software; you can redistribute it and/or
  16. // modify it under the terms of the GNU Lesser General Public
  17. // License as published by the Free Software Foundation; either
  18. // version 2.1 of the License, or (at your option) any later version.
  19. //
  20. // This library is distributed in the hope that it will be useful,
  21. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. // Lesser General Public License for more details.
  24. //
  25. // You should have received a copy of the GNU Lesser General Public
  26. // License along with this library; if not, write to the Free Software
  27. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. //
  29. /*******************************************************************************/
  30. #include "SparkFunLSM6DS3.h"
  31. #include "Wire.h"
  32. #include "SPI.h"
  33. #define CLEAR_STEP true
  34. #define NOT_CLEAR_STEP false
  35. //Create a instance of class LSM6DS3
  36. LSM6DS3 lsm6ds3( I2C_MODE, 0x6A ); //I2C device address 0x6A
  37. uint16_t detectCount = 0;
  38. void setup() {
  39. Serial.begin(9600);
  40. if( lsm6ds3.begin() != 0 ){
  41. Serial.println("Device error");
  42. }
  43. else{
  44. Serial.println("Device OK!");
  45. }
  46. if(0 != config_free_fall_detect())
  47. Serial.println("Fail to configure!");
  48. else
  49. Serial.println("Success to Configure!");
  50. }
  51. void loop()
  52. {
  53. uint8_t readDataByte = 0;
  54. //Read the wake-up source register
  55. lsm6ds3.readRegister(&readDataByte, LSM6DS3_ACC_GYRO_WAKE_UP_SRC);
  56. //Mask off the FF_IA bit for free-fall detection
  57. readDataByte &= 0x20;
  58. if( readDataByte )
  59. {
  60. detectCount ++;
  61. Serial.print("Free fall detected! ");
  62. Serial.println(detectCount);
  63. }
  64. delay(10);
  65. }
  66. int config_free_fall_detect(void)
  67. {
  68. uint8_t error = 0;
  69. uint8_t dataToWrite = 0;
  70. dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_200Hz;
  71. dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_2g;
  72. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_416Hz;
  73. error += lsm6ds3.writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, dataToWrite);
  74. error += lsm6ds3.writeRegister( LSM6DS3_ACC_GYRO_WAKE_UP_DUR, 0x00 );
  75. error += lsm6ds3.writeRegister(LSM6DS3_ACC_GYRO_FREE_FALL, 0x33);
  76. error += lsm6ds3.writeRegister( LSM6DS3_ACC_GYRO_MD1_CFG, 0x10 );
  77. error += lsm6ds3.writeRegister( LSM6DS3_ACC_GYRO_MD2_CFG, 0x10 );
  78. error += lsm6ds3.writeRegister(LSM6DS3_ACC_GYRO_TAP_CFG1, 0x01);
  79. return error;
  80. }