Need Help With Using "seconds();"

1 reply [Last post]
Title: NooBot
Joined: 04/22/2013
BotPoints: 3
User offline. Last seen 10 years 5 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

printf("stop line tracking\n");

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

{ // 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
} // or button pressed



Marty Rand
Marty Rand's picture
Title: Botball Youth Advisory Council
Joined: 07/04/2009
BotPoints: 253
User offline. Last seen 8 years 41 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");
  5. while(b_button()==0) {} // wait for button press
  7. float init_time=seconds();
  9. while(seconds()< 5.00+init_time) // While the current time is less than 5 seconds
  10. {
  11. line_track();
  12. }
  14. all_stop();
  15. printf("stop line tracking\n");
  16. msleep(2000);
  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