   leftDist = sensor2.getDist();
   perRightDist = sideright.getDist();
   perLeftDist = sideleft.getDist();
-  initTime = millis();
+  prevLine = millis();
+  prevIR = prevLine;
   setSpeed(128, 20);
 }
 
 void loop() {
-  // Get distance from sensor
-  cur = millis();
-    //print out values from sensors and motors to the console
-    Serial.print(past);
-    Serial.print(", ");
-    Serial.print(rightDist);
-    Serial.print(", ");
-    Serial.print(leftDist);
-    Serial.print(", ");
-    Serial.print(perRightDist);
-    Serial.print(", ");
-    Serial.print(perLeftDist);
-    Serial.print(", ");
-    Serial.print(analogRead(backleft));
-    Serial.print(", ");
-    Serial.print(analogRead(backright));
-    Serial.print(", ");
-    Serial.print(analogRead(frontleft));
-    Serial.print(", ");
-    Serial.println(analogRead(frontright));
-  if ((cur - initTime) % 5 == 0) {
+  curLine = millis();
+  curIR = curLine;
+  
+  //print out values from sensors and motors to the console
+  Serial.print(past);
+  Serial.print(", ");
+  Serial.print(rightDist);
+  Serial.print(", ");
+  Serial.print(leftDist);
+  Serial.print(", ");
+  Serial.print(perRightDist);
+  Serial.print(", ");
+  Serial.print(perLeftDist);
+  Serial.print(", ");
+  Serial.print(analogRead(backleft));
+  Serial.print(", ");
+  Serial.print(analogRead(backright));
+  Serial.print(", ");
+  Serial.print(analogRead(frontleft));
+  Serial.print(", ");
+  Serial.println(analogRead(frontright));
+  Serial.print(", ");
+  Serial.println(curLine - prevLine);
+    
+  if ((curLine - prevLine) >= 5) {
+    prevLine = curLine;
     if (analogRead(backleft) < threshold) { //first read from line sensors before doing anything else
       //if line detected by back-left line sensor, turn left a bit
       move(1, 40, 1); //motor 1, full speed, left

        right();
        past = 'r'; //set past movement to right
        count++; //increment abandon search count
      } else if (rightDist <= 600 || perRightDist <= 600) {
        //if there is an object beyond 600mm of the left sensors (i.e. there is something closer in the right sensors), move right
        right();
        past = 'r'; //set past movement to right
        count = 0; //reset abandon search count because the robot has found an object
      } else if (leftDist <= 600 || perLeftDist <= 600) {
        //if there is an object beyond 600mm of the right sensors (i.e. there is something closer in the left sensors), move left
        left();
        past = 'l'; //set past movement to left
        count = 0; //reset abandon search count because the robot has found an object

char past; //what direction the robot was moving previously
int count; //counter for how long before the rebot gives up searching in one direction
int threshold; //threshold for line sensors
int curIR; // current time
int prevIR; 
int curLine; 
int prevLine; 
short iter;
int maxS;
int minS;

int near = 200;
int far = 500;

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