我正在将Wemos D1 Mini和ADXL345一起使用。 我使用的是用于Arduino的Sparkfun库,并且能够读取(通过I2C)各个方向的加速度并使用中断例程。但是,我无法使ADXL345上的中断引脚(INT1)工作。 它只输出“ 1” ...
我要实现的目标是,使中断(INT1)在检测到活动时输出“ 1”,在其余时间输出“ 0”。
我编写了一个简单的代码(基于Sparkfun的示例):
#include <SparkFun_ADXL345.h> // SparkFun ADXL345 Library
#include <Wire.h>
const int inputPin = D5; // Use D5 on Wemos D1 Mini as input pin
int inputPinState = 0;
/*********** COMMUNICATION SELECTION ***********/
/* Comment Out The One You Are Not Using */
//ADXL345 adxl = ADXL345(10); // USE FOR SPI COMMUNICATION, ADXL345(CS_PIN);
ADXL345 adxl = ADXL345(); // USE FOR I2C COMMUNICATION
/****************** INTERRUPT ******************/
/* Uncomment If Attaching Interrupt */
void setup(){
Serial.begin(9600); // Start the serial terminal
Serial.println("ADXL345 Interrupt Test");
Serial.println();
// initialize input pin:
pinMode(inputPin, INPUT);
setupADXL();
}
/****************** MAIN CODE ******************/
/* Accelerometer Readings and Interrupt */
void loop(){
inputPinState = digitalRead(inputPin);
Serial.println(inputPinState);
ADXL_ISR();
}
/******************** SETUP ********************/
/* Configure ADXL345 Settings */
void setupADXL(){
adxl.powerOn(); // Power on the ADXL345
adxl.setRangeSetting(2); // Give the range settings
// Accepted values are 2g, 4g, 8g or 16g
// Higher Values = Wider Measurement Range
// Lower Values = Greater Sensitivity
adxl.setSpiBit(0); // Configure the device to be in 4 wire SPI mode when set to '0' or 3 wire SPI mode when set to 1
// Default: Set to 1
// SPI pins on the ATMega328: 11, 12 and 13 as reference in SPI Library
adxl.setActivityXYZ(1, 0, 0); // Set to activate movement detection in the axes "adxl.setActivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
adxl.setActivityThreshold(15); // 62.5mg per increment // Set activity // Inactivity thresholds (0-255)
adxl.setInactivityXYZ(1, 0, 0); // Set to detect inactivity in all the axes "adxl.setInactivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
adxl.setInactivityThreshold(15); // 62.5mg per increment // Set inactivity // Inactivity thresholds (0-255)
adxl.setTimeInactivity(5); // How many seconds of no activity is inactive?
/* Not needed in this project
//adxl.setTapDetectionOnXYZ(0, 0, 0); // Detect taps in the directions turned ON "adxl.setTapDetectionOnX(X, Y, Z);" (1 == ON, 0 == OFF)
/* Not needed in this project
// Set values for what is considered a TAP and what is a DOUBLE TAP (0-255)
adxl.setTapThreshold(50); // 62.5 mg per increment
adxl.setTapDuration(15); // 625 μs per increment
adxl.setDoubleTapLatency(80); // 1.25 ms per increment
adxl.setDoubleTapWindow(200); // 1.25 ms per increment
*/
/* Not needed in this projec
// Set values for what is considered FREE FALL (0-255)
adxl.setFreeFallThreshold(7); // (5 - 9) recommended - 62.5mg per increment
adxl.setFreeFallDuration(30); // (20 - 70) recommended - 5ms per increment
*/
// Setting "activity" to take place on INT1 pin
adxl.setImportantInterruptMapping(2, 2, 2, 1, 2); // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);"
// Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts.
// This library may have a problem using INT2 pin. Default to INT1 pin.
// Turn on Interrupts for each mode (1 == ON, 0 == OFF)
adxl.InactivityINT(0);
adxl.ActivityINT(1);
adxl.FreeFallINT(0);
adxl.doubleTapINT(0);
adxl.singleTapINT(0);
//attachInterrupt(digitalPinToInterrupt(interruptPin), ADXL_ISR, RISING); // Attach Interrupt
}
void ADXL_ISR() {
byte interrupts = adxl.getInterruptSource();
// Activity
if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){
Serial.println("*** ACTIVITY ***");
//add code here to do when activity is sensed
}