as the tittle states, how can I use logic to make two motors run while pressing one button
If (controller.ButtonR1.presed) {
Motor1.spin(forward);
Motor2.spin(forward);
}
and then I would put reverse instead of forward, right?
Yes, if you want to go reverse. It works like any motor.spin command, so you can set velocity, detection, ect.
You can put anything between the { and }, and that will run if the condition is true. You can also put any condition between ( and ), and the code will run. Only if it’s true.
You can look at this resource I posted to code your robot to turn on a motor whenever you press a button:
This code seems a bit off. First of all, @WonderBoii asked how to make two motors run while a button is being pressed. There is a key difference between the button.pressed() command and the
button.pressing() command. The button.pressing() command can be used to make a motor do something while a button is being pressed, whereas the button.pressed() function is used to perform a callback function.
If you wanted to make a motor go forward and backward while buttons are being pressed, your code would look something like this:
if (controller1.ButtonR1.pressing()){
Motor1.spin(forward);
Motor2.spin(forward);
}
else if (controller1.ButtonR2.pressing()){
Motor1.spin(reverse);
Motor2.spin(reverse);
}
else{
Motor1.stop();
Motor2.stop();
}
I also added the else statement at the end to make sure that the motors stop if a neither button is being pressed.
If you wanted to make a motor go forward and backward after a button is pressed, your code would look something like this:
void drive(){
Motor1.spin(reverse);
Motor2.spin(reverse);
}
while(true){
controller1.ButtonL1.pressed(drive);
}
Whichever funtions you are planning to use, make sure you put them inside the main driver control loop.
another thing, by any chance can you help me set the velocity too?
its for my roller intakes
You can use one of two methods to set the velocity of the motor. You initialize it before by using the Motor.setVelocity() command or you can embed it into the motor.spin statement.
Using the motor.setVelocity approach:
Motor1.setVelocity(200,velocityUnits::rpm);
Motor2.setVelocity(200,velocityUnits::rpm);
if (controller1.ButtonR1.pressing()){
Motor1.spin(forward);
Motor2.spin(forward);
}
Embedding it in the motor.spin command
if (controller1.ButtonR1.pressing()){
Motor1.spin(forward,200,velocityUnits::rpm);
Motor2.spin(forward,200,velocityUnits::rpm);
}
Both segments of code will spin the motor forward at 200 rpm while button R1 is being pressed, it just depends on which one you would like to use. The syntax can be found in the link that @Deicer provided