 // @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 13 // Debug led for tx
+#define DEBUG_LED_TX 11 // Debug led for tx
 #define DEBUG_LED_PULSE 12 // Debug led for alive pulse (tmp)
-#define DEBUG_LED_MOTION 11
+#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


void loop() 
{
  sleepNow();
  
  if (motion) {
    
    if (digitalRead(PIR_DOWN_PIN) == HIGH) {
      //digitalWrite(DEBUG_LED_MOTION, HIGH);
      
      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++;
    }  
    
    // 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 transmitId = 0;

long motionTime = 0;
long timeout = 60000; 


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

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