VexCode Sleep

Some examples have “Wait 2 seconds or 2000 milliseconds before starting the program.
vex::task::sleep( 2000 );”
What is the purpose of this?

If this is the same thing as python, sleep works the same as wait. Sleep 2000 should be wait 2 seconds if vexcode counts by milliseconds. I’m not too good at coding so take this with a grain of salt

Perhaps it is to give you time to place the robot on the field after you click “run”?

This is purely speculation, I’m not really sure.

I mean, or you could just click run after you put the robot on the field. But you never know…

vex::task::sleep takes in milliseconds and your code will stop for that amount of time, but I am not sure why you would want to put this in the beginning of your code.

The purpose of a sleep function is to allow the processor do other things like run another task and also to wait a certain amount of time. This is the reason why you would find a sleep in a while(true) loop.

Something else to consider is that V5 has multiple cpu’s and stuff like sensor readings and communication with motors is handled regardless of what the user cpu is doing. So unless you have multiple task you technical don’t need a sleep in your while(true) loop, though you can just put it in to not have problems later. If you do have multiple task sleeping is very important to let them all run since the CPU can only run one thing at a time. Calling sleep the CPU will pause your task and yield CPU it other waiting task. Although calling any system call (like getting motor speed) should also cause other tasks to run, but it might change in the future.
Also this post has some stuff about how v5 architecture works…


I haven’t seen such examples, but there used to be a bug feature when values of sensors, connected to legacy 3-wire ports (analog and digital), were returning 0 if you try to read them right after V5 startup.

So you would need to delay reading those sensors for a short period of time to let it properly initialize.

More details are in this topic:


Usually you put sleep 20 ms at the end of your while loop, so you don’t waist resources, motors can only update their speed so fast

I use sleep vex::task::sleep between tasks where my robot needs to allow the blocks to stabilize themselves. For example, I will use a very brief sleep between picking up blocks and moving so the the blocks can settle on the robot. Otherwise sometimes they fall off and mess up the whole autonomous code.

1 Like

Can’t you just replace the (sleep for 6 sec.) at the beginning to (wait 0.5 sec.)
If you are using brain inertial it would make it more efficient, right?