Skip to content

Commit e7de189

Browse files
committed
cleanup demo - formatting and logic
1 parent 1f56fec commit e7de189

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

examples/Example01_PresenceBasicReadings/Example01_PresenceBasicReadings.ino

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,45 @@ int32_t setupError = 0;
3939
int32_t presValError = 0;
4040
int32_t detectorError = 0;
4141

42+
// Presence range in mm used
43+
#define MY_XM125_RANGE_START 200
44+
#define MY_XM125_RANGE_END 1000
4245
void setup()
4346
{
4447
// Start serial
4548
Serial.begin(115200);
49+
50+
Serial.println("");
51+
Serial.println("-------------------------------------------------------");
4652
Serial.println("XM125 Example 1: Basic Presence Readings");
53+
Serial.println("-------------------------------------------------------");
4754
Serial.println("");
4855

4956
Wire.begin();
5057

5158
// If begin is successful (1), then start example
52-
int startErr = radarSensor.begin(i2cAddress, Wire);
53-
if (startErr == 1)
59+
bool success = radarSensor.begin(i2cAddress, Wire);
60+
if (success == false)
5461
{
55-
Serial.println("Begin");
56-
}
57-
else // Otherwise, infinite loop
58-
{
59-
Serial.print("Start Error Code: ");
60-
Serial.println(startErr);
6162
Serial.println("Device failed to setup - Freezing code.");
6263
while (1)
6364
; // Runs forever
6465
}
6566

6667
// Start the sensor with default register values
67-
int32_t setupError = radarSensor.presenceDetectorStart();
68+
int32_t setupError = radarSensor.presenceDetectorStart(MY_XM125_RANGE_START, MY_XM125_RANGE_END);
6869
if (setupError != 0)
6970
{
7071
Serial.print("Presence Detection Start Setup Error: ");
7172
Serial.println(setupError);
7273
}
73-
74-
// New line and delay for easier reading
74+
Serial.print("Presense Detection Started - range: ");
75+
Serial.print(MY_XM125_RANGE_START);
76+
Serial.print("mm to ");
77+
Serial.print(MY_XM125_RANGE_END);
78+
Serial.println("mm");
7579
Serial.println();
80+
7681
delay(500);
7782
}
7883

@@ -87,22 +92,25 @@ void loop()
8792
if (presValError == 0)
8893
{
8994
Serial.print("Presence Detected: ");
90-
Serial.print(distance);
91-
Serial.println("mm");
92-
// Serial.print(distance * .1);
93-
// Serial.println("cm");
94-
// Serial.print(distance * .001);
95-
// Serial.println("m");
96-
// Serial.print(distance * .001);
97-
// Serial.println("m");
98-
// Serial.print(distance * .03937008);
99-
// Serial.println("In");
95+
// if distance is > 0, presence is detected, else it is not
96+
if (distance > 0)
97+
{
98+
Serial.print("YES - Distance: ");
99+
Serial.print(distance);
100+
Serial.print("mm, ");
101+
Serial.print(distance * .1);
102+
Serial.print("cm, ");
103+
Serial.print(distance * .001);
104+
Serial.print("m, ");
105+
Serial.print(distance * .03937008);
106+
Serial.println("In");
107+
}
108+
else
109+
Serial.println("NO");
100110
}
101111
else
102-
{
103112
Serial.println("Error returning presence distance value");
104-
}
105113

106-
// Delay 0.5 seconds between readings
107-
delay(500);
114+
// Delay 2.5 seconds between readings
115+
delay(2500);
108116
}

0 commit comments

Comments
 (0)