|
| 1 | +#include "SparkFun_LIS331.h" |
| 2 | +#include <SPI.h> |
| 3 | + |
| 4 | +LIS331 xl; |
| 5 | + |
| 6 | +void setup() |
| 7 | +{ |
| 8 | + // put your setup code here, to run once: |
| 9 | + pinMode(9,INPUT); // Interrupt pin input |
| 10 | + pinMode(10, OUTPUT); // CS for SPI |
| 11 | + digitalWrite(10, HIGH); // Make CS high |
| 12 | + pinMode(11, OUTPUT); // MOSI for SPI |
| 13 | + pinMode(12, INPUT); // MISO for SPI |
| 14 | + pinMode(13, OUTPUT); // SCK for SPI |
| 15 | + SPI.begin(); |
| 16 | + xl.setSPICSPin(10); // This MUST be called BEFORE .begin() so |
| 17 | + // .begin() can communicate with the chip |
| 18 | + xl.begin(LIS331::USE_SPI); // Selects the bus to be used and sets |
| 19 | + // the power up bit on the accelerometer. |
| 20 | + // Also zeroes out all accelerometer |
| 21 | + // registers that are user writable. |
| 22 | + // This next section configures an interrupt. It will cause pin |
| 23 | + // INT1 on the accelerometer to go high when the absolute value |
| 24 | + // of the reading on the Z-axis exceeds a certain level for a |
| 25 | + // certain number of samples. |
| 26 | + xl.intSrcConfig(LIS331::INT_SRC, 1); // Select the source of the |
| 27 | + // signal which appears on pin INT1. In |
| 28 | + // this case, we want the corresponding |
| 29 | + // interrupt's status to appear. |
| 30 | + xl.setIntDuration(50, 1); // Number of samples a value must meet |
| 31 | + // the interrupt condition before an |
| 32 | + // interrupt signal is issued. At the |
| 33 | + // default rate of 50Hz, this is one sec. |
| 34 | + xl.setIntThreshold(2, 1); // Threshold for an interrupt. This is |
| 35 | + // not actual counts, but rather, actual |
| 36 | + // counts divided by 16. |
| 37 | + xl.enableInterrupt(LIS331::Z_AXIS, LIS331::TRIG_ON_HIGH, 1, true); |
| 38 | + // Enable the interrupt. Parameters indicate |
| 39 | + // which axis to sample, when to trigger |
| 40 | + // (in this case, when the absolute mag |
| 41 | + // of the signal exceeds the threshold), |
| 42 | + // which interrupt source we're configuring, |
| 43 | + // and whether to enable (true) or disable |
| 44 | + // (false) the interrupt. |
| 45 | + SerialUSB.begin(115200); |
| 46 | +} |
| 47 | + |
| 48 | +void loop() |
| 49 | +{ |
| 50 | + static long loopTimer = 0; |
| 51 | + int16_t x, y, z; |
| 52 | + if (millis() - loopTimer > 1000) |
| 53 | + { |
| 54 | + loopTimer = millis(); |
| 55 | + xl.readAxes(x, y, z); // The readAxes() function transfers the |
| 56 | + // current axis readings into the three |
| 57 | + // parameter variables passed to it. |
| 58 | + SerialUSB.println(x); |
| 59 | + SerialUSB.println(y); |
| 60 | + SerialUSB.println(z); |
| 61 | + SerialUSB.println(xl.convertToG(6,x)); // The convertToG() function |
| 62 | + SerialUSB.println(xl.convertToG(6,y)); // accepts as parameters the |
| 63 | + SerialUSB.println(xl.convertToG(6,z)); // raw value and the current |
| 64 | + SerialUSB.println(" "); // maximum g-rating. |
| 65 | + } |
| 66 | + if (digitalRead(9) == HIGH) |
| 67 | + { |
| 68 | + SerialUSB.println("Interrupt"); |
| 69 | + } |
| 70 | +} |
0 commit comments