|
1 | 1 | /* |
2 | | - example3-Buffer |
| 2 | + example3-Buffer |
3 | 3 |
|
4 | | - This example shows both how to setup the buffer but also how to route the buffer's |
5 | | - interrupt to a physical interrupt pin. |
| 4 | + This example shows both how to setup the buffer but also how to route the buffer's |
| 5 | + interrupt to a physical interrupt pin. |
6 | 6 |
|
7 | 7 | Written by Elias Santistevan @ SparkFun Electronics, October 2022 |
8 | 8 |
|
9 | | - Products: |
| 9 | + Products: |
10 | 10 |
|
11 | | - SparkFun Triple Axis Accelerometer Breakout - KX132: |
12 | | - https://www.sparkfun.com/products/17871 |
| 11 | + SparkFun Triple Axis Accelerometer Breakout - KX132: |
| 12 | + https://www.sparkfun.com/products/17871 |
13 | 13 |
|
14 | | - SparkFun Triple Axis Accelerometer Breakout - KX134: |
15 | | - https://www.sparkfun.com/products/17589 |
| 14 | + SparkFun Triple Axis Accelerometer Breakout - KX134: |
| 15 | + https://www.sparkfun.com/products/17589 |
16 | 16 |
|
17 | 17 |
|
18 | 18 | Repository: |
19 | 19 |
|
20 | | - https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library |
| 20 | + https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library |
21 | 21 |
|
22 | | - SparkFun code, firmware, and software is released under the MIT |
23 | | - License (http://opensource.org/licenses/MIT). |
| 22 | + SparkFun code, firmware, and software is released under the MIT |
| 23 | + License (http://opensource.org/licenses/MIT). |
24 | 24 | */ |
25 | 25 |
|
26 | 26 | #include <Wire.h> |
27 | 27 | #include "SparkFun_KX13X.h" |
28 | 28 |
|
29 | | -SparkFun_KX132 kxAccel; |
30 | | -// SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above |
| 29 | +SparkFun_KX132 kxAccel; |
| 30 | +//SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above |
31 | 31 |
|
32 | 32 | outputData myData; // Struct for the accelerometer's data |
33 | 33 | byte dataReadyPin = 2; // Change to fit your project. |
34 | 34 |
|
35 | | -void setup() |
| 35 | +void setup() |
36 | 36 | { |
37 | | - |
38 | | - Wire.begin(); |
39 | 37 |
|
40 | | - Serial.begin(115200); |
| 38 | + Wire.begin(); |
| 39 | + |
| 40 | + Serial.begin(115200); |
41 | 41 | Serial.println("Welcome."); |
42 | 42 |
|
43 | | - // Wait for the Serial monitor to be opened. |
44 | | - while(!Serial) |
| 43 | + // Wait for the Serial monitor to be opened. |
| 44 | + while (!Serial) |
45 | 45 | delay(50); |
46 | 46 |
|
47 | | - |
48 | | - if( !kxAccel.begin() ) |
49 | | - { |
| 47 | + if (!kxAccel.begin()) |
| 48 | + { |
50 | 49 | Serial.println("Could not communicate with the the KX13X. Freezing."); |
51 | | - while(1); |
52 | | - } |
| 50 | + while (1) |
| 51 | + ; |
| 52 | + } |
53 | 53 |
|
54 | | - Serial.println("Ready."); |
| 54 | + Serial.println("Ready."); |
55 | 55 |
|
56 | | - // Reset the chip so that old settings don't apply to new setups. |
57 | | - if( kxAccel.softwareReset() ) |
58 | | - Serial.println("Reset."); |
| 56 | + // Reset the chip so that old settings don't apply to new setups. |
| 57 | + if (kxAccel.softwareReset()) |
| 58 | + Serial.println("Reset."); |
59 | 59 |
|
60 | | - //Give some time for the accelerometer to reset. |
61 | | - //It needs two, but give it five for good measure. |
62 | | - delay(5); |
| 60 | + // Give some time for the accelerometer to reset. |
| 61 | + // It needs two, but give it five for good measure. |
| 62 | + delay(5); |
63 | 63 |
|
64 | | - // Many settings for KX13X can only be |
65 | | - // applied when the accelerometer is powered down. |
66 | | - // However there are many that can be changed "on-the-fly" |
67 | | - // check datasheet for more info, or the comments in the |
68 | | - // "...regs.h" file which specify which can be changed when. |
69 | | - kxAccel.enableAccel(false); |
| 64 | + // Many settings for KX13X can only be |
| 65 | + // applied when the accelerometer is powered down. |
| 66 | + // However there are many that can be changed "on-the-fly" |
| 67 | + // check datasheet for more info, or the comments in the |
| 68 | + // "...regs.h" file which specify which can be changed when. |
| 69 | + kxAccel.enableAccel(false); |
70 | 70 |
|
71 | | - kxAccel.enableBufferInt(); // Enables the Buffer interrupt |
72 | | - kxAccel.enablePhysInterrupt(); // Enables interrupt pin 1 |
73 | | - kxAccel.routeHardwareInterrupt(0x40); // Routes the data ready bit to pin 1 |
| 71 | + kxAccel.enableBufferInt(); // Enables the Buffer interrupt |
| 72 | + kxAccel.enablePhysInterrupt(); // Enables interrupt pin 1 |
| 73 | + kxAccel.routeHardwareInterrupt(0x40); // Routes the data ready bit to pin 1 |
74 | 74 |
|
75 | | - kxAccel.enableSampleBuffer(); // Enable buffer. |
| 75 | + kxAccel.enableSampleBuffer(); // Enable buffer. |
76 | 76 | kxAccel.setBufferOperationMode(0x00); // Enable the buffer to be FIFO. |
77 | 77 |
|
78 | | - // Additional Buffer Settings |
79 | | - |
80 | | - //uint8_t numSamples = 140; |
81 | | - //kxAccel.setBufferThreshold(numSamples); // Set the number of sample that can be stored in the buffer |
| 78 | + // Additional Buffer Settings |
82 | 79 |
|
83 | | - //kxAccel.setBufferResolution(); // Change sample resolution to 16 bit, 8 bit by default. |
84 | | - // This will change how many samples can be held in buffer. |
| 80 | + // uint8_t numSamples = 140; |
| 81 | + // kxAccel.setBufferThreshold(numSamples); // Set the number of sample that can be stored in the buffer |
85 | 82 |
|
86 | | - //kxAccel.clearBuffer(); // Clear the buffer |
87 | | - //kxAccel.getSampleLevel(); // Get the number of samples in the buffer. This number |
88 | | - // Changes depending on the resolution, see datasheet for more info. |
| 83 | + kxAccel.setBufferResolution(); // Change sample resolution to 16 bit, 8 bit by default. |
| 84 | + // This will change how many samples can be held in buffer. |
89 | 85 |
|
90 | | - kxAccel.setRange(SFE_KX132_RANGE16G); // 16g Range |
91 | | - //kxAccel.setRange(SFE_KX134_RANGE16G); // 16g for the KX134 |
92 | | - |
93 | | - //kxAccel.setOutputDataRate(); // Default is 50Hz |
94 | | - kxAccel.enableAccel(); |
| 86 | + // kxAccel.clearBuffer(); // Clear the buffer |
| 87 | + // kxAccel.getSampleLevel(); // Get the number of samples in the buffer. This number |
| 88 | + // Changes depending on the resolution, see datasheet for more info. |
95 | 89 |
|
| 90 | + kxAccel.setRange(SFE_KX132_RANGE2G); // 2g Range |
| 91 | + // kxAccel.setRange(SFE_KX134_RANGE8G); // 8g for the KX134 |
96 | 92 |
|
| 93 | + // kxAccel.setOutputDataRate(); // Default is 50Hz |
| 94 | + kxAccel.enableAccel(); |
97 | 95 | } |
98 | 96 |
|
99 | | -void loop() |
| 97 | +void loop() |
100 | 98 | { |
101 | 99 |
|
102 | | - if( digitalRead(dataReadyPin) == HIGH ) // Check for data ready pin |
103 | | - { |
104 | | - kxAccel.getAccelData(&myData); |
| 100 | + // getAccelData will return false if there is no data in the buffer |
| 101 | + if (kxAccel.getAccelData(&myData) == true) |
| 102 | + { |
| 103 | + |
105 | 104 | Serial.print("X: "); |
106 | 105 | Serial.print(myData.xData, 4); |
107 | 106 | Serial.print(" Y: "); |
108 | 107 | Serial.print(myData.yData, 4); |
109 | 108 | Serial.print(" Z: "); |
110 | 109 | Serial.print(myData.zData, 4); |
111 | | - Serial.println(); |
112 | | - |
| 110 | + Serial.println(); |
113 | 111 | } |
114 | | - |
115 | | - delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz |
116 | 112 | } |
0 commit comments