-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExample04_PresenceAdvancedReadings.ino
136 lines (111 loc) · 3.62 KB
/
Example04_PresenceAdvancedReadings.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Example 4: Presence Advanced Readings
Using the Acconeer XM125 A121 60GHz Pulsed Coherent Radar Sensor.
This example shows how operate the XM125 when the device is in Presence Reading Mode.
The sensor is initialized, then the presence distance, intra-presence, and
inter-presence values will be printed to the terminal.
By: Madison Chodikov
SparkFun Electronics
Date: 2024/1/22
SparkFun code, firmware, and software is released under the MIT License.
Please see LICENSE.md for further details.
Hardware Connections:
QWIIC --> QWIIC
Serial.print it out at 115200 baud to serial monitor.
Feel like supporting our work? Buy a board from SparkFun!
https://www.sparkfun.com/products/ - Qwiic XM125 Breakout
*/
#include "SparkFun_Qwiic_XM125_Arduino_Library.h"
#include <Arduino.h>
SparkFunXM125Presence radarSensor;
// I2C default address
uint8_t i2cAddress = SFE_XM125_I2C_ADDRESS;
// Presence distance values
uint32_t distance = 0;
uint32_t presenceDetected = 0;
uint32_t presenceDetectedSticky = 0;
uint32_t startVal = 0;
uint32_t endVal = 0;
uint32_t intraScore = 0;
uint32_t interScore = 0;
// Error statuses
uint32_t errorStatus = 0;
uint32_t busyError = 0;
void setup()
{
// Start serial
Serial.begin(115200);
Serial.println("XM125 Example 4: Presence Advanced Readings");
Serial.println("");
Wire.begin();
// If begin is successful (1), then start example
int startErr = radarSensor.begin(i2cAddress, Wire);
if (startErr == 1)
{
Serial.println("Begin");
}
else // Otherwise, infinite loop
{
Serial.print("Start Error Code: ");
Serial.println(startErr);
Serial.println("Device failed to setup - Freezing code.");
while (1)
; // Runs forever
}
delay(200);
// Start the sensor with default register values
int32_t setupError = radarSensor.detectorStart();
if (setupError != 0)
{
Serial.print("Presence Detection Start Setup Error: ");
Serial.println(setupError);
}
// New line and delay for easier reading
Serial.println();
delay(1000);
}
void loop()
{
// Check error bits
radarSensor.getDetectorErrorStatus(errorStatus);
if (errorStatus != 0)
{
Serial.print("Detector status error: ");
Serial.println(errorStatus);
}
// Start detector
if (radarSensor.setCommand(SFE_XM125_PRESENCE_START_DETECTOR) != 0)
{
Serial.println("Start detector error");
}
// Poll detector status until busy bit is cleared - CHECK ON THIS!
if (radarSensor.busyWait() != 0)
{
Serial.println("Busy wait error");
}
// Verify that no error bits are set in the detector status register
radarSensor.getDetectorErrorStatus(errorStatus);
if (errorStatus != 0)
{
Serial.print("Detector status error: ");
Serial.println(errorStatus);
}
// Read detector result register and determine detection status
radarSensor.getDetectorPresenceDetected(presenceDetected);
radarSensor.getDetectorPresenceStickyDetected(presenceDetectedSticky);
if ((presenceDetected == 1) || (presenceDetectedSticky == 1))
{
radarSensor.getDistance(distance);
Serial.print("Presence Detected: ");
Serial.print(distance);
Serial.println("mm");
radarSensor.getIntraPresenceScore(intraScore);
radarSensor.getInterPresenceScore(interScore);
Serial.print("Intra-Presence Score: ");
Serial.println(intraScore);
Serial.print("Inter-Presence Score: ");
Serial.println(interScore);
}
// Delay 0.5 second between readings
delay(500);
}