Skip to content

Commit 32f3d01

Browse files
Examples are cleaned up and funcitonal
Code is ready for review - magic numbers removed, examples cleaned up, old code removed
1 parent 8ba1b5f commit 32f3d01

File tree

10 files changed

+152
-368
lines changed

10 files changed

+152
-368
lines changed

examples/Example01_PresenceBasicReadings/Example01_PresenceBasicReadings.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void setup()
7777

7878
void loop()
7979
{
80+
// Busy wait loop until data is ready
8081
radarSensor.presenceBusyWait();
8182

8283
// Get the presence distance value and print out if no errors

examples/Example02_PresenceGPIO0Usage/Example02_PresenceGPIO0Usage.ino

Lines changed: 13 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ uint32_t presenceDetected = 0;
3535
uint32_t presenceDetectedSticky = 0;
3636
uint32_t startVal = 0;
3737
uint32_t endVal = 0;
38-
uint32_t gpioUsage;
38+
uint32_t gpioUsage = 0;
3939

4040
// Error statuses
4141
uint32_t errorStatus = 0;
4242
uint32_t busyError = 0;
43+
uint32_t presValError = 0;
4344

4445
void setup()
4546
{
@@ -79,26 +80,6 @@ void setup()
7980
}
8081

8182
delay(100);
82-
83-
// Set Start register - NOTE this is a factor 1000 larger than the RSS value
84-
if(radarSensor.setPresenceStart(300) != 0)
85-
{
86-
Serial.println("Presence Start Error");
87-
}
88-
radarSensor.getPresenceStart(startVal);
89-
Serial.print("Start Val: ");
90-
Serial.println(startVal);
91-
delay(100);
92-
93-
// Set End register - NOTE this is a factor 1000 larger than the RSS value
94-
if(radarSensor.setPresenceEnd(7000) != 0)
95-
{
96-
Serial.println("Presence End Error");
97-
}
98-
radarSensor.getPresenceEnd(endVal);
99-
Serial.print("End Val: ");
100-
Serial.println(endVal);
101-
delay(100);
10283

10384
// Turn presence detection on GPIO0 on
10485
if(radarSensor.setPresenceDetectionOnGPIO(1) != 0)
@@ -138,73 +119,29 @@ void setup()
138119
}
139120

140121
Serial.println();
141-
142-
// Original code below:
143-
// // Default start = 1000; Default stop = 5000
144-
// int32_t sensorStartError = radarSensor.presenceDetectorStart();
145-
// if(sensorStartError != 0)
146-
// {
147-
// Serial.println("Sensor Started Successfully");
148-
// }
149-
// else
150-
// {
151-
// Serial.println("Sensor not initialized correctly - Freezing code.");
152-
// while(1); // Runs forever
153-
// }
154122

155123
delay(1000);
156124
}
157125

158126
void loop()
159127
{
128+
// Busy wait loop until data is ready
129+
radarSensor.presenceBusyWait();
160130

161-
// Poll detector status until busy bit is cleared - CHECK ON THIS!
162-
if(radarSensor.presenceBusyWait() != 0)
163-
{
164-
Serial.println("Busy wait error");
165-
}
166-
167-
// Check error bits
168-
radarSensor.getPresenceDetectorErrorStatus(errorStatus);
169-
if(errorStatus != 0)
170-
{
171-
Serial.print("Detector status error: ");
172-
Serial.println(errorStatus);
173-
}
174-
175-
176-
// Start detector
177-
if(radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_START_DETECTOR) != 0)
178-
{
179-
Serial.println("Start detector error");
180-
}
181-
182-
// Poll detector status until busy bit is cleared - CHECK ON THIS!
183-
if(radarSensor.presenceBusyWait() != 0)
184-
{
185-
Serial.println("Busy wait error");
186-
}
187-
188-
// Verify that no error bits are set in the detector status register
189-
radarSensor.getPresenceDetectorErrorStatus(errorStatus);
190-
if(errorStatus != 0)
191-
{
192-
Serial.print("Detector status error: ");
193-
Serial.println(errorStatus);
194-
}
131+
// Get the presence distance value and print out if no errors
132+
presValError = radarSensor.getPresenceDistanceValuemm(distance);
195133

