// Amount of time to let the robot run. #define RUNTIME 30.0 // Direction control flags. #define LEFT 0 #define RIGHT 1 // Motor power level definitions. #define HIGH 80 #define MID 60 #define LOW 40 int irRead[5] = {0,0,0,0,0}; // Array used to store the IR sensor readings. int hit = -1; // Variable used to indicate where a light source has been detected int oldHit = hit; int timeup = 0; // Flag used to signal when time has expired. // Variables used to store the process ID's of the three main processes. int timerPID; int lightLocatorPID; int controlMotorsPID; // The timer function waits for a specified length of time and then switches the // timeup flag. void timer( float t ){ float startTime = seconds(); while( seconds() - startTime < t ){ // While time has not expired. defer(); // Relinquish control of the processor. } timeup = 1; // Once time has expired, change the flag, and exit the function. } // timer( float t ) // The lightLocator function sets the variable hit to an integer representing // the IR sensor that is detecting the most IR light. // -1 indicates that no meaningful hit was detected. // 0 through 4 indicate which sensor has a hit. void lightLocator() { int i; // A counter index. int minRead; // The lowest IR reading. int maxRead; // The highest IR reading. int maxIndex; // The index (sensor number) of the highest IR reading. while(!timeup){ // Reset tracking variables. minRead = 256; maxRead = -1; maxIndex = -1; // Loop through each sensor to obtain the readings. for(i=0; i<=4; i++){ irRead[i] = 255 - analog(i); // **** if( irRead[i]maxRead ){ // **** maxIndex = i; maxRead = irRead[i]; } } // Process the readings to see if a meaningful hit was found. if( /* Enter a Boolean statement that recognizes a true hit. */ ){ hit = maxIndex; } else{ hit = -1; } // **** } } // lightLocator() // The controlMotors function controls the motors based on the value of "hit" // which is continuously updated by lightLocator(). void controlMotors() { while(!timeup){ // As long as time is left, continue updating the motor commands. if ( hit != oldHit){ // **** oldHit = hit; if(hit == -1){ // No hit; what should you do? defer(); } if(hit == 0){ // Turn left quickly defer(); } if(hit == 1){ // Turn left slowly defer(); } if(hit == 2){ // Go forward defer(); } if(hit == 3){ // Turn right slowly defer(); } if(hit == 4){ // Turn right quickly defer(); } } } // Once time has expired kill the engines and end the function. alloff(); } // controlMotors() // Main simply starts processes for the controlling functions above. void main() { while(1){ printf("Press start to chase light.\n"); while( !start_button() ){ } // **** printf("Chasing!!\n"); reset_system_time(); // **** timeup = 0; // **** timerPID = start_process( timer( RUNTIME ) ); lightLocatorPID = start_process( lightLocator() ); controlMotorsPID = start_process( controlMotors() ); while(!timeup && !stop_button() ){ // **** // Add debugging commands here as needed. defer(); } // **** kill_process(controlMotorsPID); kill_process(lightLocatorPID); kill_process(timerPID); alloff(); beep(); } } // main()