         pwm_out[TB2] = 0;
         
         switch (st) {
+            // Moving right.
             case 0: case 3: 
                 pwm_out[TB2] = 80;
             break;
+            
+            // Moving Left.
             case 2: case 5: 
                 pwm_out[TB1] = 60;
             break;
+            
+            // Moving down.
             case 1:
                 pwm_out[LR1] = 255;
                 pwm_out[L2] = 240;
                 pwm_out[R2] = 240;
-                break;
+            break;
+                
+            // Moving up.
             case 4:
                 pwm_out[LR1] = 100;
-                break;
+            break;
         }
         
+        // Write the speeds.
         analogWrite(TB1, pwm_out[TB1]);
         analogWrite(TB2, pwm_out[TB2]);
         analogWrite(R2, pwm_out[R2]);
         analogWrite(L2, pwm_out[L2]);
         analogWrite(LR1, pwm_out[LR1]);
         
+        // Set next_dist according to the current state and width
+        // Default moving left or right.
         next_dist = curr_width;
+        
+        // Moving down. 
         if (st == 1) next_dist = DOWNHEIGHT;
+        
+        // Moving up.
         if (st == 4) next_dist = UPHEIGHT;
+        
+        // Moving fast. Change states well before the destination.
         if (state % 3 == 0) next_dist -= BUFFER + BUFFER;
+        
+        // Moving slow. Starts looking for edges when it is close.
         if (state % 3 == 1) next_dist -= BUFFER;
+        
+        // Set the failsafe in case it misses the edge.
         if ((state % 9 == 5) && (state % 9 == 8)) next_dist += SLACK;
     }
     
-        delayMicroseconds(25000); 
-        digitalWrite(13, HIGH);
-        delayMicroseconds(25000);
-        digitalWrite(13, LOW);
-        
+    // delay 50 ms and blink the light.
+    delayMicroseconds(25000); 
+    digitalWrite(13, HIGH);
+    delayMicroseconds(25000);
+    digitalWrite(13, LOW);
+        
+    // Only print every 20 cycles
     printCtr = (printCtr + 1) % 20;
     
     if (printCtr == 0) {
         int xPos, yPos;
+        // Moving up or down.
         if ((st % 3) == 1) {
             xPos = curr_width / 100;
+            // moving down.
             if (st < 3) yPos = map(distance_LR, 0, DOWNHEIGHT, 28, 0);
+            // Moving up.
             else yPos = map(distance_LR, 0, UPHEIGHT, 0, 28);
         }
+        // Moving left or right.
         else {
+            // At the top.
             if ((st == 0) || (st == 5)) yPos = 28;
+            // At the bottom
             else yPos = 0;
+            // Moving left.
             if ((st % 3) == 0) xPos = distance_TB / 100;
+            // Moving right.
             else xPos = (curr_width - distance_TB) / 100;
         }
+        
+        // Print the status.
         Serial.print("\nState: ");
         Serial.print(state);
         Serial.print(" x: ");

    widths[4] = 9*WIDTH/2;
    widths[5] = 11*WIDTH/2;
    
    curr_width = widths[(state/18)%3];
    next_dist = curr_width - BUFFER - BUFFER;

    // Encoders
    attachInterrupt(1, en_TB, RISING);
    attachInterrupt(0, en_LR, RISING);

    sei();
}

void loop() {
    int sta = (state % 18);
    int st = sta / 3;
    int distance = distance_TB;
    if (st % 3 == 1) distance = distance_LR;
    
    if (    ((state % 9  ==  8) && (adc_val[LEFTRIGHT] > HIGH_THRESHOLD_LR)) ||
            ((state % 18 ==  5) && (adc_val[TOPBOTTOM] <  LOW_THRESHOLD_TB)) ||
            ((state % 18 == 14) && (adc_val[TOPBOTTOM] > HIGH_THRESHOLD_TB)))
        next_dist = 0;
        
    if (distance > next_dist) {
        state++;
        sta = (state % 18);
        st = sta / 3;
        if (state % 9 == 0) curr_width = widths[(state/9)%6];
        
        if (state % 3 == 0) { distance_TB = 0; distance_LR = 0; }
        
        pwm_out[L2] = 255;
        pwm_out[R2] = 255;
        pwm_out[LR1] = 240;

    }
}

// Called when an analog-digital conversion completes
ISR(ADC_vect)
{
    // Record and amplify the value.
    unsigned long analogVal = (ADCL | (ADCH << 8)) << 4;
    
    // Add the new value to the rolling exponential buffer for that pin.
    adc_val[adc_pin] += (analogVal >> 6) - (adc_val[adc_pin] >> 6);
    
    // Set the next ADC conversion to happen.
    adc_pin = (adc_pin + 1) % 6;
    
    // Start the next analog-digital conversion
    ADMUX &= B11110000; ADMUX |= adc_pin; ADCSRA |= B01000000;
}
