I need help making a motor spin a certain amount of degrees and coding a emergency stop while the motor is in motion

I am building a cable winding machine in my engineering class and I need help coding it. I am using a limit switch as a emergency stop, a button as the start button, and a motor to wind up wire. The motor needs to be set up to spin a certain amount of degrees and you need to be able to press the button to start it and spin those degrees. You need to be able to press the button multiple times if needed though. While in motion the emergency stop(limit switch) needs to be able to stop it. I am using vex v5 block code.

1 Like

Welcome to the forums!
Firstly, I assume the start button is on a controller, but correct me if I am wrong.
Secondly, for an issue like this, trial and error is the best method for writing a program like this, especially if you have a limit switch.
As far as programming goes, I cannot do blocks right now, but you would want to write something like

if (Controller.Button.pressing){

there are also a lot of programming tutorials on the V5 blocks program, as well as example projects.

1 Like

Need more details.

You say ‘press the button’. Does this mean press and hold to activate it, then it shuts off if released? Or press/release to turn on and press/release to turn off?

Certain # of degrees per press? Or total?


It’s a certain amount of degrees per press.

Got to be careful with this… if you say turn X degrees and it’s short by .001 degrees the code can stop at that point. A Similar code failure can happen if you say turn exactly X degrees. If it overshoots, then the code fails. Unless the motor is overloaded, stalls, or mechanism jams you will probably hit your target. That said, I would code it to turn greater than or equal to X degrees.

But here is some base code without any bonuses/frills to get you started. Note that it does not cover instances like pressing the button again while the motor is moving already.


1 Like

Can a Motor.Stop() override a SpinFor() or does the application pause until the SpinFor() competes?

If it coded right, it can be stopped.

The code block is a starting point… there are many edge cases in it.

If you held the button down… the motor would begin turning and continue forever.

If it was held for 45 degrees, then released, that would be zero/beginning and it would continue for an additional 90.

The problem in both cases is that the code is cycling/looping and the initial condition is TRUE until the button is released OR the limit switch activates.

You could add some additional ‘and’ qualifiers like ‘motor not running’.

You could also code a different button to act as an emergency stop.

1 Like

It’s press and hold, although I think you can code it for pressing it once.