Skip to content

Commit cd5db07

Browse files
committed
Update examples
1 parent 4737801 commit cd5db07

File tree

5 files changed

+250
-262
lines changed

5 files changed

+250
-262
lines changed

examples/example1_basic_readings/example1_basic_readings.ino

+65-69
Original file line numberDiff line numberDiff line change
@@ -2,105 +2,101 @@
22
example1-BasicReadings
33
44
This example shows the basic settings and functions for retrieving accelerometer
5-
data. Other possible Range settings, depending on your accelerometer KX132 or KX134, are
6-
the following:
7-
8-
SFE_KX132_RANGE2G
9-
SFE_KX132_RANGE4G
10-
SFE_KX132_RANGE8G
11-
SFE_KX132_RANGE16G
12-
13-
SFE_KX134_RANGE8G
14-
SFE_KX134_RANGE16G
15-
SFE_KX134_RANGE32G
16-
SFE_KX134_RANGE64G
5+
data. Other possible Range settings, depending on your accelerometer KX132 or KX134, are
6+
the following:
7+
8+
SFE_KX132_RANGE2G
9+
SFE_KX132_RANGE4G
10+
SFE_KX132_RANGE8G
11+
SFE_KX132_RANGE16G
12+
13+
SFE_KX134_RANGE8G
14+
SFE_KX134_RANGE16G
15+
SFE_KX134_RANGE32G
16+
SFE_KX134_RANGE64G
1717
1818
Written by Elias Santistevan @ SparkFun Electronics, October 2022
1919
20-
Products:
20+
Products:
2121
22-
SparkFun Triple Axis Accelerometer Breakout - KX132:
23-
https://www.sparkfun.com/products/17871
22+
SparkFun Triple Axis Accelerometer Breakout - KX132:
23+
https://www.sparkfun.com/products/17871
2424
25-
SparkFun Triple Axis Accelerometer Breakout - KX134:
26-
https://www.sparkfun.com/products/17589
25+
SparkFun Triple Axis Accelerometer Breakout - KX134:
26+
https://www.sparkfun.com/products/17589
2727
2828
Repository:
2929
30-
https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library
30+
https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library
3131
32-
SparkFun code, firmware, and software is released under the MIT
33-
License (http://opensource.org/licenses/MIT).
32+
SparkFun code, firmware, and software is released under the MIT
33+
License (http://opensource.org/licenses/MIT).
3434
*/
3535

3636
#include <Wire.h>
37-
#include "SparkFun_KX13X.h"
37+
#include <SparkFun_KX13X.h> // Click here to get the library: http://librarymanager/All#SparkFun_KX13X
3838

39-
SparkFun_KX132 kxAccel;
40-
//SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
39+
SparkFun_KX132 kxAccel;
40+
// SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
4141

42-
outputData myData; // Struct for the accelerometer's data
42+
outputData myData; // Struct for the accelerometer's data
4343

44-
void setup()
44+
void setup()
4545
{
46-
47-
Wire.begin();
4846

49-
Serial.begin(115200);
47+
Wire.begin();
48+
49+
Serial.begin(115200);
5050
Serial.println("Welcome.");
5151

52-
// Wait for the Serial monitor to be opened.
53-
while(!Serial)
52+
// Wait for the Serial monitor to be opened.
53+
while (!Serial)
5454
delay(50);
5555

56-
57-
if( !kxAccel.begin() )
58-
{
56+
if (!kxAccel.begin())
57+
{
5958
Serial.println("Could not communicate with the the KX13X. Freezing.");
60-
while(1);
61-
}
59+
while (1)
60+
;
61+
}
6262

63-
Serial.println("Ready.");
63+
Serial.println("Ready.");
6464

65-
if( kxAccel.softwareReset() )
66-
Serial.println("Reset.");
65+
if (kxAccel.softwareReset())
66+
Serial.println("Reset.");
6767

68-
//Give some time for the accelerometer to reset.
69-
//It needs two, but give it five for good measure.
70-
delay(5);
71-
72-
// Many settings for KX13X can only be
73-
// applied when the accelerometer is powered down.
74-
// However there are many that can be changed "on-the-fly"
75-
// check datasheet for more info, or the comments in the
76-
// "...regs.h" file which specify which can be changed when.
77-
kxAccel.enableAccel(false);
78-
79-
kxAccel.setRange(SFE_KX132_RANGE16G); // 16g Range
80-
//kxAccel.setRange(SFE_KX134_RANGE16G); // 16g for the KX134
81-
82-
kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready.
83-
// kxAccel.setOutputDataRate(); // Default is 50Hz
84-
kxAccel.enableAccel();
68+
// Give some time for the accelerometer to reset.
69+
// It needs two, but give it five for good measure.
70+
delay(5);
8571

72+
// Many settings for KX13X can only be
73+
// applied when the accelerometer is powered down.
74+
// However there are many that can be changed "on-the-fly"
75+
// check datasheet for more info, or the comments in the
76+
// "...regs.h" file which specify which can be changed when.
77+
kxAccel.enableAccel(false);
8678

79+
kxAccel.setRange(SFE_KX132_RANGE16G); // 16g Range
80+
// kxAccel.setRange(SFE_KX134_RANGE16G); // 16g for the KX134
8781

82+
kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready.
83+
// kxAccel.setOutputDataRate(); // Default is 50Hz
84+
kxAccel.enableAccel();
8885
}
8986

90-
void loop()
87+
void loop()
9188
{
92-
// Check if data is ready.
93-
if( kxAccel.dataReady() )
94-
{
95-
kxAccel.getAccelData(&myData);
96-
Serial.print("X: ");
97-
Serial.print(myData.xData, 4);
98-
Serial.print(" Y: ");
99-
Serial.print(myData.yData, 4);
100-
Serial.print(" Z: ");
101-
Serial.print(myData.zData, 4);
102-
Serial.println();
103-
}
89+
// Check if data is ready.
90+
if (kxAccel.dataReady())
91+
{
92+
kxAccel.getAccelData(&myData);
93+
Serial.print("X: ");
94+
Serial.print(myData.xData, 4);
95+
Serial.print(" Y: ");
96+
Serial.print(myData.yData, 4);
97+
Serial.print(" Z: ");
98+
Serial.print(myData.zData, 4);
99+
Serial.println();
100+
}
104101
delay(20); // Delay should be 1/ODR (Output Data Rate), default is 1/50ODR
105-
106102
}

examples/example2_interrupts/example2_interrupts.ino

+56-58
Original file line numberDiff line numberDiff line change
@@ -5,110 +5,108 @@
55
66
Written by Elias Santistevan @ SparkFun Electronics, October 2022
77
8-
Products:
8+
Products:
99
10-
SparkFun Triple Axis Accelerometer Breakout - KX132:
11-
https://www.sparkfun.com/products/17871
10+
SparkFun Triple Axis Accelerometer Breakout - KX132:
11+
https://www.sparkfun.com/products/17871
1212
13-
SparkFun Triple Axis Accelerometer Breakout - KX134:
14-
https://www.sparkfun.com/products/17589
13+
SparkFun Triple Axis Accelerometer Breakout - KX134:
14+
https://www.sparkfun.com/products/17589
1515
1616
1717
Repository:
1818
19-
https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library
19+
https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library
2020
21-
SparkFun code, firmware, and software is released under the MIT
22-
License (http://opensource.org/licenses/MIT).
21+
SparkFun code, firmware, and software is released under the MIT
22+
License (http://opensource.org/licenses/MIT).
2323
*/
2424

2525
#include <Wire.h>
2626
#include <SPI.h>
27-
#include "SparkFun_KX13X.h"
27+
#include <SparkFun_KX13X.h> // Click here to get the library: http://librarymanager/All#SparkFun_KX13X
2828

29-
SparkFun_KX132 kxAccel;
29+
SparkFun_KX132 kxAccel;
3030
// SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
3131

3232
outputData myData; // Struct for the accelerometer's data
3333
byte dataReadyPin = 2; // Change to fit your project.
3434

35-
void setup()
35+
void setup()
3636
{
37-
38-
Wire.begin();
3937

40-
Serial.begin(115200);
38+
Wire.begin();
39+
40+
Serial.begin(115200);
4141
Serial.println("Welcome.");
4242

43-
// Wait for the Serial monitor to be opened.
44-
while(!Serial)
43+
// Wait for the Serial monitor to be opened.
44+
while (!Serial)
4545
delay(50);
4646

47-
48-
if( !kxAccel.begin() )
49-
{
47+
if (!kxAccel.begin())
48+
{
5049
Serial.println("Could not communicate with the the KX13X. Freezing.");
51-
while(1);
52-
}
50+
while (1)
51+
;
52+
}
5353

54-
Serial.println("Ready.");
54+
Serial.println("Ready.");
5555

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.");
5959

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);
6363

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);
7070

71-
kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready.
72-
kxAccel.enablePhysInterrupt(); // Enables interrupt pin 1
73-
kxAccel.routeHardwareInterrupt(0x10); // Routes the data ready bit to pin 1
71+
kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready.
72+
kxAccel.enablePhysInterrupt(); // Enables interrupt pin 1
73+
kxAccel.routeHardwareInterrupt(0x10); // Routes the data ready bit to pin 1
7474

75-
// Routing Data Ready pin to interrupt pin 2.
76-
//kxAccel.enablePhysInterrupt(true, 2); // Enables interrupt pin 2
77-
//kxAccel.routeHardwareInterrupt(0x10, 2); // Routes the data ready bit to pin 2
75+
// Routing Data Ready pin to interrupt pin 2.
76+
// kxAccel.enablePhysInterrupt(true, 2); // Enables interrupt pin 2
77+
// kxAccel.routeHardwareInterrupt(0x10, 2); // Routes the data ready bit to pin 2
7878

7979
// This will change the interrupt behavior to latch instead of pulse
80-
// In this case you'll need to release directly with clearInterrupt();.
81-
//kxAccel.setLatchControl();
80+
// In this case you'll need to release directly with clearInterrupt();.
81+
// kxAccel.setLatchControl();
8282

83-
//kxAccel.setPinMode(); // Change interrupt to active HIGH
84-
//kxAccel.setPulseWidth(); // Change the length of a pulsed (non latched) interrupt
85-
86-
kxAccel.setRange(SFE_KX132_RANGE16G); // 16g Range
87-
//kxAccel.setRange(SFE_KX134_RANGE16G); // 16g for the KX134
88-
89-
//kxAccel.setOutputDataRate(); // Default is 400Hz
90-
kxAccel.enableAccel();
83+
// kxAccel.setPinMode(); // Change interrupt to active HIGH
84+
// kxAccel.setPulseWidth(); // Change the length of a pulsed (non latched) interrupt
9185

86+
kxAccel.setRange(SFE_KX132_RANGE16G); // 16g Range
87+
// kxAccel.setRange(SFE_KX134_RANGE16G); // 16g for the KX134
9288

89+
// kxAccel.setOutputDataRate(); // Default is 400Hz
90+
kxAccel.enableAccel();
9391
}
9492

95-
void loop()
93+
void loop()
9694
{
9795

98-
if( digitalRead(dataReadyPin) == HIGH ) // Check for data ready pin
99-
{
96+
if (digitalRead(dataReadyPin) == HIGH) // Check for data ready pin
97+
{
10098
kxAccel.getAccelData(&myData);
10199
Serial.print("X: ");
102100
Serial.print(myData.xData, 4);
103101
Serial.print(" Y: ");
104102
Serial.print(myData.yData, 4);
105103
Serial.print(" Z: ");
106104
Serial.print(myData.zData, 4);
107-
Serial.println();
108-
109-
// If interrupt behavior has been changed to latch, use the
110-
// following function to clear it after reading data.
111-
//kxAccel.clearInterrupt();
105+
Serial.println();
106+
107+
// If interrupt behavior has been changed to latch, use the
108+
// following function to clear it after reading data.
109+
// kxAccel.clearInterrupt();
112110
}
113111

114112
delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz

examples/example3_buffer/example3_buffer.ino

+18-18
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
*/
2525

2626
#include <Wire.h>
27-
#include "SparkFun_KX13X.h"
27+
#include <SparkFun_KX13X.h> // Click here to get the library: http://librarymanager/All#SparkFun_KX13X
2828

2929
SparkFun_KX132 kxAccel;
30-
//SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
30+
// SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
3131

3232
outputData myData; // Struct for the accelerometer's data
3333
byte dataReadyPin = 2; // Change to fit your project.
@@ -102,21 +102,21 @@ void loop()
102102
// But we can also use getSampleLevel. getSampleLevel will return how much data is in the buffer.
103103
if (kxAccel.getSampleLevel() > 0)
104104
{
105-
/*
106-
// getAccelData is slow as it manually checks if the buffer is being used
107-
// and if the data resolution is 16-bit or 8-bit.
108-
if (kxAccel.getAccelData(&myData) == true)
109-
{
110-
Serial.println();
111-
Serial.print("X: ");
112-
Serial.print(myData.xData, 4);
113-
Serial.print(" Y: ");
114-
Serial.print(myData.yData, 4);
115-
Serial.print(" Z: ");
116-
Serial.print(myData.zData, 4);
117-
Serial.println();
118-
}
119-
*/
105+
/*
106+
// getAccelData is slow as it manually checks if the buffer is being used
107+
// and if the data resolution is 16-bit or 8-bit.
108+
if (kxAccel.getAccelData(&myData) == true)
109+
{
110+
Serial.println();
111+
Serial.print("X: ");
112+
Serial.print(myData.xData, 4);
113+
Serial.print(" Y: ");
114+
Serial.print(myData.yData, 4);
115+
Serial.print(" Z: ");
116+
Serial.print(myData.zData, 4);
117+
Serial.println();
118+
}
119+
*/
120120

121121
// We can read the data more quickly by calling getRawAccelBufferData because we know
122122
// the buffer is being used and what the data resolution is.
@@ -139,6 +139,6 @@ void loop()
139139
else
140140
{
141141
Serial.print("."); // If the data rate is 50Hz (default), we'll expect to see ~20 dots between samples
142-
delay(1); // Wait 1ms
142+
delay(1); // Wait 1ms
143143
}
144144
}

0 commit comments

Comments
 (0)