 #define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
 
 // Flags definitions
-#define F_MOTION 1 // 1 = activated  0 = no movement
-#define F_LEFT   2 // 1 = detection  0 = no detection
-#define F_RIGHT  3 // 1 = detection  0 = no detection
-#define F_ACTIVE_LEFT   4 // 1 = detection  0 = no detection
-#define F_ACTIVE_RIGHT  5 // 1 = detection  0 = no detection
+#define F_MOTION 0 // 1 = activated  0 = no movement
+#define F_LEFT   1 // 1 = detection  0 = no detection
+#define F_RIGHT  2 // 1 = detection  0 = no detection
+#define F_ACTIVE_LEFT   3 // 1 = detection  0 = no detection
+#define F_ACTIVE_RIGHT  4 // 1 = detection  0 = no detection
 
 #define MSG_BUFFER_LEN   30    // How long the transmitted message can be.
 #define MSG_QUEUE_LENGTH 3     // How many transmission message to store.
 #define MSG_TRANSMIT_NUM 5     // How many times to transmit each message.
 #define MSG_TRANSMIT_DELAY 100 // How long to wait before sending the next message.
 
-const byte DISTANCE_THRESHOLD = 40; // Less than this (in cm) activates the detection sequence.
+const byte DISTANCE_THRESHOLD = 30; // Less than this (in cm) activates the detection sequence.
 
 // Pins
 const byte PIN_PIR = 2;         // PIR on external interupt to wake up the cpu, int.0 interupt 0 on pin 2

    msgQueue[oldestIndex] = buf;
    msgTime[oldestIndex] = now;
  }
}

byte getOldestMsqQueueIndex()
{

  
  // The motion ISR sets a flag so we know it detected motion.
  if (bitRead(flags, F_MOTION)) {
    Serial.println("MOTION");
    bitClear(flags, F_MOTION);
    if (powerState == 0) {
      // Remember when the cpu woke up.
