Need Help With Using "seconds();"

1 reply [Last post]
Sabertron
Title: NooBot
Joined: 04/22/2013
Posts:
BotPoints: 3
User offline. Last seen 9 years 23 weeks ago.

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

}

}

Marty Rand
Marty Rand's picture
Title: Botball Youth Advisory Council
Joined: 07/04/2009
Posts:
BotPoints: 253
User offline. Last seen 8 years 8 weeks ago.

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:

  1. int main()
  2. {
  3. printf("Press B button when ready\n");
  4.  
  5. while(b_button()==0) {} // wait for button press
  6.  
  7. float init_time=seconds();
  8.  
  9. while(seconds()< 5.00+init_time) // While the current time is less than 5 seconds
  10. {
  11. line_track();
  12. }
  13.  
  14. all_stop();
  15. printf("stop line tracking\n");
  16. msleep(2000);
  17.  
  18. ao();
  19. printf("done\n");
  20. msleep(2000);
  21. return 0;
  22. }

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
}