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.

1 Like

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.

4 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:

1 Like

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?

2 Likes

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);
    }
}
6 Likes

I have a question. Do you need to use pointer to call a void function for thread? This is my code. vex::thread t1(void *Odometry); I’m not sure if this is correct.

You don’t NEED to, at least I don’t think (looking at jpearmans code) but tbh if you’re worried you can just make it an integer and have a pointer to that. It’s still gonna execute so

I tried his code. It doesn’t seem right.
Odometry is a void function. Is it ok to put it in the thread? Or do I need to use int function?

Why not try it first? It’s not difficult to change, you just need to change the type and add a return value (just do 2 as the value)

Plus it does say that void pointer is inconvertible to int pointer so I’m thinking I’m correct.

This is a two year old topic, you should probably have started a new one.

What exactly are you trying to do, what does the Odometry function look like ?

4 Likes

I am trying to use thread to do multitasking.

void Odometry(){      
 
 while(Competition.isAutonomous()){
    double encdChangeL = encdL - prevEncdL;
    double encdChangeR = encdR - prevEncdR;
     double sumEncdChange = encdChangeL + encdChangeR; 
deltaAngle = (encdChangeL - encdChangeR) / baseWidth;
double encdChangeS = encdS - prevEncdS;
shiftS = encdChangeS;
angle += deltaAngle;

double halfSumEncdChange = sumEncdChange / 2;
if(deltaAngle==0)
{
  position = position + Node(halfSumEncdChange * sin(angle), halfSumEncdChange * cos(angle));
} 
else if(deltaAngle!=0)
{
  shiftS = (encdChangeS/deltaAngle) * sin(deltaAngle/2)*2;
}
double xShift = shiftS * cos(prevAngle+ (deltaAngle/2));
double yShift = shiftS * -sin(prevAngle + (deltaAngle/2)); 
position = position + Node(position.getX()+xShift, position.getY()+yShift);
prevEncdL = encdL;
prevEncdR = encdR;
prevEncdS = encdS; 
wait(5,msec);
}
}

ok, so at the point you try and start the thread in pre_auton, has Odometry already be defined ? ie. is the Odometry function in the same file and either declared or defined before pre_auto ?

3 Likes