/*-----------------------------------------------------------------------------*/
void
iqRunDisplayHome( bool showExit = false )
{
    static  long  nPgmStartTime = nPgmTime;

    // display static text
    displayStringAt(  1, LCD_ROW1, "Running" );

    // Calculate robot running time
    long secs  = (nPgmTime - nPgmStartTime)/1000;
    int  mins  = secs / 60;
    int  hours = mins / 60;

    // display program running time
    displayStringAt(  80, LCD_ROW1, "%02d:%02d:%02d", hours, mins % 60, secs % 60 );

    // run/stop/exit select box
    if( showExit )
      iqDrawSelectButtonAndText( 0, 1, "Exit");
    else
    if( robotEnabled )
      iqDrawSelectButtonAndText( 0, 1, "Stop");
    else
      iqDrawSelectButtonAndText( 0, 1, "Run ");
}
//Displays distance to light source **Only when driving towards light**
task distanceDisplay(){
	SensorType[S2] = sensorEV3_Ultrasonic;
	float distance;

	while(true){
			distance = SensorValue(S2);
		//displays light distance
		if(BrightLight >= 4 && BrightLight <= 6){
			displayStringAt(70, 90, "Light Detected!");//Display
			displayStringAt(70, 60, "Light Dist: %.2f",distance);//Display
		}
		//Erases display
		else{
			displayStringAt(70, 90, "                         ");//Display
			displayStringAt(70, 60, "                         ");//Display
		}
		wait1Msec(100);
	}
}
/*-----------------------------------------------------------------------------*/
void
iqDrawSelectButtonAndText( int xpos, int ypos, char *text )
{
    drawLine( xpos+ 1, ypos,   xpos+ 9, ypos   );
    drawLine( xpos+ 1, ypos+6, xpos+ 9, ypos+6 );
    drawLine( xpos   , ypos+1, xpos   , ypos+5 );
    drawLine( xpos+10, ypos+1, xpos+10, ypos+5 );
    drawLine( xpos+ 2, ypos+3, xpos+ 4, ypos+1 );
    drawLine( xpos+ 4, ypos+1, xpos+ 8, ypos+5 );
    displayStringAt( xpos+12, ypos+6, text );
}
/*-----------------------------------------------------------------------------*/
void
iqDisplayLine( int row, bool select, char *str )
{
    if( row < 0 || row > 43 )
        return;

    if( select ) {
        fillRect( 0, row+1, 127, row-8);
        displayInverseStringAt( 1, row, str );
    }
    else {
        eraseRect( 0, row+1, 127, row-8);
        displayStringAt( 1, row, str);
    }
}
//Follow brightest light direction
void lightFollow(){
//Boleans for storing previous rotation direction
bool wasRotatingR, wasRotatingL;
//Displays brightest direction
displayStringAt(90, 120, "Light Dir = %d",BrightLight);//Display
//Display sound level label
displayStringAt(5, 120, "Sound Level");//Display

		//rotate Left
	if(BrightLight < 4 && BrightLight > 0){
			motor[right] = 10;
			motor[left] = -10;
			//Find direction of rotation
			wasRotatingR = false;
			wasRotatingL = true;
		}
		//Rotate Right
		else if(BrightLight > 6){

				motor[right] = -10;
				motor[left] = 10;
				//Find direction of rotation
				wasRotatingR = true;
				wasRotatingL = false;
		}
		//Drive forward while slightly turning right
		else if (BrightLight == 4){
			//If robot was rotating than stop, so robot doesn't pass light direction
			if(wasRotatingR || wasRotatingL){
			//stop motors
				motor[right] = 0;
				motor[left] = 0;
				wait1Msec(500);
		}
			//set right slightly faster than left for slow tilt
			motor[right] = 30;
			motor[left] = 28;
			wasRotatingR = false;
			wasRotatingL = false;
		}
		//Drive forward
		else if (BrightLight == 5){
			//If robot was rotating than stop, so robot doesn't pass light direction
			if(wasRotatingR || wasRotatingL){
				//stop motors
				motor[right] = 0;
				motor[left] = 0;
				wait1Msec(500);
		//play sound when directly following light
			setSoundVolume(20);
		//playSound
			playSound(soundFastUpwardTones);
		}
			motor[right] = 30;
			motor[left] = 30;
			//Set both rotation values to false: robot is driving forward
			wasRotatingR = false;
			wasRotatingL = false;
		}
		//Drive forward while slightly turning left
		else if (BrightLight == 6 ){
			//If robot was rotating than stop, so robot doesn't pass light direction
			if(wasRotatingR || wasRotatingL){
				motor[right] = 0;
				motor[left] = 0;
				wait1Msec(500);
		}
			motor[right] = 28;
			motor[left] = 30;
			//Set both rotation values to false: robot is driving forward
			wasRotatingR = false;
			wasRotatingL = false;
		}
		//When no brightest light direction is detected, continue rotating in last
		//rotation direction, or rotate left if robot wasn't previously rotating
		else{
			if(wasRotatingR){
				motor[right] = -10;
				motor[left] = 10;
			}
			else if(wasRotatingL){
				motor[right] = 10;
				motor[left] = -10;
			}
			else{
				motor[right] = 10;
				motor[left] = -10;
				wasRotatingL = true;
			}
		}//end else

}//end light follow