Delay

is there a code for a delay command for c?

You could start up a counter and use the counter to tell the robot to do certain things at a certain time (remember the code loops every 26. something milliseconds).

Unfortunately, as far as I know, there is no delay function.

There are timer interrupts (which, sorry, I’m unsure how to use). But for most applications you rarely need any more resolution than 26.2ms. It’s pretty much constant from loop to loop.

Wait(x);

x being the number of milliseconds. Thus, if you want the code to pause for 3 seconds, you type “Wait(3000);”

Where did you find reference to the “Wait” command? Did you find it in a manual. I’m trying to find a reference to all these kinds of functions?

I believe Wait(n) is in EasyC, not in mplab. The two operate much differently in this respect.

In mplab you don’t want a command like wait 3 seconds inside your code. The code needs to do it’s own paperwork every loop. You can use a counter variable like i_am_samus suggests. Here’s my note to myself in the code:

//  timer notes:
//
//  The autonomous code functions are called by the controller 
//  every 26.2 mS (milliseconds), or 38.17 times each second.
//  For calculations we will use 38 times per second.
//  The total length of Autonomous Mode is 15 seconds, 
//  or  570 counts total (or exactly 572.55 counts).

(I’m willing to be corrected if this isn’t right.)

As I recall from other places here at CD, the counter variable method isn’t exact but is close enough.

If you’re using MPLAB and last year’s code, you should have a “delay.h” file (partially copied here). If you’re using this year’s code, some of these functions appear to be
called, but from an unused code block, and the file “delay.h” is gone. Perhaps Kevin can shed more light on this?


/* PIC 17Cxxx and 18Cxxx cycle-count delay routines.
 *
 *   Functions:
 *               Delay1TCY()
 *               Delay10TCY()  // 17Cxx only
 *               Delay10TCYx()
 *               Delay100TCYx()
 *               Delay1KTCYx()
 *               Delay10KTCYx()
 */

You can setup one of the timers and increment an integer in the interrupt routine (more accurate and adjustable period) or increment a counter variable in the default functions (fixed period but easier to implement and only works when OI is talking to the robot). Spinning a loop to get a delay is a bad idea in this application. The “paperwork” referred to in this post is the processing of messages to and from the operator interface controller. If you start missing those messages or not replying in a timely manner, the master game controller will disable your robot for the duration of the match.

Update: A simple spin to create usec type delays is OK but don’t spin for a long time relative to the cycle of the main loop (~23ms).

There are several ways to implement delays in software for FRC robots. The best advice for how to do it in your case will depend on why you want a delay. What do you intend to use it for? The more specific you can be, the more helpful an answer you will get.