Issues with multiple buttons controlling 1 motor

I have been working on my controls and I found that I need to control 2 motors in order to do certain things, I need to be able to press 1 button and have both spin forward, press a different button and have both spin backward, and press a third button that makes 1 spin forward and the other spin reverse. For the life of me I cant figure out how to make this happen.

Here’s some pseudocode that does this sort of thing:

//inside the infinite loop in your user control program:

if (Button1.isBeingPressed()){
    Motor1.spin(forward);
    Motor2.spin(forward);
}
else if (Button2.isBeingPressed()){
    Motor1.spin(backward);
    Motor2.spin(backward);
}
else if (Button3.isBeingPressed()){
    Motor1.spin(forward);
    Motor2.spin(backward);
}
else{
    Motor1.stop();
    Motor2.stop();
}
6 Likes

Hi @holbrook!
I have a question about your code.
Would this be any less clunky than the code you wrote? Or would it function the same way?

Changed Code
//inside the infinite loop in your user control program:

if (Button1.isBeingPressed()){
    Motor1.spin(forward);
    Motor2.spin(forward);
}
else if: (Button1.isBeingPressed) == False && (Button2.isBeingPressed) == False && (Button3.isBeingPressed) == False{
    Motor1.stop();
    Motor2.stop();
}
if (Button2.isBeingPressed()){
    Motor1.spin(backward);
    Motor2.spin(backward);
}
else if: (Button1.isBeingPressed) == False && (Button2.isBeingPressed) == False && (Button3.isBeingPressed) == False{
    Motor1.stop();
    Motor2.stop();
}
if (Button3.isBeingPressed()){
    Motor1.spin(forward);
    Motor2.spin(backward);
}
else if: (Button1.isBeingPressed) == False && (Button2.isBeingPressed) == False && (Button3.isBeingPressed) == False{
    Motor1.stop();
    Motor2.stop();
}
Your Code
//inside the infinite loop in your user control program:

if (Button1.isBeingPressed()){
    Motor1.spin(forward);
    Motor2.spin(forward);
}
else if (Button2.isBeingPressed()){
    Motor1.spin(backward);
    Motor2.spin(backward);
}
else if (Button3.isBeingPressed()){
    Motor1.spin(forward);
    Motor2.spin(backward);
}
else{
    Motor1.stop();
    Motor2.stop();
}

Yes. That would 100% be more clunky. It might work, but I see no reason to do it this inefficiently. Also, a tip about working with boolean operators: never say MyBool == false or MyBool = true. MyBool or !MyBool will work just fine.

3 Likes

That looks like it’d work fine, but I don’t think it’d be “less clunky”. Why check if all 3 buttons are not being pressed 3 times when you only need to check once?

4 Likes

Also, when working with many chained IfElse statements, consider using a switch statement instead.

Edit: To clarify, my definition of “clunky” is code that is much longer and more complicated than necessary.

3 Likes

I was really just thinking that when using if else: statements that often in drive control will always lead to clunky results, because that is the case when controlling the drivetrain.

Thanks for the tip! I’ll remember that for my future programs.

What is a switch statemet? I only really know python and it doesn’t seem that is an actual function in python.

That’s what I meant by clunky too.

Eh, a switch (as they work in c/c++) wouldn’t really make sense in this case. A switch will branch code based on the value of a single int (or something that can be cast/evaluated as an int). But since that’s not what we’re doing in this example, a switch wouldn’t be a good fit.

(Edit: If you really wanted to do this with a switch, you could do it by bit-shifting the boolean values and then bitwise or-ing them:

int buttons = Button1.isBeingPressed() | Button2.isBeingPressed()<<1 | Button3.isBeingPressed()<<2;
//buttons will be 1 if only button 1 is pressed, 2 if only button 2 is pressed, 
//4 if only button 3 is pressed, 0 if no buttons are pressed, 
//and another value between 3-7 if some combination of buttons is pressed

switch (buttons){
    case 1:
        Motor1.spin(forward);
        Motor2.spin(forward);
        break;
    case 2:
        Motor1.spin(backward);
        Motor2.spin(backward);
        break;
    case 4:
        Motor1.spin(forward);
        Motor2.spin(backward);
        break;
    default:
        Motor1.stop();
        Motor2.stop();
}

but this is not a good way to solve this problem.)

6 Likes

I converted your pseudo code into actual code but for some reason when I start the program both motors instantly spin forward and pressing the buttons doesn’t do anything, no clue why.

int l1 = (controller.get_digital(pros::E_CONTROLLER_DIGITAL_L1));
int l2 = (controller.get_digital(pros::E_CONTROLLER_DIGITAL_L2));
int x = (controller.get_digital(pros::E_CONTROLLER_DIGITAL_X));

if ((l1 = 1)) {
roller = 120;
flywheel = 120;
}
else if ((l2 = 1)){
roller = -120;
flywheel = -120;
}
else if ((x = 1)){
roller = 120;
flywheel = -120;
}
else {
roller = 0;
flywheel = 0;
}

Your problem is on lines like this:

if ((l1 = 1)) {

if (l1 = 1) will assign 1 to l1, and then return true. So all your iff statements will always evaluate to true, and thus the motor will always spin.

Two equals signs will test for equality, or alternately, since the values you’re reading are bools already (or can easily be cast to bools), you don’t need to test for equality at all.

if (l1 == 1){

or just

if (l1){

should both work fine.

5 Likes

Thanks so much, it works perfectly.

1 Like

If you want your code to look like this, just put this before and after your code ``` and it will automatically format.

Also just as a rule of thumb for programming, I would recommend never to use the letter l as a variable name, as it can be incredibly hard to read when next to 1 ( l1). Instead I would reccomend more meaningful variable names like motor1.

8 Likes

Also, avoid I and O because they are confusing.

5 Likes

That would work but its redundant, slow, and less readable.