 // threshold value we trigger an action.
 // we may want to wait 50ms between readings, since the waves may echo around for a while...
 void updateProxySens(){
-  if( ( millis() - proxy_sens_last_ping ) < PROXY_SENS_PING_SLEEP ){
+  uint32_t ps_current_time = millis();
+  if( ( ps_current_time - proxy_sens_last_ping ) < PROXY_SENS_PING_SLEEP ){
     // we return, since we want to have some time between pings.
     return;
   }
-  if( ( millis() - proxy_sens_last_action_triggered ) < PROXY_SENS_TRIGGER_SLEEP ){
+  if( ( ps_current_time - proxy_sens_last_action_triggered ) < PROXY_SENS_TRIGGER_SLEEP ){
     // means we will sleep for some time and don't trigger another action.
     return;
   }
-  unsigned int uS = sonar.ping();
-  //unsigned int uS = sonar.ping_median( 5 );
+  //unsigned int uS = sonar.ping();
+  unsigned int uS = sonar.ping_median( 5 );
+  proxy_sens_last_ping = ps_current_time;
   int current_distance = (int)( uS / US_ROUNDTRIP_CM );
 #ifdef PROXYSENS_DEBUG
   Serial << "ProxySens: distance is: " << current_distance << endl;


#include <NewPing.h>

#define proxy_sens_trig 12          // the pin to which the HC-SR04 trigger pin has been attached.
#define proxy_sens_echo 4           // the pin to which the HC-SR04 echo pin has been attached.
#define proxy_sens_max_distance 300 // the maximum distance.

int proxy_sens_basal_dist = 0;  // the basal, initial distance measured by the sensor.

// connection notes:
// Has to be connected to digital pins (NOT A...)
// Gnd -> GND
// Echo -> 10
// Trig -> 12
// Vcc -> +5V