196-
// Read detector result register and determine detection status
197-
radarSensor.getPresenceDetectorPresenceDetected(presenceDetected);
198-
radarSensor.getPresenceDetectorPresenceStickyDetected(presenceDetectedSticky);
199-
200-
if((presenceDetected == 1) | (presenceDetectedSticky == 1))
134+
if(presValError == 0)
201135
{
202-
radarSensor.getPresenceDistance(distance);
203136
Serial.print("Presence Detected: ");
204137
Serial.print(distance);
205138
Serial.println("mm");
206139
}
140+
else
141+
{
142+
Serial.println("Error returning presence distance value");
143+
}
207144

208-
// Delay 1 second between readings
209-
delay(1000);
145+
// Delay 0.5 seconds between readings
146+
delay(500);
210147
}

examples/Example03_PresenceSerialPlotter/Example03_PresenceSerialPlotter.ino

Lines changed: 16 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ uint32_t endVal = 0;
3939
// Error statuses
4040
uint32_t errorStatus = 0;
4141
uint32_t busyError = 0;
42+
uint32_t presValError = 0;
4243

4344
void setup()
4445
{
@@ -65,111 +66,33 @@ void setup()
6566

6667
delay(200);
6768

68-
// Presence Sensor Setup
69-
// Reset sensor configuration to reapply configuration registers
70-
radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_RESET_MODULE);
71-
72-
// Check error and busy bits
73-
radarSensor.getPresenceDetectorErrorStatus(errorStatus);
74-
if(errorStatus != 0)
75-
{
76-
Serial.print("Detector status error: ");
77-
Serial.println(errorStatus);
78-
}
79-
80-
delay(100);
81-
82-
// Set Start register
83-
if(radarSensor.setPresenceStart(300) != 0)
84-
{
85-
Serial.println("Presence Start Error");
86-
}
87-
radarSensor.getPresenceStart(startVal);
88-
89-
delay(100);
90-
// Set End register
91-
if(radarSensor.setPresenceEnd(7000) != 0)
92-
{
93-
Serial.println("Presence End Error");
94-
}
95-
radarSensor.getPresenceEnd(endVal);
96-
delay(100);
97-
98-
// Apply configuration
99-
if(radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_APPLY_CONFIGURATION) != 0)
100-
{
101-
// Check for errors
102-
radarSensor.getPresenceDetectorErrorStatus(errorStatus);
103-
if(errorStatus != 0)
104-
{
105-
Serial.print("Detector status error: ");
106-
Serial.println(errorStatus);
107-
}
108-
109-
Serial.println("Configuration application error");
110-
}
111-
112-
// Poll detector status until busy bit is cleared
113-
if(radarSensor.presenceBusyWait() != 0)
69+
// Start the sensor with default register values
70+
int32_t setupError = radarSensor.presenceDetectorStart();
71+
if(setupError != 0)
11472
{
115-
Serial.print("Busy wait error");
73+
Serial.print("Presence Detection Start Setup Error: ");
74+
Serial.println(setupError);
11675
}
11776

118-
// Check detector status
119-
radarSensor.getPresenceDetectorErrorStatus(errorStatus);
120-
if(errorStatus != 0)
121-
{
122-
Serial.print("Detector status error: ");
123-
Serial.println(errorStatus);
124-
}
125-
126-
// Extra line and 1 second delay for easier readings
77+
// New line and delay for easier reading
12778
Serial.println();
128-
delay(1000);
79+
delay(500);
12980
}
13081

13182
void loop()
13283
{
133-
134-
// Check error bits
135-
radarSensor.getPresenceDetectorErrorStatus(errorStatus);
136-
if(errorStatus != 0)
137-
{
138-
Serial.print("Detector status error: ");
139-
Serial.println(errorStatus);
140-
}
141-
142-
143-
// Start detector
144-
if(radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_START_DETECTOR) != 0)
145-
{
146-
Serial.println("Start detector error");
147-
}
148-
149-
// Poll detector status until busy bit is cleared - CHECK ON THIS!
150-
if(radarSensor.presenceBusyWait() != 0)
151-
{
152-
Serial.println("Busy wait error");
153-
}
154-
155-
// Verify that no error bits are set in the detector status register
156-
radarSensor.getPresenceDetectorErrorStatus(errorStatus);
157-
if(errorStatus != 0)
158-
{
159-
Serial.print("Detector status error: ");
160-
Serial.println(errorStatus);
161-
}
84+
// Busy wait loop until data is ready
85+
radarSensor.presenceBusyWait();
86+
87+
// Get the presence distance value and print out if no errors
88+
presValError = radarSensor.getPresenceDistanceValuemm(distance);
16289

