Pressed Function Coding

How do I create a function where if I press a certain button, the motor keeps spinning until I press another button. (Pressed Function)

// (in while loop)
if(controller.button.pressing()){
  spin = true;
}
if(controller.otherbutton.pressing()){
  spin = false;
}
3 Likes

You can also do something like this.

2 Likes

I would make a function like


bool motorState = false;

void toggleMotor(){
    motorState = !motorState;
    if(motorState){
        motor.spin(forward);
    } else{
         motor.stop();
   }
}

Then call this when the button is pressed

This is not the best way of doing it I think

I looked through your code and I saw that you independently called each indexer instead you could use motor groups like this

motorgroup indexers = motorgroup(indexer1, indexer2)

Then use it like 1 motor

Edit: fixed typo

2 Likes

Oh thanks that is amazing. I had no clue you could do that

Would you put that in where you set up the configuration at the top?

Yes, you would put this after you set up motors or with your variables.

Oh ok. I will try it.

Where you say in while loop, does that mean it has its own while loop? Meaning that I have a while loop for all of my code and one while loop for my function?

You just need to be checking that snippet regularly to check if the button is being pressed. That could mean having your entire code in a while loop or multithreading the function in a loop

1 Like