Skip to content

Commit a3a0a5f

Browse files
Magic numbers removed from library
Added more constants, cleaned up code, added new functions; need to cleanup and finish sensing
1 parent 5c6af60 commit a3a0a5f

File tree

6 files changed

+255
-64
lines changed

6 files changed

+255
-64
lines changed

examples/Example01_DistanceBasicReadings/Example01_DistanceBasicReadings.ino

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ SfeXM125 radarSensor;
2929
// I2C default address
3030
uint8_t i2cAddress = SFE_XM125_I2C_ADDRESS;
3131

32+
uint32_t distanceVal = 0;
33+
3234
void setup()
3335
{
3436
// Start serial
@@ -48,11 +50,47 @@ void setup()
4850
Serial.println("Device failed to setup - Freezing code.");
4951
while(1); // Runs forever
5052
}
53+
54+
// Default start = 1000; Default stop = 5000
55+
if(radarSensor.distanceBegin() != 0)
56+
{
57+
Serial.println("Sensor started successfully");
58+
}
59+
else
60+
{
61+
Serial.println("Sensor not initialized correctly - Freezing code.");
62+
while(1);
63+
}
64+
65+
66+
// Test code below - delete once complete
67+
uint32_t startVal = 0;
68+
uint32_t endVal = 0;
69+
radarSensor.getDistanceStart(startVal);
70+
radarSensor.getDistanceEnd(endVal);
71+
Serial.print("Start Val: ");
72+
Serial.println(startVal);
73+
Serial.print("End Val: ");
74+
Serial.println(endVal);
75+
76+
delay(1000);
77+
78+
5179
}
5280

5381
void loop()
5482
{
55-
// Request Distance Data from the device
83+
// Request Distance Data from the device
84+
radarSensor.setDistanceCommand(SFE_XM125_DISTANCE_START_DETECTOR);
85+
radarSensor.distanceBusyWait();
86+
radarSensor.getDistancePeak0Distance(distanceVal);
87+
88+
if(distanceVal != 0)
89+
{
90+
Serial.print("Peak Distance Found: ");
91+
Serial.print(distanceVal);
92+
Serial.println(" mm");
93+
}
5694

5795
delay(100);
5896
}

examples/Example04_PresenceBasicReadings/Example04_PresenceBasicReadings.ino

Lines changed: 125 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ SfeXM125 radarSensor;
3030
uint8_t i2cAddress = SFE_XM125_I2C_ADDRESS;
3131

3232
// Value to fill presence distance (in mm)
33-
uint32_t distance;
34-
uint32_t presenceDetected;
33+
uint32_t distance = 0;
34+
uint32_t presenceDetected = 0;
35+
uint32_t presenceDetectedSticky = 0;
36+
uint32_t errorStatus = 0;
37+
uint32_t regErr = 0;
38+
39+
// DELETE TEST VARIABLES ONCE COMPLETE
40+
uint32_t startVal = 0;
41+
uint32_t endVal = 0;
3542

