 
 void loop() 
 {
-  sleepNow();
+  if (!bitRead(flags, F_DOWN) && !bitRead(flags, F_UP)) {
+    digitalWrite(DEBUG_LED_MOTION, LOW);
+    sleepNow();
+  } else {
+    digitalWrite(DEBUG_LED_MOTION, HIGH);
+  }
+  
+  // Wait a while waiting for the next pir to detect 
+  unsigned long currentMillis = millis();
+  if (currentMillis - motionTime > timeout) {
+     bitClear(flags, F_UP);
+     bitClear(flags, F_DOWN);
+  }
+  
   
   if (motion) {
+    motionTime = currentMillis;
+    
     
     if (digitalRead(PIR_DOWN_PIN) == HIGH) {
-      //digitalWrite(DEBUG_LED_MOTION, HIGH);
+      //
+      // Has the other pir recently detected motion
+      if (bitRead(flags, F_UP)) {
+        // Report that someone just went down stairs.
+        transmitId++;
+        char buf[50];
+        sprintf(buf, "id=%lu,mv=%u,d=DOWN", transmitId, readVcc());
+        transmit(buf);
+        bitClear(flags, F_UP);
+        bitClear(flags, F_DOWN);
+      } else {
+        // Remember that it happened
+        bitSet(flags, F_DOWN);
+      }
+      //digitalWrite(DEBUG_LED_MOTION, LOW);
       
-      char buf[50];
-      sprintf(buf, "c=%lu,mv=%u,F_DOWN_STAT", count, readVcc());
-      transmit(buf);
-      
-      count++;
     } else if (digitalRead(PIR_UP_PIN) == HIGH) {
       //digitalWrite(DEBUG_LED_MOTION, HIGH);
-    
-      char buf[50];
-      sprintf(buf, "c=%lu,mv=%u,F_UP_STAT", count, readVcc());
-      transmit(buf);
-      
-      count++;
+      // Has the other pir recently detected motion
+      if (bitRead(flags, F_DOWN)) {
+        // Report that someone just went up stairs.
+        transmitId++;
+        char buf[50];
+        sprintf(buf, "id=%lu,mv=%u,d=UP", transmitId, readVcc());
+        transmit(buf);
+        bitClear(flags, F_DOWN);
+        bitClear(flags, F_UP);
+      } else {
+        // Remember that it happened
+        bitSet(flags, F_UP);
+      }
+      //digitalWrite(DEBUG_LED_MOTION, LOW);
+     
     }  
-    
+
     // Motion delt with, reset to zero
     motion = 0;
   }
-  
-  /*
-  if (motionUp) {
-    motionUp = 0;
-    char buf[50];
-    sprintf(buf, "c=%lu,F_UP_STAT", count);
-    transmit(buf);
-    
-    count++;
-  }
-  
-  if (motionDown) {
-    motionDown = 0;
-    char buf[50];
-    sprintf(buf, "c=%lu,F_DOWN_STAT", count);
-    transmit(buf);
-    
-    count++;
-  }
-  */
-  
-  /*
-  // Movement detected
-  if (bitRead(flags, F_UP_STAT)) {
-    bitClear(flags, F_UP_STAT);
-    bitClear(flags, F_UP);
-    
-    if (digitalRead(PIR_UP_PIN) == HIGH) {
-      //digitalWrite(DEBUG_LED_MOTION, HIGH);
-    
-      char buf[50];
-      sprintf(buf, "c=%lu,F_UP_STAT", count);
-      transmit(buf);
-      
-      count++;
-    }
-    
-
-    
-  } 
-  
-  if (bitRead(flags, F_DOWN_STAT)) {
-    bitClear(flags, F_DOWN_STAT);
-    bitClear(flags, F_DOWN);
-    
-    if (digitalRead(PIR_DOWN_PIN) == HIGH) {
-      //digitalWrite(DEBUG_LED_MOTION, HIGH);
-      
-      char buf[50];
-      sprintf(buf, "c=%lu,F_DOWN_STAT", count);
-      transmit(buf);
-      
-      count++;
-    }
-    
-    
-  }
-  */
-
-}
-
-void XXloop()
-{
-  
-  unsigned long currentMillis = millis();
-  if (currentMillis - previousMillis > interval) {
-    previousMillis = currentMillis;
-    // TODO: reset previousMillis when overruns max value.
-    
-    pulse = !pulse;
-    digitalWrite(DEBUG_LED_PULSE, pulse);
-    
-
-    // Initial movement detected
-    if (bitRead(flags, F_UP_STAT)) {
-      lastPirUp = currentMillis;
-      //Serial.println("UP on");
-      bitClear(flags, F_UP_STAT);
-      digitalWrite(DEBUG_LED_MOTION, HIGH);
-    }
-    
-    // Initial movement detected
-    if (bitRead(flags, F_DOWN_STAT)) {
-      lastPirDown = currentMillis;
-      //Serial.println("DOWN on");
-      bitClear(flags, F_DOWN_STAT);
-      digitalWrite(DEBUG_LED_MOTION, HIGH);
-    }
-    
-
-    
-    if (bitRead(flags, F_UP) && currentMillis - lastPirUp > pirReset) {
-      bitClear(flags, F_UP);
-      //Serial.println("UP off");
-      digitalWrite(DEBUG_LED_MOTION, LOW);
-    }
-      
-    if (bitRead(flags, F_DOWN) && currentMillis - lastPirDown > pirReset) {
-      bitClear(flags, F_DOWN);
-      //Serial.println("DOWN off"); 
-      digitalWrite(DEBUG_LED_MOTION, LOW);
-    }
-      
-    if (bitRead(flags, F_UP) || bitRead(flags, F_DOWN) || pulse) { 
-    //if (pulse) {
-      char buf[50];
-      sprintf(buf, "count=%lu,mv=%u,currentMillis=%lu", count, readVcc(), currentMillis); 
-      //sprintf(buf, "count=%lu,flags=%u", count, flags); 
-
-      if (bitRead(flags, F_UP)) { 
-        strcat(buf, " UP ");
-      }
-      
-      if (bitRead(flags, F_DOWN)) { 
-        strcat(buf, " DOWN ");
-      }
-        
-      
-        
-      //Serial.println(buf);
-      
-      /*
-      digitalWrite(RF_POWER_PIN, HIGH); // power up the transmitter
-      digitalWrite(DEBUG_LED_TX, true); // Flash a light to show transmitting
-      vw_send((uint8_t *)buf, strlen(buf));
-      vw_wait_tx(); // Wait until the whole message is gone
-      digitalWrite(DEBUG_LED_TX, false);
-      digitalWrite(RF_POWER_PIN, LOW); // power down the transmitter
-      */
-      transmit(buf);
-      
-      count++;
-    }
-    
-  }
-  
-  
-
-}
+ 
+}
+

