 void search() {
   while(!found) {
     // 0 is forward, 1 is backward
-    move(1, 128, 1); //right motor
-    move(2, 128, 0); //left motor
+    move(1, 128, rightDir); //right motor
+    move(2, 128, leftDir); //left motor
     calcDistance();
-    Serial.println(leftDistance);
+    //Serial.print("left: ");
+    Serial.print(leftDistance);
+    Serial.print(",");
     Serial.println(rightDistance);
     delay(10);
-    if((leftDistance <= maximumRange && leftDistance >= minimumRange) ||
+    if(lost) {
+      if(diff>0){//turn right
+        rightDir = 0;
+        leftDir = 1;
+      } else {//turn left
+        rightDir = 1;
+        leftDir = 0;
+      }
+    }
+    if((leftDistance <= maximumRange && leftDistance >= minimumRange) &&
        (rightDistance <= maximumRange && rightDistance >=minimumRange)) {
         found = true;
-        Serial.println("FOUND");
+        //Serial.println("FOUND");
       stop();  
+      follow();
       //uncomment for looped testing of search function  
       //delay(1000);
       //found = false;

}

void calcDistance() {
/* 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);
 //Calculate the distance (in cm) based on the speed of sound.
 leftDistance = duration/58.2;  
 
 digitalWrite(rightTrig, LOW); 
 delayMicroseconds(2); 
 digitalWrite(rightTrig, HIGH);
 delayMicroseconds(10); 
 digitalWrite(rightTrig, LOW);
 duration = pulseIn(rightEcho, HIGH);
 //Calculate the distance (in cm) based on the speed of sound.
 rightDistance = duration/58.2;  
}

/*

#define rightTrig 6 // right Trigger Pin

// POSSIBLY CHANGE THIS FOR ARENA SIZE
int maximumRange = 150; // Maximum range needed
int minimumRange = 1; // Minimum range needed
long leftDistance = 1000;
long rightDistance = 1000;
long prevLeft;
long prevRight;
long durationLeft, cmLeft, inchesLeft, cmRight, inchesRight, durationRight;
long lD[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
long rD[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int iterator = 0;

boolean found = false;  // used for search()
boolean lost = false;

boolean tagged = false; // used for follow()
int rightDirMod = 0;
int leftDirMod = 0;

//------------------------------
#define baseSpeed 128
int leftMod = 0;
int rightMod = 0;
int leftSpeed = 0;
int rightSpeed = 0;

int speedModifier;
int diff;
int diffMag;

int leftDir = 0;
int rightDir = 1;
//-----------------------------

void setup(){
  Serial.begin (9600);
