I've been trying to create code that line tracks for only a specific amount of time. I've tried run_for, seconds(); and even get_motor_position_counter to time it. After pressing the B button, the robot would skip the line tracking and then go directly to show the "stop line tracking."
If there is a better way to do it, or if there is a way to correct the code below, please let me know!
void line_track();
int main()
{
printf("Press B button when ready\n");
while(b_button()==0) {} // wait for button press
double seconds();
while((seconds())< 5.00) // While the current time is less than 5 seconds
{
line_track();
}
all_stop();
printf("stop line tracking\n");
msleep(2000);
ao();
printf("done\n");
msleep(2000);
return 0;
}
void line_track()
{
int rport=7, leftmtr=0, rghtmtr=3; // identify port and motors
int threshold=512; // set threshold for light conditions for line tracking
int high=100, low=-10; // set "high" and "low" speeds
while(side_button()==0)
{ // stop if button is pressed
while (analog10(7) > threshold)
{ // continue until not dark
motor(leftmtr,high); motor(rghtmtr,low);
printf("on line\n"); // arc left
} // or button pressed
while (analog10(7) <= threshold)
{ // continue until dark
motor(leftmtr,low); motor(rghtmtr,high); //arc right
printf("white\n");
} // or button pressed
}
}
seconds() is a KIPR function that returns a float of the time since the Link was turned on. Thus, you need an initial time and that time+the desired time. Replace your main function with:
Note that I have not tested this code and it is prone to bugs. Also, I noticed that your code is prone to go up to 1 full loop after the 5 seconds if it gets in line_track() at the wrong time. You may want to look at that as well.
-Marty Rand
{
Senior programmer at Norman Advanced Robotics
Former senior programmer at Whittier Middle School
Youth Advisory Council
All around nerd
}