 #define DROPPING_CONNECTOR 254
 #define ALERT 255
 
-SoftwareSerialWithHalfDuplex* bus[3];
+#define NUM_CONN 3
 
-int RX[3] = {A0, A2, A4};
-int TX[3] = {A1, A3, A5};
-int coil[3] = {6, 5, 10}; // magnets
-int distanceToSource[3];
+SoftwareSerial* bus[NUM_CONN];
+
+int RX[NUM_CONN] = {A1, A3, A5};
+int TX[NUM_CONN] = {A0, A2, A4};
+int coil[NUM_CONN] = {6, 5, 10}; // magnets
+int distanceToSource[NUM_CONN];
 int shortestDistanceToSource;
 int lampState;
 int alertState;

void processEnergy() {
  // assess energy supply
  shortestDistanceToSource = 99;
  for (int i =0; i<3; i++) if (distanceToSource[i] < shortestDistanceToSource) shortestDistanceToSource = distanceToSource[i];
  
  // if we have energy, switch (or keep) lamp on, pass it on and check again in some time
  if (shortestDistanceToSource<99) {
    if (!lampState) switchLampOn();
    for (int i =0; i<3; i++) if (distanceToSource[i] > shortestDistanceToSource) bus[i]->write(shortestDistanceToSource);
    addTimer(100, processEnergy);
  }
  // if not, switch off the lamp and drop connectors if we were cut out
  else {
    if (lampState) {
      switchLampOff();
      for(int i=0; i<3; i++) digitalWrite(coil[i], LOW);
      addTimer(1000, resetConnectors);
    }
  }
  
  for (int i=0; i<3; i++) distanceToSource[i] = 99;
}

void setup() {
  // set up pins
  for (int i=0; i<3; i++) bus[i] = new SoftwareSerialWithHalfDuplex(RX[i], TX[i], false, false);
  for (int i=0; i<3; i++) pinMode(coil[i], OUTPUT);
  pinMode(leds1pin, OUTPUT);
  pinMode(leds2pin, OUTPUT);
  pinMode(button2pin, INPUT_PULLUP);
  
  // set initial states
  for (int i=0; i<3; i++) bus[i]->begin(1200);
  resetConnectors();
  switchLampOff();
  for (int i=0; i<3; i++) distanceToSource[i] = 99;
  shortestDistanceToSource = 99;
  alertState = false;
  

    if (!digitalRead(button2pin)) {
      if(SERIAL_DEBUG) Serial.println("alert");
      char c = ALERT; 
      for (int i=0; i<NUM_CONN; i++) bus[i]->write(c);
    }
  }
  
  // check all lines for incoming data
  for (int i=0;i<NUM_CONN;i++) if(bus[i]->available()) {
    char c = bus[i]->read();
    if(SERIAL_DEBUG) Serial.println("received: "+c);
    switch(c) {
