# Changing speeds of a motor

Because of the different conditions on the field, our team would like to be able to change the base speed at any time during the match. We would want to achieve this by pressing a button the the controller once which changes the speed of base of the robot. If pressed again, the base speed returns to normal. Is there any way this can be achieved in pros?

1 Like

It’s fairly simple. You would want drive speed here to be a variable. When you press the button, the drive speed becomes set at a specific value (if statements and bools should help). When you press the button again, it changes the bool back. To make it easier, rather than using a boolean, as I’ve done this before, I find it easier to just use 1 (as the value of your updating, not speed, variable) and multiply by -1 every time the button is pressed. This way, if it’s -1, the speed is set to 20%, per say, and if the value is 1, it reads the controller for speed values. Hope this helped!

An example of how to do this is here, at the end of opcontrol. It uses okapi but the technique applies.

1 Like

if you use .isPressed() wont you have to hold the button for the duration of the task?

No. It registers a single tap every time you tap it once. You don’t have to hold it down to get it work.

Would something like this work?

``````void opcontrol() {

while(true) {

double left = abs(master.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_Y)) > 3 ? master.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_Y) : 0;
double right = abs(master.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_Y)) > 3 ? master.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_Y) : 0;

bool count = false;
int baseVol = 1;

if(master.get_digital(pros::E_CONTROLLER_DIGITAL_Y)) {
count = !count;

count == true ? baseVol = 0.5 : baseVol = 1;

pros::delay(10);
}

LMotor1 = left * baseVol;
LMotor2 = left * baseVol;
RMotor1 = right * baseVol;
RMotor2 = right * baseVol;
}
``````

The idea is that the if statement changes the baseVol according to the button press and multiplies the value (1 at full speed or 0.5 at half speed) directly to the joystick value.

ps. The delay in the if statement is to prevent the button being registered multiple times when pressed.

isPressed does not return true once per press, it returns true whenever the button is currently being pressed; if you held the button down for 1 second, then isPressed would return true for that entire period. If you want something that will return true exactly once when the button is pressed, look to the other methods, like changedToPressed.

2 Likes

Since I am not using okapi, how will I apply this to my program?

You can set something up to where you press button “1” and the chassis is set to fast. If you press button “2” then the chassis will be set to slower speed.

if(controller1.button1.pressed)
{set chassis to 100%}

else if(controller1. button2.pressed)
{Set chassis to 50%)

else
{}

Is

``````controller.get_digital
``````

the equivalent to

``````.isPressed()
``````

in okapi?

Yes, on a ControllerButton

1 Like

Oh sorry. I’ve been helping people with VexCode so i mixed the two up. My apologies.