 };
 
 byte flags = 0; // Booleans
+
+
 
 volatile byte powerState = 0;     // 0 = ping off, 1 = ping on.
 volatile unsigned long awakeTime = 0; // When the cpu woke up.

#define F_ACTIVE_LEFT   4 // 1 = detection  0 = no detection
#define F_ACTIVE_RIGHT  5 // 1 = detection  0 = no detection

const byte DISTANCE_THRESHOLD = 40; // Less than this (in cm) activates the detection sequence.

// Pins

void createTransmitionMsg(char *movementDirection)
{
  msgId++;
  char buf[MSG_BUFFER_LEN];
  // Device rf id, message id, direction char, volts
  sprintf(buf, "%u,%lu,%c,%lu", RF_ID, msgId, movementDirection, readVcc());
  // TODO queue message for transmition multiple times.
  //transmit(buf); // just send it for now.
  msgEnqueue(buf);
}

void msgEnqueue(char *buf) 
{
  unsigned long now = millis();
  byte queued = 0;
  for (uint8_t i = 0; i < MSG_QUEUE_LENGTH; i++) {
    if (msgQueue[i][0] == 0) { // First character is zero.
      msgQueue[i] = buf;
      msgTime[i] = now;
      queued = 1; // Managed to add to the queue.
    }
  }
  
  if (queued == 0) {
    // Didn't get into the queue.
    byte oldestIndex = getOldestMsqQueueIndex(); 

    // Replace the oldest one
    msgQueue[oldestIndex] = buf;
    msgTime[oldestIndex] = now;
  }
}

byte getOldestMsqQueueIndex()
{
  unsigned long now = millis();
  long longestDiff = 0;
  byte oldestIndex = 0;
  // Find the oldest message.
  for (uint8_t i = 0; i < MSG_QUEUE_LENGTH; i++) {
    long diff = now - msgTime[i];
    if (diff > longestDiff) {
      longestDiff = diff;
      oldestIndex = i;
    }
  }
  
  return oldestIndex;
}

/**
 * Send the correct message for the correct number of times.
 */
void processMsgQueue()
{
  if (millis() - lastTransmit > MSG_TRANSMIT_DELAY) {
    // Time to send another transmition if one is waiting.
    byte oldestIndex = getOldestMsqQueueIndex(); 
    // Check for a message.
    if (strlen(msgQueue[oldestIndex]) == 0) {
      // nothing to do.
      return;
    }

    transmit(msgQueue[oldestIndex]);
    msgTransmitted[oldestIndex]++;
    
    // If the message has been sent enough times, delete it.
    if (msgTransmitted[oldestIndex] > MSG_TRANSMIT_NUM) {
      char buf[MSG_BUFFER_LEN] = {0};
      msgQueue[oldestIndex] = buf;
      msgTime[oldestIndex] = 0;
      msgTransmitted[oldestIndex] = 0;
    }
  }
}

/**
 * Transmit a message.
 */
void transmit(char *buf) 
{
