Robot Drive for 10 seconds

unsigned int timer = 0;

//Forward for 10 seconds
if(timer < 382){
pwm01 = pwm02 = 225;
timer++;
}

since the autonomus loops every 26.2ms
10 / 0.0262 = ~382

would this work to drive the robot forward for 10s/

thanks

I am not sure about the 382, but other than that it looks good. The only thing that I see is that on most robots the motors are faced different directions, so if that is your case, you just made your robot spin in a circle for 10 seconds.:ahh: Something to look out for.
Joey

We’re not sure how we did this, that code is right for drive straight

we first tried
pwm01 = 200;
pwm02 = 54;

and it just spun

Have you got your IR board working yet, personally I think that having forward and stop functions mapped to two buttons is a better solution to driving forward a said time or distance then a solution like this. However this is a good start in the right direction for autonomous. If you need help with hybrid coding for the IR give a shout.

I’d suggest stopping the motors after the 10 seconds are up.:slight_smile:

I’d also suggest slowing the motors down before you stop them, also speeding them up instead of just going from 127 to 255. If you don’t do this and your robot is top heavy then you run the risk of tipping over. You can do this by modifying your counter slightly.

Sudocode:
pwm_2 = pwm_1;
static int counter = 0;

counter++;
if (counter <= 10)
{
pwm_1 = 137;
}
else if (counter > 10 && counter <= 20)
{
pwm_1 = 157;
}

etc…

We did this one year and it seemed to work pretty well. There might be a better way to do it, but this was simple and we wrote it in about 10 minutes.

If you have access to encoders, you may want to have your robot drive a certain distance rather than for a certain length of time.

That way, you could write a function like Drive(distance, speed);

They most likely reversed the polarity of one motor when they connected it to a Victor. This has been a common practice for 364 for a number of years, so in fact, setting both motors to 254 will make it drive forward.

One other thing to note is that your counter will need to be static.