 }
 
 void calcDistance() {
-  if (iterator == 20) {
-    iterator = 0;
-  }
-  prevLeft = leftDistance;
-  prevRight = rightDistance;
-/* The following trigPin/echoPin cycle is used to determine the
- distance of the nearest object by bouncing soundwaves off of it. */ 
-// digitalWrite(leftTrig, LOW); 
-// delayMicroseconds(2); 
-// digitalWrite(leftTrig, HIGH);
-// delayMicroseconds(10); 
-// digitalWrite(leftTrig, LOW);
-// duration = pulseIn(leftEcho, HIGH);
+ prevLeft = leftDistance;
+ prevRight = rightDistance;
+ //The following trigPin/echoPin cycle is used to determine the
+ //distance of the nearest object by bouncing soundwaves off of it.
  //Calculate the distance (in cm) based on the speed of sound.
  delay(10);
-  digitalWrite(leftTrig, LOW);
-  delayMicroseconds(5);
-  digitalWrite(leftTrig, HIGH);
-  delayMicroseconds(10);
-  digitalWrite(leftTrig, LOW);
-  pinMode(leftEcho, INPUT);
-  durationLeft = pulseIn(leftEcho, HIGH);
+ digitalWrite(leftTrig, LOW);
+ delayMicroseconds(5);
+ digitalWrite(leftTrig, HIGH);
+ delayMicroseconds(10);
+ digitalWrite(leftTrig, LOW);
+ pinMode(leftEcho, INPUT);
+ durationLeft = pulseIn(leftEcho, HIGH);
  leftDistance = durationLeft/58.2;  
 
-  delay(10);
- 
-// digitalWrite(rightTrig, LOW); 
-// delayMicroseconds(2); 
-// digitalWrite(rightTrig, HIGH);
-// delayMicroseconds(10); 
-// digitalWrite(rightTrig, LOW);
-// duration = pulseIn(rightEcho, HIGH);
-  digitalWrite(rightTrig, LOW);
-  delayMicroseconds(5);
-  digitalWrite(rightTrig, HIGH);
-  delayMicroseconds(10);
-  digitalWrite(rightTrig, LOW);
-  pinMode(rightEcho, INPUT);
-  durationRight = pulseIn(rightEcho, HIGH);
- //Calculate the distance (in cm) based on the speed of sound.
- rightDistance = durationRight/58.2;  
-
-// if((leftDistance<minimumRange)||(leftDistance>maximumRange)) {
-//  leftDistance = prevLeft;
-// }
-// if((rightDistance<minimumRange)||(rightDistance>maximumRange)) {
-//  rightDistance = prevRight;
-// }
-
-//button
-//esc code
- 
-// iterator++;
-// leftDistance = 0;
-// rightDistance = 0;
-// for (int j= 0; j < 20; j++) {
-//  leftDistance += lD[j];
-//  rightDistance += rD[j];
-// }
-// leftDistance = leftDistance/20;
-// rightDistance = rightDistance/20;
+ delay(10);
+
+ digitalWrite(rightTrig, LOW);
+ delayMicroseconds(5);
+ digitalWrite(rightTrig, HIGH);
+ delayMicroseconds(10);
+ digitalWrite(rightTrig, LOW);
+ pinMode(rightEcho, INPUT);
+ durationRight = pulseIn(rightEcho, HIGH);
+ rightDistance = durationRight/58.2; 
+
+ readLine();
 }
 
 /*

//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise

  digitalWrite(STBY, HIGH); //disable standby

  boolean inPin1 = LOW;
  boolean inPin2 = HIGH;

  if(direction == 1){
    inPin1 = HIGH;
    inPin2 = LOW;
  }

  if(motor == 1){
    digitalWrite(AIN1, inPin1);
    digitalWrite(AIN2, inPin2);
    analogWrite(PWMA, speed);
  }else{
    digitalWrite(BIN1, inPin1);
    digitalWrite(BIN2, inPin2);
    analogWrite(PWMB, speed);
  }
}

void stop(){

  pinMode(rightTrig, OUTPUT);
  pinMode(rightEcho, INPUT);

  pinMode(frontInput, INPUT);
  pinMode(leftInput, INPUT);
  pinMode(rightInput, INPUT);
  pinMode(frontLed, OUTPUT);
  pinMode(leftLed, OUTPUT);
  pinMode(rightLed, OUTPUT);
  
  readLine();
}

void loop(){
  search();
}

//line sensors take calibration reading
void readLine() {
 int ambient = 0;
 int lit = 0;
 int value = 0;

 digitalWrite(frontLed, LOW);
 delay(5);                            //To give ADC and LED transition time
 ambient = analogRead(frontInput);
 digitalWrite(frontLed, HIGH);
 delay(5);
 lit = analogRead(frontInput);
 if(initialize) {
  frontBaseVal = lit - ambient;  
 }
 frontVal = lit - ambient;
 
 digitalWrite(leftLed, LOW);
 delay(5);                            //To give ADC and LED transition time
 ambient = analogRead(leftInput);
 digitalWrite(leftLed, HIGH);
 delay(5);
 lit = analogRead(leftInput);
 if(initialize) {
  leftBaseVal = lit - ambient;
 }
 leftVal = lit - ambient;
 
 digitalWrite(rightLed, LOW);
 delay(5);                            //To give ADC and LED transition time
 ambient = analogRead(rightInput);
 digitalWrite(rightLed, HIGH);
 delay(5);
 lit = analogRead(rightInput);
 if(initialize) {
  rightBaseVal = lit - ambient;
 }
 rightVal = lit - ambient;
 if(!initialize) {
  checkLine();
 } else {
  initialize = false;
  Serial.println("initialization done.");
 }
}

void checkLine() {
  if((frontVal - frontBaseVal) >= lineThreshold) {
    recovery(1);
    return;
  }
  if((leftVal - leftBaseVal) >= lineThreshold) {
    recovery(2);
    return;
  }
  if((rightVal - rightBaseVal) >= lineThreshold) {
    recovery(3);
    return;
  }
}

void recovery(int sensor) {
  // 0 is forward, 1 is backward
  //move(1, 200, rightDir); //right motor
  //move(2, 200, leftDir); //left motor
  switch(sensor) {
    case 1:   //front, burst of back up
      Serial.println("FRONT TRIGGERED");
      move(1, 255, 1);
      move(2, 255, 1);
      delay(1000);
      break;  //left, burst forward, back up to right
    case 2:
      Serial.println("LEFT TRIGGERED");
      move(1, 255, 0);
      move(2, 255, 0);
      delay(500);
      move(1, 128, 1);
      move(2, 255, 1);
      delay(500);
      break;
    case 3:   //right, burst forward, back up to left
      Serial.println("RIGHT TRIGGERED");
      move(1, 255, 1);
      move(2, 255, 1);
      delay(500);
      move(2, 128, 1);
      move(1, 255, 1);
      delay(500);
      break;
  }
}

void move(int motor, int speed, int direction){
