 void processEnergy() {
   // assess energy supply
   shortestDistanceToSource = 99;
-  for (int i =0; i<3; i++) if (distanceToSource[i] < shortestDistanceToSource) shortestDistanceToSource = distanceToSource[i];
+  for (int i =0; i<NUM_CONN; 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);
+    for (int i =0; i<NUM_CONN; 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);
+      for(int i=0; i<NUM_CONN; i++) digitalWrite(coil[i], LOW);
       addTimer(1000, resetConnectors);
     }
   }
   
-  for (int i=0; i<3; i++) distanceToSource[i] = 99;
+  for (int i=0; i<NUM_CONN; 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);
+  for (int i=0; i<NUM_CONN; i++) {
+    bus[i] = new SoftwareSerial(RX[i], TX[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);
+  for (int i=0; i<NUM_CONN; i++) bus[i]->begin(1200);
   resetConnectors();
   switchLampOff();
-  for (int i=0; i<3; i++) distanceToSource[i] = 99;
+  for (int i=0; i<NUM_CONN; i++) distanceToSource[i] = 99;
   shortestDistanceToSource = 99;
   alertState = false;
   

          alertState = true;
          alertTimer = addTimer(random(1000, 5000), dropConnector);
          // propagate signal though network
          for (int j=0; j<3; j++) if (j!=i) bus[j]->write(c);
        }
        break;
      case DROPPING_CONNECTOR: // some connector in the network is dropped

}

void resetConnectors() {
  for (int i=0; i<NUM_CONN; i++) digitalWrite(coil[i], HIGH);
}

void dropConnector() {
  // send out message that we're going to drop a connector
  char c = DROPPING_CONNECTOR;
  for (int i=0; i<NUM_CONN; i++) bus[i]->write(c);
  
  // pick random connector to drop
  int n = 0;
  for (int i=0; i<NUM_CONN; i++) if (distanceToSource[i]<99) n++;
  n = random(1,n+1);
  int i=0;
  while(n) {