#define F_UP_STAT 3     // 1 = new  0 = old
#define F_DOWN_STAT 4   // 1 = new  0 = old

unsigned long count = 0;

long previousMillis = 0;
long interval = 1000; 

long pirReset = 3000; // How long to ignore further detects.
long lastPirUp = 0;
long lastPirDown = 0;

volatile boolean pulse = false;


// Store the pir states on interupt.
volatile byte flags = 0;

volatile byte motionUp = 0;
volatile byte motionDown = 0;

volatile byte motion = 0;

// Motion on either interupt
void isrMotion() {
  motion = 1;
}

// Interupt on PIR_UP_PIN
void isrPirUp() 
{
  // Motion detected.
  bitSet(flags, F_UP);
  bitSet(flags, F_UP_STAT);
  motionUp = 1;
  /*
  // Cannot detect both at once, so see if the other fired too.
  if (!bitRead(flags, F_DOWN_STAT)) {
    if (digitalRead(PIR_DOWN_PIN) == HIGH) {
      isrPirDown();
    }
  }
  */
  
}

// Interupt on PIR_DOWN_PIN
void isrPirDown() 
{
  // Motion detected.
  bitSet(flags, F_DOWN);
  bitSet(flags, F_DOWN_STAT);
  motionDown = 1;
  
  /*
  // Cannot detect both at once, so see if the other fired too.
  if (!bitRead(flags, F_UP_STAT)) {
    if (digitalRead(PIR_UP_PIN) == HIGH) {
      isrPirUp();
    }
  }
  */
}

/**
 * @see http://forum.arduino.cc/index.php/topic,85627.0.html

// @see http://www.airspayce.com/mikem/arduino/VirtualWire_8h.html
#define RF_TX_PIN 9    // Pin 12 is the default sender pin so change it here.
#define RF_POWER_PIN 10 // Provide power to the transmitter
#define DEBUG_LED_TX 11 // Debug led for tx
#define DEBUG_LED_PULSE 12 // Debug led for alive pulse (tmp)
#define DEBUG_LED_MOTION 13

#define PIR_UP_PIN 2        // int.0 interupt 0 on pin 2
#define PIR_DOWN_PIN 3      // int.1 interupt 1 on pin 3
