FreeFallDetect.ino 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #define CLEAR_STEP true
  33. #define NOT_CLEAR_STEP false
  34. //Create a instance of class LSM6DS3
  35. LSM6DS3 lsm6ds3( I2C_MODE, 0x6A ); //I2C device address 0x6A
  36. uint16_t detectCount = 0;
  37. void setup() {
  38. Serial.begin(9600);
  39. if( lsm6ds3.begin() != 0 ){
  40. Serial.println("Device error");
  41. }
  42. else{
  43. Serial.println("Device OK!");
  44. }
  45. if(0 != config_free_fall_detect())
  46. Serial.println("Fail to configure!");
  47. else
  48. Serial.println("Success to Configure!");
  49. }
  50. void loop()
  51. {
  52. uint8_t readDataByte = 0;
  53. //Read the wake-up source register
  54. lsm6ds3.readRegister(&readDataByte, LSM6DS3_ACC_GYRO_WAKE_UP_SRC);
  55. //Mask off the FF_IA bit for free-fall detection
  56. readDataByte &= 0x20;
  57. if( readDataByte )
  58. {
  59. detectCount ++;
  60. Serial.print("Free fall detected! ");
  61. Serial.println(detectCount);
  62. }
  63. delay(10);
  64. }
  65. int config_free_fall_detect(void)
  66. {
  67. uint8_t error = 0;
  68. uint8_t dataToWrite = 0;
  69. dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_200Hz;
  70. dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_2g;
  71. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_416Hz;
  72. error += lsm6ds3.writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, dataToWrite);
  73. error += lsm6ds3.writeRegister( LSM6DS3_ACC_GYRO_WAKE_UP_DUR, 0x00 );
  74. error += lsm6ds3.writeRegister(LSM6DS3_ACC_GYRO_FREE_FALL, 0x33);
  75. error += lsm6ds3.writeRegister( LSM6DS3_ACC_GYRO_MD1_CFG, 0x10 );
  76. error += lsm6ds3.writeRegister( LSM6DS3_ACC_GYRO_MD2_CFG, 0x10 );
  77. error += lsm6ds3.writeRegister(LSM6DS3_ACC_GYRO_TAP_CFG1, 0x01);
  78. return error;
  79. }