How do you make a motor spin 3 times in a row when a singular button is pressed?

I am trying to make our indexer shoot 3 times when a button is pressed with waits in between each shot, is there any tips you guys can give me?

Use a task:

1 Like

To spin a motor a set number of times, I recommend using a for loop. This will repeat the code inside of the loop. In this snippet from our competition code, the indexer will rotate one time in a complete circle, but you can change the amount of degrees and add a second line to turn itself back if that applies. I also have our flywheel automatically turn off to prevent overheating by leaving it running. I hope this helps!

   if(Controller1.ButtonR1.pressing()){ //Choose which button you want pressed
     for (int i = 0; i <= 2; i++) {
         IndexerMotor.spinFor(forward,360,degrees); 
         //Add a line to turn indexer back if needed
         wait(1,seconds);
     }
     FlywheelMotor.stop();
   }
1 Like

Thank you so much, this will really help us for our adventure to Dallas for Worlds! We are trying to improve our robot before worlds and this is one of the ways it will work better. Thanks again!

Just remember, if you put that in your primary while loop in driver control, it will block other actions for more than 3 seconds. If you put it in a second thread (or task, same thing) you can run it in parallel.

6 Likes

What kind of different thread do you think? We have our autonomous shooting two discs so we don’t want it to be in the “When Started” thread.

Is there anything to be worried about if they put all that delaying inside a button callback? There is a single shared thread for all callbacks right? So during that shooting actions other callbacks likely wouldn’t work?

1 Like

On V5, every runs in a separate thread, so even if the callback blocks it’s all good.
On IQ, IQ2 and EXP, as we have more limited resources, a callback runs in a thread that’s assigned dynamically from what I call a thread pool, the number of available threads in the pool varies by platform, but IIRC is 8 on IQ.

As this looks like a V5 related question, putting this part

     for (int i = 0; i <= 2; i++) {
         IndexerMotor.spinFor(forward,360,degrees); 
         //Add a line to turn indexer back if needed
         wait(1,seconds);
     }
     FlywheelMotor.stop();

in a callback for the R1 button would be a good way of doing it.

and in blocks that would go under a “when button R1 pressed” hat

4 Likes

Maybe add an escape/timeout in case it sticks halfway through so that you’re not locked out from manual control of your mechanism.