Multitasking 2 Variables

I have 2 variables that I use and I want to know how to make them both run at the same time when a button is pressed. How would I do that with the code I have?

vexcodeInit();
  inertial1.calibrate();
  Brain.Screen.clearScreen(vex::white);
  Brain.Screen.setPenColor(vex::black);
  lcdButton AutonRed(300, 100, 150, 100,"Red Auton", "#FF2525");
  wait(20, msec);
  lcdButton AutonBlue(150, 100, 150, 100, "Blue Auton", "#2546ff");
  wait(20, msec);
  lcdButton AutonSkills(235, 200, 150, 100, "Skills Auton", "#00ff33");
  int AutonRedCooldown = 0;
  int AutonBlueCooldown = 0;
  int AutonSkillsCooldown = 0;
  while(1){
    if (AutonRed.pressing() && AutonRedCooldown < 1){
      Brain.Screen.setFont(monoL);
      Brain.Screen.clearScreen(red);
      Brain.Screen.setFillColor(red);
      Controller1.Screen.print("Red Auton Enabled");
      AutonSelect = 1;
      AutonRedCooldown = 100;
      Brain.Screen.print("Red");
      wait(200, msec);
      Brain.Screen.print("Red");
      wait(200, msec);
      Brain.Screen.print("Red");
      wait(200, msec);
      Brain.Screen.print("Red");
      wait(200, msec);
      Brain.Screen.print("Red");
      wait(200, msec);
      Brain.Screen.print("Red");
      wait(200, msec);
      Brain.Screen.print("Red");
      wait(200, msec);
      Brain.Screen.print("Red");
      wait(200, msec);
      Brain.Screen.print("Red");
      wait(200, msec);
      Brain.Screen.print("Red");
      wait(200, msec);
      Brain.Screen.print("Red");
      wait(200, msec);
      repeat(999999999){
        RedScroll();
        BScroll();
      }
    }
    else if (AutonBlue.pressing() && AutonBlueCooldown < 1){
      Brain.Screen.setFont(monoL);
      Brain.Screen.clearScreen(blue);
      Brain.Screen.setFillColor(blue);
      Controller1.Screen.print("Blue Auton Enabled");
      AutonSelect = 2;
      AutonBlueCooldown = 100;
      Brain.Screen.print("Blue");
      wait(200, msec);
      Brain.Screen.print("Blue");
      wait(200, msec);
      Brain.Screen.print("Blue");
      wait(200, msec);
      Brain.Screen.print("Blue");
      wait(200, msec);
      Brain.Screen.print("Blue");
      wait(200, msec);
      Brain.Screen.print("Blue");
      wait(200, msec);
      Brain.Screen.print("Blue");
      wait(200, msec);
      Brain.Screen.print("Blue");
      repeat(999999999){
        BlueScroll();
        BScroll();
      }
    }
    else if (AutonSkills.pressing() && AutonSkillsCooldown < 1){
      Brain.Screen.setFont(monoL);
      Brain.Screen.clearScreen("#00ff33");
      Brain.Screen.setFillColor("#00ff33");
      Controller1.Screen.print("Skills Auton Enabled");
      AutonSelect = 3;
      AutonSkillsCooldown = 100;
      Brain.Screen.print("Skills");
      wait(200, msec);
      Brain.Screen.print("Skills");
      wait(200, msec);
      Brain.Screen.print("Skills");
      wait(200, msec);
      Brain.Screen.print("Skills");
      wait(200, msec);
      Brain.Screen.print("Skills");
      wait(200, msec);
      Brain.Screen.print("Skills");
      repeat(999999999){
        SkillsScroll();
        BScroll();
      }
    }
    task::sleep(10);
    AutonRedCooldown--;
    AutonBlueCooldown--;
    AutonSkillsCooldown--;
  }
  
}

I basically want for an example BScroll and RedScroll running at the same time with a minor delay with each one.

This isn’t an answer, just a general thing. Use a for loop t repeat the printing to clear up your code. Here’s some pseudo code: (sorry for the syntax, I usually use C#)
for(int i = 0; i < 10; i++)
{
Brain.Screen.print("thing");
wait(200, msec);
}

Variables don’t run. If you jump to a variable, you’re running the stack as code, and thats probably not a great idea.

yes but can I have those running at the same time?(I’m sorry if I’m appearing as rude. It’s just I’ve been looking for an answer for like 3 hrs)

Let me clear up what I mean to do. Let me use the variables BScroll and BlueScroll as an example.
I want when BlueScroll starts to run it’s code, BScroll’s code to start running at the same time. How will I do that?

You don’t want variables to run, you want functions to execute concurrently. You can pass a function (that takes no arguments) as an argument for creating a task. The Brain will run the two tasks under the illusion they are running at the same time.

2 Likes

My VEXCode PID tutorial shows how to multitask. It should be on the VEXForum wiki if you don’t know where to find it :slight_smile:

Let’s back up to the start. The code looks like an autonomous selector. Then depending on what is selected you are calling different functions like BlueScroll(); Sound right?

To make sure we are using the same terminology. A variable is a container that holds something and a function is section of code that you want to run. I think you are trying to call two functions() at the same time. Correct?

Will you share your BlueScroll(), BScroll(), and RedScroll() functions with us?

You are right on how they are auton buttons but they are not the actual buttons. They are graphics that happen after the button had been pressed. I will upload the code for all those functions once I get home.

@Hudsonville_Robotics
Function BScroll

#include "vex.h"

void BScroll(){
  Brain.Screen.setCursor(10, 1);
        Brain.Screen.print("");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("6");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("61");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("610");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("6105");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("  6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("    6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("      6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("        6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("          6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("            6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("              6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                  6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                    6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                      6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                        6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                          6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                            6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                              6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                                6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                                  6105B");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                                    6105B");
        Brain.Screen.clearLine();
}

RedScroll

#include "vex.h"



void RedScroll(){
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("Red");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("Red Auton");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("Red Auton is");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("Red Auton is now");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("  Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("    Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("      Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("        Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("          Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("            Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("              Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("                Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("                  Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("                    Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("                      Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("                        Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("                          Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("                            Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("                              Red Auton is now ready!");
wait(200, msec);
Brain.Screen.clearLine();
Brain.Screen.print("                                Red Auton is now ready!");
}

BlueScroll

#include "vex.h"

void BlueScroll(){
  Brain.Screen.print("");
        Brain.Screen.setCursor(1, 1);
        Brain.Screen.clearLine();
        Brain.Screen.print("Blue");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("Blue Auton");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("Blue Auton is");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("Blue Auton is now");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("  Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("    Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("      Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("        Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("          Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("            Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("              Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                  Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                    Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                      Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                        Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                          Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                            Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                              Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                                Blue Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
}

And there is another
SkillsScroll

#include "vex.h"

void SkillsScroll(){
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("");
        Brain.Screen.clearLine();
        Brain.Screen.print("Skills");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("Skills Auton");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("Skills Auton is");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("Skills Auton is now");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("  Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("    Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("      Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("        Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("          Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("            Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("              Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                  Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                    Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                      Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                        Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                          Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                            Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                              Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
        Brain.Screen.print("                                Skills Auton is now ready!");
        wait(200, msec);
        Brain.Screen.clearLine();
}

Also sorry for such a late reply. It was a very busy weekend.

There are many opportunities to optimize your code. Let me review it.

1 Like

I’m not going to exactly rewrite the code for you, but here’s some food for thought.

void
scrollingText_1() {
    int xpos = -300;
    int ypos = 20;

    while(1) {
      Brain.Screen.printAt( xpos, ypos, "  Skills Auton is now ready!   ");
      xpos += 5;
      if( xpos > 480 )
        xpos = -300;

      this_thread::sleep_for(50);
    }
}

void
scrollingText_2() {
    int xpos = 480;
    int ypos = 50;

    while(1) {
      Brain.Screen.printAt( xpos, ypos, "  Blue Auton is now ready!   ");
      xpos -= 5;
      if( xpos < -300 )
        xpos = 480;

      this_thread::sleep_for(50);
    }
}

int main() {
    int count = 0;
   
    vex::thread  t1(scrollingText_1);
    vex::thread  t2(scrollingText_2);

    while(1) {
        // Allow other tasks to run
        this_thread::sleep_for(10);
    }
}
4 Likes