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…

1 Like

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:

1 Like