 // 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;

  // simple stuff, if we are closer than 4cm trigger an action.
  if( current_distance < PROXY_SENS_TRIGGER_DISTANCE ){
    switchLights();
  }
}


//int PROXY_SENS_THRESH = 1;      // the threshold value, if we get closer by PROXY_SENS_THRESH cm we trigger an action.
int PROXY_SENS_TRIGGER_DISTANCE = 4;    // trigger distance in cm. if we get closer we trigger an action.
uint32_t PROXY_SENS_TRIGGER_SLEEP = 1000;  // time in ms we don't trigger another action if one was triggered.
uint32_t PROXY_SENS_PING_SLEEP = 70;        // time in ms we wait between pings.
uint32_t proxy_sens_last_ping = 0;          // last time we sent a ping.

NewPing sonar( proxy_sens_trig, proxy_sens_echo, proxy_sens_max_distance );
