Controller Priority During a Wait Function

I’m a high school teacher working on a project using VEX V5 Components.
Students are making a “Mario Kart” game where two people race two small karts head to head. The project has to use sensors, so the students put Optical sensors on the karts that can interact with the track. Like Mario Kart, they want to have a spot for a speed boost, and a spot to slow down the kart to give the other kart an advantage. We were using Drivetrain controls, but that didn’t work at all, so we switched to using Controller.Axis.Position. For testing, we are using pretty drastic slowdowns and speed increases, but plan on the standard drive speed to be 75%, Speed Boost at 100%, and the Slow Down at 50%. Right now standard speed is 50%, slow down is 10%, speed boost is 100%. The following code works, but only when the kart is on a certain color, and it changes the moment it drives forwards off of it.
(Forgive the formatting, this is my first time posting)

while (true) {
    
  
  if (Optical12.hue() > 0 && Optical12.hue()<46) { //Sees Yellow
    Motor5.spin(forward, Controller1.Axis2.position()*.1, (percent));
    Motor6.spin(forward, Controller1.Axis3.position()*.1, (percent));
   
  }
  if (Optical12.hue()>60 && Optical12.hue()<70) { //Sees Green
    Motor5.spin(forward, Controller1.Axis2.position(), percent);
    Motor6.spin(forward, Controller1.Axis3.position(), percent);
    
  }
  else {
    Motor5.spin(forward, Controller1.Axis2.position()*.5, (percent));
    Motor6.spin(forward, Controller1.Axis3.position()*.5, (percent));
  }
}

Now the issue comes in. We don’t want the speed boost, nor the slow down, to be only while driving over that color. If a robot drives over a yellow piece of paper, we want it to slow down for 5 seconds while still being fully controllable. We added a wait function so the code looks as follows:

while (true) {
    
  
  if (Optical12.hue() > 0 && Optical12.hue()<46) { //Sees Yellow
    Motor5.spin(forward, Controller1.Axis2.position()*.1, (percent));
    Motor6.spin(forward, Controller1.Axis3.position()*.1, (percent));
    wait(5,seconds);
  }
  if (Optical12.hue()>60 && Optical12.hue()<70) { //Sees Green
    Motor5.spin(forward, Controller1.Axis2.position(), percent);
    Motor6.spin(forward, Controller1.Axis3.position(), percent);
    wait(5,seconds);
  }
  else {
    Motor5.spin(forward, Controller1.Axis2.position()*.5, (percent));
    Motor6.spin(forward, Controller1.Axis3.position()*.5, (percent));
  }
}

SO THE ISSUE!
The brain stores whatever the axis position was when the robot saw the color, modifies it accordingly, and holds that for 5 seconds. So if the robot is driving straight at 50% and senses green, it speeds up to 100%, but cannot be controlled, so it just drives straight at full speed for 5 seconds, not allowing the driver to turn or slow down if they choose.

We’ve been messing around with a for loop that counts to 5 seconds, and also using a float called speedmod that changes in the code and is used to modify the position, but no luck.
The kart either doesn’t maintain the newly set speed once it drives off the color, or holds on to whatever the input of the controller axis was and modifies it for 5 seconds.

Any help would be greatly appreciated!! Thank You!!

edit: code tags added by mods, please remember to use them.

2 Likes

This code can be achieved through the use of a timer.

//set up variables
timer boostTimer;
float speedModifiier = 0.5;

while (true) {
  if (Optical12.hue() > 0 && Optical12.hue()<46) { //Sees Yellow
    //set speed modifier to 0.1 to slow all driver inputs
    speedModifier = 0.1;
    //reset timer to make sure the effect lasts 5 seconds
    boostTimer.clear();
  }

  if (Optical12.hue()>60 && Optical12.hue()<70) { //Sees Green
    //set speed modifier to 1 to make all driver inputs faster
    speedModifier = 1;
    //clear timer to make it last 5 seconds
    boostTimer.clear();
  }

 //When 5 seconds have passed, we reset the speedModifier value back to normal
  if(booseTimer.value()>5){ // .value() gets the current time in seconds 
      speedModifier = 0.5;
  }

  //Drive the robot, this is called every update loop so that the drivers always have control
  Motor5.spin(forward, Controller1.Axis2.position()*speedModifier, (percent));
  Motor6.spin(forward, Controller1.Axis3.position()*speedModifier, (percent));
  wait(10,msec);

}

The code uses a speedModifier variable, which is multiplied by the joystick inputs to scale them. When the robot drives over a color, the timer is reset and the speed modifier is changed. When 5 seconds have passed, the value of speedModifier is set back to its default. This code was written inside vex forum and may have some minor syntax or formatting issues as a result.

3 Likes

Thank you so much!!
This works well, we also didn’t realize “timer” was built in, that is helpful for the future.