|
1 | | -#include <Wire.h> // Used to establish serial communication on the I2C bus |
2 | 1 | #include "SparkFun_TMAG5273_Arduino_Library.h" // Used to send and recieve specific information from our sensor |
| 2 | +#include <Wire.h> // Used to establish serial communication on the I2C bus |
3 | 3 |
|
4 | 4 | TMAG5273 sensor; // Initialize hall-effect sensor |
5 | 5 |
|
6 | 6 | // I2C default address |
7 | 7 | uint8_t i2cAddress = TMAG5273_I2C_ADDRESS_INITIAL; |
8 | 8 |
|
9 | | -void setup() |
| 9 | +void setup() |
10 | 10 | { |
11 | | - Wire.begin(); |
12 | | - // Start serial communication at 115200 baud |
13 | | - Serial.begin(115200); |
14 | | - |
15 | | - // Begin example of the magnetic sensor code (and add whitespace for easy reading) |
16 | | - Serial.println("TMAG5273 Example 1: Basic Readings"); |
17 | | - Serial.println(""); |
18 | | - |
19 | | - // If begin is successful (0), then start example |
20 | | - if(sensor.begin(i2cAddress, Wire) == 1) |
21 | | - { |
22 | | - Serial.println("Begin"); |
23 | | - } |
24 | | - else // Otherwise, infinite loop |
25 | | - { |
26 | | - Serial.println("Device failed to setup - Freezing code."); |
27 | | - while(1); // Runs forever |
28 | | - } |
29 | | - |
| 11 | + Wire.begin(); |
| 12 | + // Start serial communication at 115200 baud |
| 13 | + Serial.begin(115200); |
| 14 | + |
| 15 | + // Begin example of the magnetic sensor code (and add whitespace for easy reading) |
| 16 | + Serial.println("TMAG5273 Example 1: Basic Readings"); |
| 17 | + Serial.println(""); |
| 18 | + |
| 19 | + // If begin is successful (0), then start example |
| 20 | + if (sensor.begin(i2cAddress, Wire) == 1) |
| 21 | + { |
| 22 | + Serial.println("Begin"); |
| 23 | + } |
| 24 | + else // Otherwise, infinite loop |
| 25 | + { |
| 26 | + Serial.println("Device failed to setup - Freezing code."); |
| 27 | + while (1) |
| 28 | + ; // Runs forever |
| 29 | + } |
30 | 30 | } |
31 | 31 |
|
32 | | - |
33 | | -void loop() |
| 32 | +void loop() |
34 | 33 | { |
35 | | - // Checks if mag channels are on - turns on in setup |
36 | | - if(sensor.getMagneticChannel() != 0) |
37 | | - { |
38 | | - sensor.setTemperatureEn(true); |
39 | | - |
40 | | - float magX = sensor.getXData(); |
41 | | - float magY = sensor.getYData(); |
42 | | - float magZ = sensor.getZData(); |
43 | | - float temp = sensor.getTemp(); |
44 | | - |
45 | | - Serial.print("("); |
46 | | - Serial.print(magX); |
47 | | - Serial.print(", "); |
48 | | - Serial.print(magY); |
49 | | - Serial.print(", "); |
50 | | - Serial.print(magZ); |
51 | | - Serial.println(") mT"); |
52 | | - Serial.print(temp); |
53 | | - Serial.println(" C"); |
54 | | - } |
55 | | - else |
56 | | - { |
57 | | - // If there is an issue, stop the magnetic readings and restart sensor/example |
58 | | - Serial.println("Mag Channels disabled, stopping.."); |
59 | | - while(1); |
60 | | - } |
61 | | - |
62 | | - delay(100); |
| 34 | + // Checks if mag channels are on - turns on in setup |
| 35 | + if (sensor.getMagneticChannel() != 0) |
| 36 | + { |
| 37 | + sensor.setTemperatureEn(true); |
| 38 | + |
| 39 | + float magX = sensor.getXData(); |
| 40 | + float magY = sensor.getYData(); |
| 41 | + float magZ = sensor.getZData(); |
| 42 | + float temp = sensor.getTemp(); |
| 43 | + |
| 44 | + Serial.print("Data - Magnetic: [ X: "); |
| 45 | + Serial.print(magX); |
| 46 | + Serial.print(", Y: "); |
| 47 | + Serial.print(magY); |
| 48 | + Serial.print(", Z: "); |
| 49 | + Serial.print(magZ); |
| 50 | + Serial.print(" ] mT, Temp: "); |
| 51 | + Serial.print(temp); |
| 52 | + Serial.println(" C"); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + // If there is an issue, stop the magnetic readings and restart sensor/example |
| 57 | + Serial.println("Mag Channels disabled, stopping.."); |
| 58 | + while (1) |
| 59 | + ; |
| 60 | + } |
| 61 | + |
| 62 | + delay(300); |
63 | 63 | } |
0 commit comments