Skip to content

Commit 47f5765

Browse files
Functional presence begin example
The issues with delays have been sorted and is now functional printing out data to the terminal in arduino.
1 parent 030922f commit 47f5765

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

examples/Example01_PresenceBasicReadings/Example01_PresenceBasicReadings.ino

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ void setup()
6161
while(1); // Runs forever
6262
}
6363

64-
delay(200);
65-
6664
// Start the sensor with default register values
6765
int32_t setupError = radarSensor.presenceDetectorStart();
6866
if(setupError != 0)
@@ -73,7 +71,7 @@ void setup()
7371

7472
// New line and delay for easier reading
7573
Serial.println();
76-
delay(1000);
74+
delay(500);
7775
}
7876

7977
void loop()
@@ -82,17 +80,17 @@ void loop()
8280

8381
// Get the presence distance value and print out if no errors
8482
presValError = radarSensor.getPresenceDistanceValuemm(distance);
85-
if(presValError != 0)
83+
if(presValError == 0)
8684
{
8785
Serial.print("Presence Detected: ");
8886
Serial.print(distance);
8987
Serial.println("mm");
9088
}
9189
else
9290
{
93-
Serial.println("Error returning presence distance value");
91+
Serial.print("Error returning presence distance value");
9492
}
9593

96-
// Delay 1 second between readings
97-
delay(1000);
94+
// Delay 0.5 seconds between readings
95+
delay(50);
9896
}

src/sfeQwiicXM125.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,20 @@ int32_t QwDevXM125::distanceBegin()
6868
{
6969
return 2;
7070
}
71-
delay(100);
7271

7372
// Set Start register
7473
if(setDistanceStart(300) != 0)
7574
{
7675
return 3;
7776
}
78-
delay(100);
77+
delay(100); // give time for command to set
7978

8079
// Set End register
8180
if(setDistanceEnd(2500) != 0)
8281
{
8382
return 4;
8483
}
85-
delay(100);
84+
delay(100); // give time for command to set
8685

8786
// Apply configuration
8887
if(setDistanceCommand(SFE_XM125_DISTANCE_APPLY_CONFIGURATION) != 0)
@@ -132,6 +131,7 @@ int32_t QwDevXM125::distanceDetectorReadingSetup()
132131
{
133132
return 2;
134133
}
134+
delay(100); // give time for command to set
135135

136136
// Poll detector status until busy bit is cleared - CHECK ON THIS!
137137
if(distanceBusyWait() != 0)
@@ -769,37 +769,38 @@ int32_t QwDevXM125::distanceBusyWait()
769769
int32_t QwDevXM125::presenceDetectorStart()
770770
{
771771
// Presence Sensor Setup
772-
uint32_t errorStatus;
772+
uint32_t errorStatus = 0;
773773

774774
// Reset sensor configuration to reapply configuration registers
775775
if(setPresenceCommand(SFE_XM125_PRESENCE_RESET_MODULE) != 0)
776776
{
777777
return 1;
778778
}
779+
delay(100); // give time for command to set
779780

780781
// Check detector status error and busy bits
781-
getPresenceDetectorErrorStatus(errorStatus);
782+
if(getPresenceDetectorErrorStatus(errorStatus) != 0)
783+
{
784+
return 2;
785+
}
782786
if(errorStatus != 0)
783787
{
784-
Serial.println("Error status errrrrr");
785-
return errorStatus;
786-
// return 2;
788+
return 3;
787789
}
788-
delay(100);
789790

790791
// Set Presence Start register
791792
if(setPresenceStart(300) != 0)
792793
{
793-
return 3;
794+
return 4;
794795
}
795-
delay(100);
796+
delay(100); // give time for command to set
796797

797798
// Set End register
798799
if(setPresenceEnd(2500) != 0)
799800
{
800-
return 4;
801+
return 5;
801802
}
802-
delay(100);
803+
delay(100); // give time for command to set
803804

804805
// Apply configuration
805806
if(setPresenceCommand(SFE_XM125_PRESENCE_APPLY_CONFIGURATION) != 0)
@@ -808,23 +809,24 @@ int32_t QwDevXM125::presenceDetectorStart()
808809
getPresenceDetectorErrorStatus(errorStatus);
809810
if(errorStatus != 0)
810811
{
811-
return 5;
812+
return 6;
812813
}
813814

814-
return 6;
815+
return 7;
815816
}
817+
delay(100); // give time for command to set
816818

817819
// Poll detector status until busy bit is cleared
818820
if(presenceBusyWait() != 0)
819821
{
820-
return 7;
822+
return 8;
821823
}
822824

823825
// Check detector error status
824826
getPresenceDetectorErrorStatus(errorStatus);
825827
if(errorStatus != 0)
826828
{
827-
return 8;
829+
return 9;
828830
}
829831

830832
// If no errors, return 0
@@ -849,6 +851,7 @@ int32_t QwDevXM125::getPresenceDistanceValuemm(uint32_t &presenceVal)
849851
{
850852
return 2;
851853
}
854+
delay(100);
852855

853856
// Poll detector status until busy bit is cleared - CHECK ON THIS!
854857
if(presenceBusyWait() != 0)
@@ -863,7 +866,6 @@ int32_t QwDevXM125::getPresenceDistanceValuemm(uint32_t &presenceVal)
863866
return 4;
864867
}
865868

866-
867869
// Read detector result register and determine detection status
868870
getPresenceDetectorPresenceDetected(presenceDetected);
869871
getPresenceDetectorPresenceStickyDetected(presenceDetectedSticky);
@@ -908,7 +910,7 @@ int32_t QwDevXM125::getPresenceDetectorStatus(uint32_t &status)
908910

909911
int32_t QwDevXM125::getPresenceDetectorErrorStatus(uint32_t &status)
910912
{
911-
int32_t retVal;
913+
int32_t retVal = 0;
912914
uint32_t regVal = 0;
913915
retVal = _theBus->readRegister16Region(SFE_XM125_PRESENCE_DETECTOR_STATUS, (uint8_t*)&regVal, 4);
914916
flipBytes(regVal);

0 commit comments

Comments
 (0)