Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <avr/sleep.h>
  3. #include <SparkFun_ADXL345.h> // SparkFun ADXL345 Library
  4. #include <pins_arduino.h>
  5.  
  6. #define LED_BUILTIN 13
  7. #define VBATPIN A9
  8. #define ADXL345_MG2G_MULTIPLIER (0.004) // 4mg per lsb
  9. #define SENSORS_GRAVITY_STANDARD (9.80665F)
  10. volatile bool is_asleep = false;
  11. volatile int isr_counter;
  12.  
  13. ADXL345 adxl = ADXL345(); // USE FOR I2C COMMUNICATION
  14.  
  15. /// INTERRUPT
  16. int interruptPin = 1; // Setup pin X to be the interrupt pin
  17.  
  18. float ab_accY;
  19.  
  20. // Offset due to gravity/sensor
  21. float gYoffset;
  22.  
  23. /* Declare functions */
  24. void sleepNow();
  25. void wake();
  26. int digPinToInterrupt();
  27.  
  28.  
  29. void setup()
  30. {
  31. pinMode(LED_BUILTIN, OUTPUT);
  32. digitalWrite(LED_BUILTIN, HIGH);
  33. delay(2000);
  34. Serial.begin(9600);
  35.  
  36. // ***************** SETUP ADXL 345 ****************************
  37. adxl.powerOn(); // Power on the ADXL345
  38.  
  39. adxl.setRangeSetting(16); // Give the range settings
  40. // Accepted values are 2g, 4g, 8g or 16g
  41. // Higher Values = Wider Measurement Range
  42. // Lower Values = Greater Sensitivity
  43.  
  44. adxl.setRate(100); // Sampling rate to 100 Hz
  45. adxl.setActivityXYZ(0, 1, 0); // Set to activate movement detection in the axes "adxl.setActivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
  46. adxl.setActivityThreshold(20); // 62.5mg per increment // Set activity // Inactivity thresholds (0-255)// (default 75)
  47. adxl.setInactivityXYZ(0, 0, 0); // Set to detect inactivity in all the axes "adxl.setInactivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
  48. adxl.setTapDetectionOnXYZ(0, 0, 0); // Detect taps in the directions turned ON "adxl.setTapDetectionOnX(X, Y, Z);" (1 == ON, 0 == OFF)
  49. adxl.setInterruptMapping(ADXL345_INT_ACTIVITY_BIT,ADXL345_INT2_PIN);
  50. adxl.InactivityINT(0);
  51. adxl.ActivityINT(1);
  52. adxl.FreeFallINT(0);
  53. adxl.doubleTapINT(0);
  54. adxl.singleTapINT(0);
  55.  
  56.  
  57. float offYsum = 0;
  58.  
  59. // 10 seconds worth of measurements (assuming 100 Hz)
  60. for (int j = 0; j <= 1000; j++)
  61. {
  62. // Accelerometer Readings
  63. int x,y,z;
  64. adxl.readAccel(&x, &y, &z); // Read the accelerometer values and store them in variables declared above x,y,z
  65. float accY = 0.3065 * y;
  66. offYsum += accY;
  67. }
  68.  
  69. gYoffset = offYsum/1000;
  70. pinMode(interruptPin,INPUT_PULLUP);//Set pin d2 to input using the builtin pullup resistor
  71. Serial.println("End of setup");
  72.  
  73. }
  74.  
  75. void loop()
  76. {
  77. int x,y,z;
  78. adxl.getInterruptSource();
  79. adxl.readAccel(&x, &y, &z); // Read the accelerometer values and store them in variables declared above x,y,z
  80.  
  81. float accY = 0.3065 * y;
  82. ab_accY = abs(abs(accY) - abs(gYoffset));
  83. float measThresh = 3;
  84.  
  85. if ( ab_accY <= measThresh)
  86. {
  87. sleepNow();
  88. }
  89. else
  90. {
  91. /* Continue */
  92. }
  93.  
  94. }
  95.  
  96. /* --------------------------HELPER FUNCTION----------------------------- */
  97.  
  98. int digPinToInterrupt(int digPin) {
  99. int intPin;
  100. if (digPin == 1){
  101. intPin = 3;
  102. // return intPin;
  103. }
  104. else if (digPin == 0)
  105. {
  106. intPin = 2;
  107. // return intPin;
  108. }
  109. else {
  110.  
  111. }
  112. return intPin;
  113. }
  114. // ------------------------ INTERRUPT ROUTINES -------------------- //
  115.  
  116.  
  117. void wakeUp(){
  118. if (is_asleep)
  119. {
  120. sleep_disable();
  121. detachInterrupt(digPinToInterrupt(interruptPin));
  122. digitalWrite(LED_BUILTIN, HIGH);
  123. isr_counter++;
  124. is_asleep = false;
  125. }
  126. }
  127.  
  128. void sleepNow()
  129. {
  130. delay(10);
  131. if (!is_asleep)
  132. {
  133. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  134. noInterrupts();
  135. sleep_enable();
  136. digitalWrite(LED_BUILTIN, LOW);
  137. is_asleep = true;
  138. attachInterrupt(digPinToInterrupt(interruptPin),wakeUp,HIGH);
  139. interrupts();
  140. sleep_cpu();
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement