How to Run Something in a Loop every few Seconds

In VEXcode Pro V5, I know that the program is running a loop in robot-config.cpp which takes in the controller input, but it requires me to turn on robot expert mode, which means it won’t auto-generate the code for me anymore.

How would you run a loop side by side with the controller one in the background?
My purpose currently for the loop is to print the robot temps to the Brain screen.

I’m wondering whether this would be possible in C++.

I think you can just put something like “Brain.Screen.print(motorname.temp());” in your driver control loop. The formatting is probably wrong, but it’s around that ballpark. Don’t forget to do “Brain.Screen.Newline();” too.


Yeah, just do something in your driver control function like

while (true) {
  Brain.setCursor(1, 1);
  vex::task::sleep(2000); // At least in IQ, this function takes milliseconds

I don’t know any of the real functions, so you’ll have to find them in the API. That is to say, this is pseudocode.


I didn’t even know that there was a Newline function on the Brain! I’ve just been using Brain.Screen.setCursor(row, column) and then Brain.Screen.print().

I know what the functions are, I just didn’t know how to loop them.

But will that stop all running code, since it is a while loop?
I’m doing VRC by the way, not VIQ.

Check out how tasks / threads / multitasking works on the VEXCode api. It is definitely possible but requires a little more programming knowledge.


No. While loops do not stop code from running any more than drive forward commands. Are you ever surprised that the brain can drive forward while also getting input from your controller? What happens is you have two groups of code running simultaneously. On one hand, you have your controller input code, which VEX generates, which allows you to drive your robot with the controller. On the other hand, you have your driver control function, which ecutes commands in the order it receives them. So when you have code like this:

drive.forward(12, inches);
drive.turnLeft(90, degrees);

Are you surprised when it drives forward and then turns left? So when you have code like this:

while (gyro.heading < 90) {
  drive.turnLeft(5, degrees);
  wait(20, msec);
brain.print("Finished turning!");

Are you surprised when it waits until the while loop ends, I.e the condition is no longer true, to execute the commands outside of and after the while loop? If that’s what you mean “stop the code from running”, yes it will do that, but I don’t think that’s a problem. What code specifically are you concerned will not run, and could you please paste it in here?


This is the code that I want to run in the while loop with a wait time of about 3 seconds:

  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Right Motor 1: ");
  Brain.Screen.print("%f", rightMotorA.temperature(temperatureUnits::celsius));
  Brain.Screen.setCursor(2, 1);
  Brain.Screen.print("Right Motor 2: ");
  Brain.Screen.print("%f", rightMotorB.temperature(temperatureUnits::celsius));
  Brain.Screen.setCursor(3, 1);
  Brain.Screen.print("Left Motor 1: ");
  Brain.Screen.print("%f", leftMotorA.temperature(temperatureUnits::celsius));
  Brain.Screen.setCursor(4, 1);
  Brain.Screen.print("Left Motor 2: ");
  Brain.Screen.print("%f", leftMotorB.temperature(temperatureUnits::celsius));
  Brain.Screen.setCursor(5, 1);
  Brain.Screen.print("Rollers: ");
  Brain.Screen.print("%f", Rollers.temperature(temperatureUnits::celsius));
  Brain.Screen.setCursor(6, 1);
  Brain.Screen.print("Lift: ");
  Brain.Screen.print("%f", Lift.temperature(temperatureUnits::celsius));
  Brain.Screen.setCursor(7, 1);
  Brain.Screen.print("Arm: ");
  Brain.Screen.print("%f", Arm.temperature(temperatureUnits::celsius));
  Brain.Screen.setCursor(8, 1);
  Brain.Screen.print("Claw: ");
  Brain.Screen.print("%f", Claw.temperature(temperatureUnits::celsius));

I’m just scared of it stopping autonomous or driver control, but if it doesn’t do this, then I should be fine.

Three final notes:

If the condition in your while loop parentheses is true, the condition will always evaluate to true, the code after the while loop will never run. The exception is if you put the loop inside your driver_control() function, the field control will end the function at the end of driver control and the loop will therefore cut out. Any code in the driver_control() function and after the while (true) loop will never run.

Remember to clear your screen at the beginning of every iteration of the loop, or your text will overwrite itself or something like that.

Remember to have a wait in every iteration of the loop. I think vex::task::sleep(3000); will work. If it does, the input parameter is in milliseconds.

1 Like

It is good to note that some printing/outputting commands are yielding
For instance, telling your controller to vibrate may cause the code to halt for roughly 100 milliseconds until the controller finishes vibrating. This may or may not be the case for the other commands, but it is always best to be safe when it comes to possible yielding commands. You should consider multithreading instead:

int print_to_brain_screen_consecutively(){
//do other stuff
return 1;

int main(){
vex::task brainScreen(print_to_brain_screen_consecutively);

//do stuff

return 1;

Why did you put a loop in your robot configuration file? No no no, put it in your main.cpp file.


My thread skills are rusty but you could run the temp monitor in a separate thread so that it is not affected by the Auton or Driver Control methods. In Blocks you would simply use another When Started primary block but in VexCodePro it takes a bit more.

//Declare a separate thread for temp monitoring
vex::thread TempMonitorThread;

//My demo motor
motor motor1 = motor(PORT1,ratio18_1, false);

// Create your temp monitoring function with a while loop and a wait delay 
void MonitorMotorTemp(void){
    Brain.Screen.printAt(10,10, "L1 Temp: %d   ", motor1.temperature(temperatureUnits::celsius));
    wait(1, sec); // <- Important: At least 20 ms but 1 second is fine.

//Boilerplate competition functions...
void pre_auton(void) {

void autonomous(void) {
  motor1.spinTo(90, deg);

void usercontrol(void) {
  while (1) {
    //Driver control 
    wait(20, msec); 

int main() {
  // Initialize the temp monitoring thread in the main function
  // which will run separate from the Auton and Driver control
  TempMonitorThread = thread(MonitorMotorTemp);



  while (true) {
    wait(100, msec);

Definitely a couple of solutions here…
Connor had a good example of exactly what I would do if I needed another loop, and I do that in my code for a few things. However, you don’t need an additional loop at all for printing motor temperatures. Just drop that print code you pasted in one of your replies into your driver loop, and it will have zero impact on your loop. That should work. Go ahead and test it even if you’re worried about it not working, won’t break anything.
You mentioned a 3 second wait time… I’m not sure whats thats for but I don’t think you need it. Just drop in your print commands into driver control. The commands you have are instant and won’t cause any problems, and they don’t need a wait time. I think you’re over complicating it.


Yeah definitely a good idea to multithread, although the print command’s don’t take any time, so for this purpose they should be fine to drop their print code in the driver loop. May be worth noting it won’t update in auton if it’s in the driver loop, but that’s not so important.

and yeah… don’t put that stuff in robot-config.cpp xD

You need it if you’re putting it in a separate loop because without it the loop will run a zillion times a second and your program will time out. It doesn’t have to be three seconds, but it’s not important enough to shrink the wait time to 20 msec, or anything. You won’t miss much in three seconds.

1 Like

I will try this out! Thank you!

1 Like