3643
void setup()
3744
{
@@ -55,44 +62,130 @@ void setup()
5562

5663
delay(200);
5764

58-
// Default start = 1000; Default stop = 5000
59-
int32_t sensorStartError = radarSensor.presenceDetectorStart();
60-
if(sensorStartError != 0)
61-
{
62-
Serial.println("Sensor Started Successfully");
63-
}
64-
else
65+
66+
67+
// // Default start = 1000; Default stop = 5000
68+
// int32_t sensorStartError = radarSensor.presenceDetectorStart();
69+
// if(sensorStartError != 0)
70+
// {
71+
// Serial.println("Sensor Started Successfully");
72+
// }
73+
// else
74+
// {
75+
// Serial.println("Sensor not initialized correctly - Freezing code.");
76+
// while(1); // Runs forever
77+
// }
78+
79+
80+
// // Test code below - delete once complete
81+
82+
// radarSensor.getPresenceStart(startVal);
83+
// radarSensor.getPresenceEnd(endVal);
84+
// Serial.print("Start Val: ");
85+
// Serial.println(startVal);
86+
// Serial.print("End Val: ");
87+
// Serial.println(endVal);
88+
89+
delay(1000);
90+
//while(1);
91+
}
92+
93+
void loop()
94+
{
95+
// Set Start register
96+
if(radarSensor.setPresenceStart(1000) != 0)
6597
{
66-
Serial.println("Sensor not initialized correctly - Freezing code.");
67-
while(1); // Runs forever
98+
Serial.println("Presence Start Error");
6899
}
69-
70-
// Test code below - delete once complete
71-
uint32_t startVal = 0;
72-
uint32_t endVal = 0;
73100
radarSensor.getPresenceStart(startVal);
74-
radarSensor.getPresenceEnd(endVal);
75101
Serial.print("Start Val: ");
76102
Serial.println(startVal);
103+
104+
delay(100);
105+
// Set End register
106+
if(radarSensor.setPresenceEnd(5000) != 0)
107+
{
108+
Serial.println("Presence End Error");
109+
}
110+
radarSensor.getPresenceEnd(endVal);
77111
Serial.print("End Val: ");
78112
Serial.println(endVal);
113+
114+
delay(100);
115+
// Apply configuration
116+
if(radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_APPLY_CONFIGURATION) != 0)
117+
{
118+
Serial.println("Configuration application error");
119+
}
120+
121+
delay(100);
122+
// Poll detector status until busy bit is cleared - CHECK ON THIS!
123+
if(radarSensor.presenceBusyWait() != 0)
124+
{
125+
Serial.println("Busy wait error");
126+
}
127+
128+
delay(100);
129+
// Check error bits
130+
131+
132+
delay(100);
133+
// Start detector
134+
if(radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_START_DETECTOR) != 0)
135+
{
136+
Serial.println("Start detector error");
137+
}
138+
139+
delay(100);
140+
// Poll detector status until busy bit is cleared - CHECK ON THIS!
141+
if(radarSensor.presenceBusyWait() != 0)
142+
{
143+
Serial.println("Busy wait error");
144+
}
145+
146+
delay(100);
147+
// Verify that no error bits are set in the detector status register
148+
radarSensor.getPresenceDetectorError(errorStatus);
149+
if((errorStatus & SFE_XM125_PRESENCE_DETECTOR_STATUS_MASK) != 0)
150+
{
151+
Serial.println("Detector status error");
152+
}
79153

80-
delay(1000);
81-
while(1);
82-
}
83-
84-
void loop()
85-
{
86-
radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_START_DETECTOR);
87-
// If Presence is detected, then print out distance from device
154+
delay(100);
155+
// Read detector result register and determine
88156
radarSensor.getPresenceDetectorPresenceDetected(presenceDetected);
89-
Serial.print("Presence Detector flag: ");
90-
Serial.println(presenceDetected);
157+
radarSensor.getPresenceDetectorPresenceStickyDetected(presenceDetectedSticky);
158+
radarSensor.getPresenceDetectorRegError(regErr);
159+
160+
if((presenceDetected == 1) & (presenceDetectedSticky == 1))
161+
{
162+
radarSensor.getPresenceDistance(distance);
163+
Serial.print("Presence Detected: ");
164+
Serial.print(distance);
165+
Serial.println("mm");
166+
}
167+
168+
// Reset sensor configuration to reapply configuration registers
169+
//radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_RESET_MODULE);
170+
171+
Serial.println("loop completed");
172+
173+
delay(2000);
174+
175+
176+
// ***** PREVIOUS CODE BELOW *****
177+
178+
// radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_START_DETECTOR);
179+
// // If Presence is detected, then print out distance from device
180+
// radarSensor.presenceBusyWait();
181+
// radarSensor.getPresenceDetectorPresenceDetected(presenceDetected);
182+
// Serial.print("Presence Detector flag: ");
183+
// Serial.println(presenceDetected);
91184

92-
radarSensor.getPresenceDistance(distance);
93-
Serial.print("Presence detected at ");
94-
Serial.print(distance);
95-
Serial.println("mm");
185+
// radarSensor.getPresenceDistance(distance);
186+
// Serial.print("Presence detected at ");
187+
// Serial.print(distance);
188+
// Serial.println("mm");
96189

97190
// if(presenceDetected == 1)
98191
// {
@@ -102,5 +195,6 @@ void loop()
102195
// Serial.println("mm");
103196
// }
104197

105-
delay(500);
198+
// Read out at a slower rate for easier reading (0.5 second delay)
199+
// delay(500);
106200
}

src/SparkFun_Qwiic_XM125_Arduino_Library.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class SfeXM125 : public QwDevXM125
1212

1313
/// @brief Sets up Arduino I2C driver using the specified I2C address then calls the super class begin.
1414
/// @param address Address of the I2C device.
15+
/// @param wirePort Wire port of the I2C device.
16+
/// @param version Distance (0) or Presence (1) version
1517
/// @return True if successful, false otherwise.
1618
bool begin(uint8_t deviceAddress = SFE_XM125_I2C_ADDRESS, TwoWire &wirePort = Wire)
1719
{

0 commit comments

Comments
 (0)