 #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;
+unsigned long transmitId = 0;
+
+long motionTime = 0;
+long timeout = 60000; 
 
 
 // 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


void transmit(char *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 
}

void setup()
{
    // @see http://forum.arduino.cc/index.php/topic,85627.0.html
    //power_adc_disable();
    
    pinMode(DEBUG_LED_TX, OUTPUT);
    pinMode(DEBUG_LED_PULSE, OUTPUT);
    pinMode(DEBUG_LED_MOTION, OUTPUT);
    
    


void loop() 
{
  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) {
      //
      // 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);
      
    } else if (digitalRead(PIR_UP_PIN) == HIGH) {
      //digitalWrite(DEBUG_LED_MOTION, HIGH);
      // 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;
  }
 
}

