How to change turn velocity by 5% with a button press

so im writing code, and I am trying to make it so that when ButtonDown is pressed, it sets the turn velocity to 5% lower than what it is at. for example, if it is at 100% turn velocity, and I press ButtonDown, it changes to 95%, another button press would set it to 90% and another to 85%, etc.

same thing wanted for going up, and with Drive velocity
the code I thought would work, but doesn’t is posted below:
(it just sets it to go 5% in the other direction)
Controller1.ButtonUp.pressed(Drivetrain.setTurnVelocity(-5, percent))

1 Like

You need to check for a new button press every time or else it will keep dropping your velocity every time your loop updates. In PROS you can do something like

if (master.get_digital_new_press(E_CONTROLLER_DIGITAL_A)) {
     // increase velocity
    }
else if (master.get_digital_new_press(E_CONTROLLER_DIGITAL_X)) {
     // decrease velocity
    }
else {
    // don't change velocity
   }

I’m not sure if there is a similar feature in vexcode but it should be fairly simple to implement.

1 Like

AryanG, could you please explain that more, i tried to copy and paste that in, but it didn’t work
our drivetrain is called Drivetrain, and our controller is Controller1

That’s for PROS, not vexcode.

for vexcode you’d use this:

if (Controller1.ButtonUp.pressing()) {
     // increase velocity
    }
else if (Controller1.ButtonDown.pressing()) {
     // decrease velocity
    }
else {
    // don't change velocity
   }

Keep in mind that if this is just thrown into a while loop it’ll check every microsecond or so, turning the velocity to the extremes in a fraction of a second. Ideally you’d only check this once every half-second or so.

2 Likes

Your logic is almost there. The problem lies with how the .set function sets the speed to your parameter and nothing else. So when you tell it to go -5 units, its going backwards 5 units (negative is reverse). What you need to do instead is store your wanted speed in a variable, and use that variable to set the speed. When you want to change the speed of the drivetrain, change the value of that variable.

I also want to ask what you are trying to accomplish by doing this. Whatever you are trying to fix, there probably is an easier way than pushing a button a bunch.

if (Controller1.ButtonUp.pressed()) {
     driveSpeed += 5;
}
else if (Controller1.ButtonDown.pressed()) {
     driveSpeed -= 5;
}

Drivetrain.setTurnVelocity(driveSpeed, percent);
2 Likes

You also need another variable to keep track of the button states

int maxVelocity = 100;
bool buttonUpRegistered = false;
bool buttonDownRegistered = false;
while(1)
{
   int scaledValue = Controller1.Axis1.value() * maxVelocity / 100;

   // do something with scaledValue

   if (Controller1.ButtonUp.pressing() )
   {
     if( buttonUpRegistered==false )  // only change scaling once per button press
     { 
      maxVelocity += 5;
      if( maxVelocity>100 ) // do not go over 100%
          maxVelocity=100;
      buttonUpRegistered = true;
     }
   }
   else
   {
      buttonUpRegistered = false;
   }

   if (Controller1.ButtonDown.pressing() )
   {
     if( buttonDownRegistered==false )   // only change scaling once per button press
     { 
      maxVelocity -= 5;
      if( maxVelocity<50 ) // do not drop below 50%
          maxVelocity=50;
      buttonDownRegistered = true;
     }
   }
   else
   {
      buttonDownRegistered = false;
   }

  // rest of the usercontrol code

 vex::task::sleep(10);
}
2 Likes

Well all these ideas are good, the plan is to have one button that sets Turn velocity to 75 percent, one that sets it to 40 percent, one that sets it to 5 percent higher than before, and one that sets it to 5 percent lower than before. Then do the same for the lettered buttons but with drive velocity, I’m using a drivetrain BTW

Your code is great. But I would like to suggest that you combine two else statments into one and do not use with “else”.

if (!Controller1.ButtonUp.pressing() && !Controller1.ButtonDown.pressing()) {
buttonDownRegistered = false;
}

Why? All that does is make it so you can’t change the velocity in the same direction every other interval.

Your interval is also only 10 miliseconds, so holding down the button will mean immediate changes from 0 to 100 and vice versa

Your code is OK but less practical than @weilin. Note you will need to put your code in the infinite while loop of driver control. And you will need to press and release the button within the while loop circle. It is physically challenging when each while loop circle is 10 ms. If you press the button and release it after 15 ms then the velocity change by 10% since the code repeated for two while loop circle. @weilin 's code solves this issue.

1 Like

He expanded the time to release from 10 ms to 20 ms. The cool down needs to be much greater. I wrote pseudocode because we aren’t supposed to write the code for the OP, that’s why I didn’t add the loop.

Has there been a working solution for this code?

Please do not revive old threads. If you still have a question after reading an old thread, start a new one specific to your question.

@DRow please lock

1 Like

Sorry, didn’t mean to revive it- just wanted to implement this function on my bot is all

Sorry to bother anyone or revive the thread again, but I really need to know if there has been a solution to this problem or if I should make another topic about it.

Make another topic about it.

It may be best to link to this topic showing that you did your homeowkr and there was no solution.
New topics tend to get more attention than old ones anyways so you’re likely to get an answer faster than if you keep pinging back here.

5 Likes