Use of timers in swerve competition

Our robot club had a competition in October which involved avoiding an obstacle while line following:

An IR sensor would be a nice addition but I didn’t use hardware at all, just software. I added timers and a swerve() function to the PID linefollowing code. The swerve() code just goes right for a second then goes left, when it returns the main code starts following the line again. It worked pretty well, the video is at

along with other bots which used sensors to detect the obstacle.

I noticed that others have figured out how to add IR rangefinders so I’ll try that also. Here is some code similar to what I used:

// This function swerves around a can.
void swerve()
set_motors(160, -160);
set_motors(-160, 160);

int main()
unsigned long get_ms();
unsigned long millis();
unsigned long ckMillis = 0;

// This is the "main loop" - it will run forever.
	if (swerveOnce == 0)
		ckMillis = get_ms();
		if(ckMillis > 2200)
			set_motors(0, 0);


I saw your video a few weeks ago and thought it was very cool. Thank you for sharing it and your code with us!

We recently modified a few 3pis to race around the outside of an arbitrary irregularly shaped structure using Sharp IR sensors (for an event at the most recent LVBots competition). It was fairly easy to add sensors to 3pi’s available analog inputs, and they performed rather well. We will release a video of the event available at some point, and we plan on writing up a sample project that will hopefully make it easy for others to modify their 3pis in similar ways.

If you add sensors to your 3pi, we’d love to hear more about it!

- Ben

Thanks, I’ll keep an eye open for that project, sounds exactly like what I’d like to see!