How to change speeds of a motor

hello, I’m trying to make my motors change speed by the press of a button I currently have them at half speed and when I push x it should change it to full speed and I don’t know what to do or how to fix kinda stuck here.

here’s my code…( i apologize for the sloppiness plan on making it more pretty later)

int x;
x = 0;

if(x == 0)
{
		if (master.get_digital(DIGITAL_L1)) //arm up
		{
			left_arm.move_velocity(-100);
			right_arm.move_velocity(-100);
		}
		else if(master.get_digital(DIGITAL_L2)) //arm down
		{
			left_arm.move_velocity(100);
			right_arm.move_velocity(100);
		}
		else
		{
			left_arm.move_velocity(0);
			right_arm.move_velocity(0);
		}
}

//----------------------------------end of arm function

if(master.get_digital(DIGITAL_X))
{
x = 1;
}

if(x == 1)
{
if (master.get_digital(DIGITAL_L1)) //arm up
{
left_arm.move_velocity(-200);
right_arm.move_velocity(-200);
}
else if(master.get_digital(DIGITAL_L2)) //arm down
{
left_arm.move_velocity(200);
right_arm.move_velocity(200);
}
else
{
left_arm.move_velocity(0);
right_arm.move_velocity(0);
}

if(master.get_digital(DIGITAL_B))
{
  x = 0;
}

}

Instead of using nested if loops you could also use && (not sure if it would actually fix it). Also move the B button part outside of the if statements. Try checking the motors on the brain and see what the command is for the arm motors. Maybe you have a 100 rpm cartridge in your motor and making the motor go at 200 would be the same as going at 100.

1 Like

Set x equal to your default divider.

You only update the value of x when the button is pressed so you can use:
If “button pressed” then x = (x==1) ? 2 : 1; which is a conditional (a short hand if-then-else)

In your motor control statements you can divide your velocity by x.

If you still need help I have a link to actual code that uses a button press to switch between brake modes (and other drive options) but it uses okapilib 3 which has different functions.
You don’t need to get two buttons to change speed. One button can toggle it.