-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_TLV_reading_ack.ino
67 lines (58 loc) · 1.66 KB
/
arduino_TLV_reading_ack.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <Tlv493d.h>
#include <Wire.h>
// Tlv493d Opject
Tlv493d Tlv493dMagnetic3DSensor = Tlv493d();
void setup() {
Wire.begin();
Serial.begin(9600);
while (!Serial)
;
Tlv493dMagnetic3DSensor.begin();
Tlv493dMagnetic3DSensor.setAccessMode(Tlv493dMagnetic3DSensor.MASTERCONTROLLEDMODE);
Tlv493dMagnetic3DSensor.disableTemp();
Wire.beginTransmission(0x70);
Wire.write(0x00);
Wire.endTransmission();
Wire.beginTransmission(0x71);
Wire.write(0x00);
Wire.endTransmission();
Wire.beginTransmission(0x72);
Wire.write(0x00);
Wire.endTransmission();
Wire.beginTransmission(0x73);
Wire.write(0x00);
Wire.endTransmission();
}
void loop() {
delay(Tlv493dMagnetic3DSensor.getMeasurementDelay());
Wire.beginTransmission(0x72);
Wire.write(2);
Wire.endTransmission();
float bx = Tlv493dMagnetic3DSensor.getX();
float by = Tlv493dMagnetic3DSensor.getY();
float bz = Tlv493dMagnetic3DSensor.getZ();
// Check for reading flag from python
if (Serial.available() > 0) {
char command = Serial.read();
if (command == 'R') {
float sumBX = 0.0;
float sumBY = 0.0;
float sumBZ = 0.0;
for (int i = 0; i < 64; i++) {
delay(Tlv493dMagnetic3DSensor.getMeasurementDelay());
Tlv493dMagnetic3DSensor.updateData();
sumBX += Tlv493dMagnetic3DSensor.getX();
sumBY += Tlv493dMagnetic3DSensor.getY();
sumBZ += Tlv493dMagnetic3DSensor.getZ();
}
float avgBX = sumBX / 64.0;
float avgBY = sumBY / 64.0;
float avgBZ = sumBZ / 64.0;
Serial.print(avgBX);
Serial.print(",");
Serial.print(avgBY);
Serial.print(",");
Serial.println(avgBZ);
}
}
}