163-
// Read detector result register and determine detection status
164-
radarSensor.getPresenceDetectorPresenceDetected(presenceDetected);
165-
radarSensor.getPresenceDetectorPresenceStickyDetected(presenceDetectedSticky);
166-
167-
if((presenceDetected == 1) | (presenceDetectedSticky == 1))
90+
if(presValError == 0)
16891
{
16992
radarSensor.getPresenceDistance(distance);
17093
Serial.println(distance);
17194
}
17295

173-
// Delay 1 second between readings
174-
delay(1000);
96+
// Delay 0.5 seconds between readings
97+
delay(500);
17598
}

examples/Example04_PresenceAdvancedReadings/Example04_PresenceAdvancedReadings.ino

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ uint32_t presenceDetected = 0;
3535
uint32_t presenceDetectedSticky = 0;
3636
uint32_t startVal = 0;
3737
uint32_t endVal = 0;
38-
uint32_t gpioUsage;
38+
uint32_t intraScore = 0;
39+
uint32_t interScore = 0;
3940

4041
// Error statuses
4142
uint32_t errorStatus = 0;
@@ -66,73 +67,16 @@ void setup()
6667

6768
delay(200);
6869

69-
// Setup: start the sensor - reset, apply configuration
70-
// Loop: anything there? get the distance
71-
72-
// Presence Sensor Setup
73-
// Reset sensor configuration to reapply configuration registers
74-
radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_RESET_MODULE);
75-
76-
// Check error and busy bits
77-
radarSensor.getPresenceDetectorErrorStatus(errorStatus);
78-
if(errorStatus != 0)
79-
{
80-
Serial.print("Detector status error: ");
81-
Serial.println(errorStatus);
82-
}
83-
84-
delay(100);
85-
86-
// Set Start register
87-
if(radarSensor.setPresenceStart(300) != 0)
88-
{
89-
Serial.println("Presence Start Error");
90-
}
91-
radarSensor.getPresenceStart(startVal);
92-
Serial.print("Start Val: ");
93-
Serial.println(startVal);
94-
95-
delay(100);
96-
// Set End register
97-
if(radarSensor.setPresenceEnd(7000) != 0)
98-
{
99-
Serial.println("Presence End Error");
100-
}
101-
radarSensor.getPresenceEnd(endVal);
102-
Serial.print("End Val: ");
103-
Serial.println(endVal);
104-
delay(100);
105-
106-
// Apply configuration
107-
if(radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_APPLY_CONFIGURATION) != 0)
70+
// Start the sensor with default register values
71+
int32_t setupError = radarSensor.presenceDetectorStart();
72+
if(setupError != 0)
10873
{
109-
// Check for errors
110-
radarSensor.getPresenceDetectorErrorStatus(errorStatus);
111-
if(errorStatus != 0)
112-
{
113-
Serial.print("Detector status error: ");
114-
Serial.println(errorStatus);
115-
}
116-
117-
Serial.println("Configuration application error");
118-
}
119-
120-
// Poll detector status until busy bit is cleared
121-
if(radarSensor.presenceBusyWait() != 0)
122-
{
123-
Serial.print("Busy wait error");
124-
}
125-
126-
// Check detector status
127-
radarSensor.getPresenceDetectorErrorStatus(errorStatus);
128-
if(errorStatus != 0)
129-
{
130-
Serial.print("Detector status error: ");
131-
Serial.println(errorStatus);
74+
Serial.print("Presence Detection Start Setup Error: ");
75+
Serial.println(setupError);
13276
}
13377

78+
// New line and delay for easier reading
13479
Serial.println();
135-
13680
delay(1000);
13781
}
13882

@@ -176,8 +120,16 @@ void loop()
176120
Serial.print("Presence Detected: ");
177121
Serial.print(distance);
178122
Serial.println("mm");
123+
124+
radarSensor.getPresenceIntraPresenceScore(intraScore);
125+
radarSensor.getPresenceInterPresenceScore(interScore);
126+
127+
Serial.print("Intra-Presence Score: ");
128+
Serial.println(intraScore);
129+
Serial.print("Inter-Presence Score: ");
130+
Serial.println(interScore);
179131
}
180132

181-
// Delay 1 second between readings
182-
delay(1000);
133+
// Delay 0.5 second between readings
134+
delay(500);
183135
}

0 commit comments

Comments
 (0)