 }
 
 
+void update_single_lever_led(int pin, int number) {
+  if (position_is_ok[number] == false) {
+    if ((millis() % 1000) > 500) {
+      digitalWrite(pin, HIGH);
+    }
+    else {
+      digitalWrite(pin, LOW);
+    }
+  }
+  else {
+    if (movement_allowed(number)) {
+      digitalWrite(pin, LOW);
+    }
+    else {
+      digitalWrite(pin, HIGH);
+    }
+  }
+}
+
+/* void update_single_lever_led(int pin, int number) { */
+/*   if ((millis() % 1000) > 500) { */
+/*     digitalWrite(pin, HIGH); */
+/*   } */
+/*   else { */
+/*     digitalWrite(pin, LOW); */
+/*   } */
+/* } */
+
+// xxxx
+
 void setup() {
-  // TODO: attach the pins and hook up react_to_movement() functions.
+  pinMode(13, OUTPUT);
+  digitalWrite(13, LOW);
+
+  // Attach the pins and hook up react_to_movement() functions.
+  for (int number = 0; number < ARRAY_SIZE; number++) {
+    levers[number] = Bounce();
+  }
+
+  levers[SWITCH_5].attach(PIN_LEVER_SWITCH_5, INPUT_PULLUP);
+  levers[SWITCH_1].attach(PIN_LEVER_SWITCH_1, INPUT_PULLUP);
+  levers[SWITCHING_PERMISSION].attach(PIN_LOCK_SWITCHING_PERMISSION, INPUT_PULLUP);
+  levers[ROUTE_A1].attach(PIN_LEVER_ROUTE_A1, INPUT_PULLUP);
+  levers[ROUTE_A2].attach(PIN_LEVER_ROUTE_A2, INPUT_PULLUP);
+  levers[ROUTE_BERENBACH1].attach(PIN_LEVER_ROUTE_BERENBACH1, INPUT_PULLUP);
+  levers[ROUTE_BERENBACH2].attach(PIN_LEVER_ROUTE_BERENBACH2, INPUT_PULLUP);
+  levers[SIGNAL_A1].attach(PIN_LEVER_SIGNAL_A1, INPUT_PULLUP);
+  levers[SIGNAL_A2].attach(PIN_LEVER_SIGNAL_A2, INPUT_PULLUP);
+  levers[ROUTE_A_FIXATION].attach(PIN_BUTTON_ROUTE_A_FIXATION, INPUT_PULLUP);
+
+  for (int number = 0; number < ARRAY_SIZE; number++) {
+    levers[number].interval(20);
+  }
+  // Special case: route fixation button should take a second to activate.
+  levers[ROUTE_A_FIXATION].interval(1000);
+
+  // Attach the LEDs.
+  pinMode(PIN_LED_SWITCH_5, OUTPUT);
+  pinMode(PIN_LED_SWITCH_1, OUTPUT);
+  pinMode(PIN_LED_SIGNAL_A2, OUTPUT);
+  pinMode(PIN_LED_SIGNAL_A1, OUTPUT);
+  pinMode(PIN_LED_SWITCHING_PERMISSION, OUTPUT);
+  pinMode(PIN_LED_ROUTE_A, OUTPUT);
+  pinMode(PIN_LED_ROUTE_BERENBACH, OUTPUT);
+  pinMode(PIN_LED_ROUTE_A_FIXATION, OUTPUT);
+
+  // TODO: look up the initial lever positions and update accordingly.
 }
 
 
 void loop() {
-  // TODO: update the LEDs and update the outgoing pins.
-}
+  for (int number = 0; number < ARRAY_SIZE; number++) {
+    levers[number].update();
+    if (levers[number].rose()) {
+      react_to_movement(number, PLUS);
+    }
+    if (levers[number].fell()) {
+      react_to_movement(number, MINUS);
+    }
+  }
+  // TODO: update the LEDs.
+  update_single_lever_led(PIN_LED_SWITCH_5, SWITCH_5);
+  update_single_lever_led(PIN_LED_SWITCH_1, SWITCH_1);
+  update_single_lever_led(PIN_LED_SIGNAL_A1, SIGNAL_A1);
+  update_single_lever_led(13, SIGNAL_A1);
+  update_single_lever_led(PIN_LED_SIGNAL_A2, SIGNAL_A2);
+  update_single_lever_led(PIN_LED_SWITCHING_PERMISSION, SWITCHING_PERMISSION);
+  // TODO: Update other three leds
+
+  // TODO: update the outgoing pins.
+}

int ROUTE_A2 = 4;
int ROUTE_BERENBACH1 = 5;
int ROUTE_BERENBACH2 = 6;
int ROUTE_A_FIXED = 7;
int SIGNAL_A1 = 8;
int SIGNAL_A2 = 9;


  }

  // Route fixation blocks routes.
  if (current_position[ROUTE_A_FIXATION] == MINUS) {
    blocked[ROUTE_A1] = true;
    blocked[ROUTE_A2] = true;
    // Special case: also keep yourself blocked (it is only un-blocked by
    // moving a signal back to PLUS).
    blocked[ROUTE_A_FIXATION] = true;
  }
}


void change_position(int number,
                     int new_position) {
  if (new_position == MINUS) {
    digitalWrite(13, HIGH);
  }
  else {
    digitalWrite(13, LOW);
  }

  current_position[number] = new_position;
  position_is_ok[number] = true;
  update_requirements();
