@@ -58,9 +58,16 @@ void setup() {
5858 Serial.print (F (" Threshold: " ));
5959 Serial.println (threshold);
6060
61- // Set up proximity interrupt for close detection
62- vcnl.setProxLowThreshold (0 );
61+ // Set up proximity interrupt for close/away detection
62+ // IMPORTANT: Interrupts fire on TRANSITIONS. The sensor must be below
63+ // the low threshold (far state) before it can trigger CLOSE by crossing
64+ // above the high threshold. Set low threshold above ambient so the
65+ // sensor starts in the "far" state.
66+ uint16_t lowThreshold = (ambient + threshold) / 2 ; // midpoint
67+ vcnl.setProxLowThreshold (lowThreshold);
6368 vcnl.setProxHighThreshold (threshold);
69+ Serial.print (F (" Low threshold: " ));
70+ Serial.println (lowThreshold);
6471 vcnl.setProxPersistence (VCNL4030_PROX_PERS_1);
6572 vcnl.setProxInterruptMode (VCNL4030_PROX_INT_BOTH);
6673
@@ -74,19 +81,19 @@ void setup() {
7481}
7582
7683void loop () {
84+ Serial.print (F (" Prox: " ));
85+ Serial.print (vcnl.readProximity ());
86+
7787 if (intFired) {
7888 intFired = false ;
79- uint16_t prox = vcnl.readProximity ();
8089 uint8_t flags = vcnl.readInterruptFlags ();
8190
82- Serial.print (F (" INT! Prox: " ));
83- Serial.print (prox);
84-
8591 if (flags & VCNL4030_PROX_IF_CLOSE)
86- Serial.print (F (" -> CLOSE" ));
92+ Serial.print (F (" *** IRQ CLOSE *** " ));
8793 if (flags & VCNL4030_PROX_IF_AWAY)
88- Serial.print (F (" -> AWAY" ));
89-
90- Serial.println ();
94+ Serial.print (F (" *** IRQ AWAY ***" ));
9195 }
96+
97+ Serial.println ();
98+ delay (100 );
9299}
0 commit comments