|  | 
| 5 | 5 | 
 | 
| 6 | 6 |   Written by Elias Santistevan @ SparkFun Electronics, October 2022 | 
| 7 | 7 | 
 | 
| 8 |  | -	Products: | 
|  | 8 | +  Products: | 
| 9 | 9 | 
 | 
| 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 | 
| 12 | 12 | 
 | 
| 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 | 
| 15 | 15 | 
 | 
| 16 | 16 | 
 | 
| 17 | 17 |   Repository: | 
| 18 | 18 | 
 | 
| 19 |  | -		https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library | 
|  | 19 | +    https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library | 
| 20 | 20 | 
 | 
| 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). | 
| 23 | 23 | */ | 
| 24 | 24 | 
 | 
| 25 | 25 | #include <Wire.h> | 
| 26 | 26 | #include <SPI.h> | 
| 27 |  | -#include "SparkFun_KX13X.h" | 
|  | 27 | +#include <SparkFun_KX13X.h> // Click here to get the library: http://librarymanager/All#SparkFun_KX13X | 
| 28 | 28 | 
 | 
| 29 |  | -SparkFun_KX132 kxAccel;  | 
|  | 29 | +SparkFun_KX132 kxAccel; | 
| 30 | 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.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 | 
| 74 | 74 | 
 | 
| 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 | 
| 78 | 78 | 
 | 
| 79 | 79 |   // 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(); | 
| 82 | 82 | 
 | 
| 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 | 
| 91 | 85 | 
 | 
|  | 86 | +  kxAccel.setRange(SFE_KX132_RANGE16G); // 16g Range | 
|  | 87 | +  // kxAccel.setRange(SFE_KX134_RANGE16G);        // 16g for the KX134 | 
| 92 | 88 | 
 | 
|  | 89 | +  // kxAccel.setOutputDataRate();   // Default is 400Hz | 
|  | 90 | +  kxAccel.enableAccel(); | 
| 93 | 91 | } | 
| 94 | 92 | 
 | 
| 95 |  | -void loop()  | 
|  | 93 | +void loop() | 
| 96 | 94 | { | 
| 97 | 95 | 
 | 
| 98 |  | -  if( digitalRead(dataReadyPin) == HIGH ) // Check for data ready pin | 
| 99 |  | -	{  | 
|  | 96 | +  if (digitalRead(dataReadyPin) == HIGH) // Check for data ready pin | 
|  | 97 | +  { | 
| 100 | 98 |     kxAccel.getAccelData(&myData); | 
| 101 | 99 |     Serial.print("X: "); | 
| 102 | 100 |     Serial.print(myData.xData, 4); | 
| 103 | 101 |     Serial.print(" Y: "); | 
| 104 | 102 |     Serial.print(myData.yData, 4); | 
| 105 | 103 |     Serial.print(" Z: "); | 
| 106 | 104 |     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(); | 
| 112 | 110 |   } | 
| 113 | 111 | 
 | 
| 114 | 112 |   delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz | 
|  | 
0 commit comments