@@ -39,6 +39,11 @@ uint32_t regErr = 0;
3939// DELETE TEST VARIABLES ONCE COMPLETE
4040uint32_t startVal = 0 ;
4141uint32_t endVal = 0 ;
42+ uint32_t counter = 0 ;
43+ uint32_t intra = 0 ;
44+ uint32_t regVal = 0 ;
45+ uint32_t busyBit = 0 ;
46+ uint32_t busyError = 0 ;
4247
4348void setup ()
4449{
@@ -50,50 +55,38 @@ void setup()
5055 Wire.begin ();
5156
5257 // If begin is successful (0), then start example
53- if (radarSensor.begin (i2cAddress, Wire) == 1 )
58+ int startErr = radarSensor.begin (i2cAddress, Wire);
59+ if (startErr == 1 )
5460 {
5561 Serial.println (" Begin" );
5662 }
5763 else // Otherwise, infinite loop
5864 {
65+ Serial.print (" Start Error Code: " );
66+ Serial.println (startErr);
5967 Serial.println (" Device failed to setup - Freezing code." );
6068 while (1 ); // Runs forever
6169 }
6270
6371 delay (200 );
6472
6573
74+ // Test code setup below:
75+ // Reset sensor configuration to reapply configuration registers
76+ radarSensor.setPresenceCommand (SFE_XM125_PRESENCE_RESET_MODULE);
6677
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- }
78+ // Check error and busy bits
79+ radarSensor.getPresenceDetectorErrorStatus (errorStatus);
80+ if (errorStatus != 0 )
81+ {
82+ Serial.print (" Detector status error: " );
83+ Serial.println (errorStatus);
84+ }
9285
93- void loop ()
94- {
86+ delay ( 100 );
87+
9588 // Set Start register
96- if (radarSensor.setPresenceStart (1000 ) != 0 )
89+ if (radarSensor.setPresenceStart (300 ) != 0 )
9790 {
9891 Serial.println (" Presence Start Error" );
9992 }
@@ -103,79 +96,126 @@ void loop()
10396
10497 delay (100 );
10598 // Set End register
106- if (radarSensor.setPresenceEnd (5000 ) != 0 )
99+ if (radarSensor.setPresenceEnd (5200 ) != 0 )
107100 {
108101 Serial.println (" Presence End Error" );
109102 }
110103 radarSensor.getPresenceEnd (endVal);
111104 Serial.print (" End Val: " );
112105 Serial.println (endVal);
113-
114106 delay (100 );
107+
115108 // Apply configuration
116109 if (radarSensor.setPresenceCommand (SFE_XM125_PRESENCE_APPLY_CONFIGURATION) != 0 )
117110 {
111+ // Check for errors
112+ radarSensor.getPresenceDetectorErrorStatus (errorStatus);
113+ if (errorStatus != 0 )
114+ {
115+ Serial.print (" Detector status error: " );
116+ Serial.println (errorStatus);
117+ }
118+
118119 Serial.println (" Configuration application error" );
119120 }
120-
121- delay (100 );
121+
122122 // Poll detector status until busy bit is cleared - CHECK ON THIS!
123123 if (radarSensor.presenceBusyWait () != 0 )
124124 {
125- Serial.println (" Busy wait error" );
125+ Serial.print (" Busy wait error: " );
126+ Serial.println (radarSensor.getPresenceRegisterVal (busyError));
126127 }
128+
129+ // Check detector status
130+ radarSensor.getPresenceDetectorErrorStatus (errorStatus);
131+ if (errorStatus != 0 )
132+ {
133+ Serial.print (" Detector status error: " );
134+ Serial.println (errorStatus);
135+ }
136+
137+ Serial.println ();
138+
139+ // Original code below:
140+ // // Default start = 1000; Default stop = 5000
141+ // int32_t sensorStartError = radarSensor.presenceDetectorStart();
142+ // if(sensorStartError != 0)
143+ // {
144+ // Serial.println("Sensor Started Successfully");
145+ // }
146+ // else
147+ // {
148+ // Serial.println("Sensor not initialized correctly - Freezing code.");
149+ // while(1); // Runs forever
150+ // }
151+
127152
128- delay (100 );
153+ delay (1000 );
154+ // while(1);
155+ }
156+
157+ void loop ()
158+ {
159+
129160 // Check error bits
161+ radarSensor.getPresenceDetectorErrorStatus (errorStatus);
162+ if (errorStatus != 0 )
163+ {
164+ Serial.print (" Detector status error: " );
165+ Serial.println (errorStatus);
166+ }
167+
130168
131-
132- delay (100 );
133169 // Start detector
134170 if (radarSensor.setPresenceCommand (SFE_XM125_PRESENCE_START_DETECTOR) != 0 )
135171 {
136172 Serial.println (" Start detector error" );
137173 }
138174
139- delay (100 );
140175 // Poll detector status until busy bit is cleared - CHECK ON THIS!
141176 if (radarSensor.presenceBusyWait () != 0 )
142177 {
143178 Serial.println (" Busy wait error" );
144179 }
145180
146- delay (100 );
147181 // 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 )
182+ radarSensor.getPresenceDetectorErrorStatus (errorStatus);
183+ if (errorStatus != 0 )
150184 {
151185 Serial.println (" Detector status error" );
152186 }
153187
154- delay ( 100 );
188+
155189 // Read detector result register and determine
156190 radarSensor.getPresenceDetectorPresenceDetected (presenceDetected);
157191 radarSensor.getPresenceDetectorPresenceStickyDetected (presenceDetectedSticky);
158192 radarSensor.getPresenceDetectorRegError (regErr);
193+ radarSensor.getPresenceRegisterVal (regVal);
194+
195+
196+ // Serial.print("Presence Detected: ");
197+ // Serial.println(presenceDetected);
198+ // Serial.print("Presence Detected Sticky: ");
199+ // Serial.println(presenceDetectedSticky);
159200
160- if ((presenceDetected == 1 ) & (presenceDetectedSticky == 1 ))
201+ if ((presenceDetected == 1 ) | (presenceDetectedSticky == 1 ))
161202 {
162203 radarSensor.getPresenceDistance (distance);
163204 Serial.print (" Presence Detected: " );
164205 Serial.print (distance);
165206 Serial.println (" mm" );
166207 }
167208
168- // Reset sensor configuration to reapply configuration registers
169209 // radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_RESET_MODULE);
170210
171- Serial.println (" loop completed " );
211+ Serial.println ();
172212
173- delay (2000 );
213+ delay (1000 );
174214
175215
176216 // ***** PREVIOUS CODE BELOW *****
177217
178- // radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_START_DETECTOR );
218+ // radarSensor.setPresenceCommand(XM125_PRESENCE_APPLY_CONFIGURATION );
179219 // // If Presence is detected, then print out distance from device
180220 // radarSensor.presenceBusyWait();
181221 // radarSensor.getPresenceDetectorPresenceDetected(presenceDetected);
0 commit comments