         leftDir = 0;
       }
     }
-    if((leftDistance <= maximumRange && leftDistance >= minimumRange) &&
-       (rightDistance <= maximumRange && rightDistance >=minimumRange)) {
+    if((leftDistance <= maxRange && leftDistance >= minRange) &&
+       (rightDistance <= maxRange && rightDistance >=minRange)) {
         found = true;
         //Serial.println("FOUND");
       stop();  

  }
}

void follow() {
  while(!tagged) {
    calcDistance();
    diff = leftDistance - rightDistance;
    diffMag = abs(diff);

    if(!(leftDistance <= maximumRange && leftDistance >= minimumRange) &&
       !(rightDistance <= maximumRange && rightDistance >=minimumRange)) {
        found = false;
        lost = true;
        search();

#include <Servo.h>

//BLDC ESC parameters
//Used: VXL-3s Traxxas Single Channel ESCs
Servo leftMotor, rightMotor;
#define leftMotPin  10 
#define rightMotPin 9

//Line sensor parameters
//Used: RPR-220
//LF = left front, LB = left back
//RF = right front, RB = right back
#define LFinput A0
#define LFled   5
#define LBinput A1
#define LBled   4
#define RFinput A2
#define RFled   3
#define RBinput A3
#define RBled   2

int inputPins[4] = {LFinput, LBinput, RFinput, RBinput};
int ledPins[4] = {LFled, LBled, RFled, RBled};

#define lineThreshold 100
boolean initialize = true;
long LFbaseVal, LBbaseVal, RFbaseVal, RBbaseVal;
long bvals[4] = {LFbaseVal, LBbaseVal, RFbaseVal, RBbaseVal};
long LFval, LBval, RFval, RBval;
long vals[4] = {LFval, LBval, RFval, RBval};

//Ultrasonic range finder parameters
//Used: HC-SR04
#define leftEcho 11
#define leftTrig 12
#define rightEcho 8
#define rightTrig 7

//Distance parameters
#define maxRange 20
#define minRange 1
long leftDistance = 1000;
long rightDistance = 1000;
long prevLeft, prevRight;
long durationLeft, durationRight;

//Search parameters
boolean found = false; //true when enemy located
boolean lost = false;  //true after enemy located for first time

//Follow parameters
boolean tagged = false; //unclear usage

//Motor speed parameters
#define baseSpeed 128
int leftMod = 0;
int rightMod = 0;
int leftSpeed = 0;
int rightSpeed = 0;

//Motor direction parameters
int diff;
int diffMag;
int leftDir = 0;
int rightDir = 1;

void setup() {
  Serial.begin(9600);
  pinMode(LFinput, INPUT);
  pinMode(LFled, OUTPUT);
  pinMode(LBinput, INPUT);
  pinMode(LBled, OUTPUT);
  pinMode(RFinput, INPUT);
  pinMode(RFled, OUTPUT);
  pinMode(RBinput, INPUT);
  pinMode(RBled, OUTPUT);
  
  pinMode(leftTrig, OUTPUT);
  pinMode(leftEcho, INPUT);
  pinMode(rightTrig, OUTPUT);
  pinMode(rightEcho, INPUT);

  leftMotor.attach(leftMotPin);
  rightMotor.attach(rightMotPin);

  readLine();
}

void loop() {
  search();
}

//readLine
void readLine() {   //line sensors take calibration reading
 int ambient = 0;
 int lit = 0;
 int value = 0;
 //ORDER: LF LB RF RB
 for(int k = 0; k < 4; k++) {
  digitalWrite(ledPins[k], LOW);
  delay(5);   //To give ADC and LED transition time
  ambient = analogRead(inputPins[k]);
  digitalWrite(ledPins[k], HIGH);
  delay(5);
  lit = analogRead(inputPins[k]);
  if(initialize) {
    bvals[k] = lit - ambient;
  }
  vals[k] = lit - ambient;
 }
 if(!initialize) {
  checkLine();
 } else {
