We are using Vex IQ block coding and would like to start and stop a motor with the same button. The simple solution seems like the If/Then/Else command. However, trying that and many other things, the team could never “toggle” back and forth with the motor starting and stopping with the push of the same button. Thanks for any advice we get.
Welcome to the forum!
Seems like you are on the right path - could you share the code you have so far? We will be better able to help you that way.
What behavior is happening with the code you’ve posted?
When Eup is pressed the motor starts. However, when it is pressed again, the motor doesn’t stop.
“isSpinning” doesn’t actually mean the motor is moving, it means you sent the motor a spinTo or spinFor command and the motor is still spinning to the new position.
You will need to create a new boolean to track the state of the motor and test that instead.
I’m sorry but you’re talking above my students’ and my head. Is there a block example someone can show of how to get a button to do this? It must be a common thing for teams to want to do to save having to use one button to start the motor and one to stop it. Thanks so much.
A boolean is a form of variable that can have two states: true or false. Other forms of variables include integers and lists, which can be numbers or a group of numbers respectively.
Essentially, what you have to do here is keep track of if the motor is spinning or not yourself. We can do this by making a new boolean in the Variables tab. In my example I named it
is_spinning, but you can name it whatever you want.
When the program is started, we want to set
is_spinning to false (because the motors are probably not spinning immediately when the program begins).
Then, inside our
When Controller button [E Up] [pressed] block, we want to change what the motor is doing depending on the value of the boolean
is_spinning is true, we want to stop the motor and set
is_spinning to false. If
is_spinning is false, we want to spin the motor and set
is_spinning to true.
I hope that you and your student took something away from this post.
Something like this should do the trick. (please note that I hardly ever use Vex IQ blocks, but the logic here is correct).
Also a good idea is to add a “wait until <not ((e up button) pressed)>” at the end so it doesn’t cycle over and over, making a random outcome.
No need to do that when using events.
Thanks so much for all of the responses. We’ll try it out at the next practice and give an update. It will definitely provide a coding instruction moment. Our team is getting pretty good at the basic coding commands and really likes having controller buttons do a variety of things. This year they want to learn the more advanced features and tricks. This will fit right in.
This is a little tighter and gets them to use the “not” block.
I think this is the real problem, or a good bit of it.
Alternatively, you can simply put a “wait .25 seconds” after the spin/no spin command.
The brain’s code is cycling MANY time before you can get your finger OFF the button.
It’s an event handler, it will only execute once each time the button is pressed.
Ah… I’m used to seeing that bit of code inside a forever loop.
Yep, I’m always on that side of the button. Watch it being pressed, do the debounce, still pressed, etc.
Life is much simpler when it’s events.
Thanks, everyone for all your suggestions. The kids tried both of the first two methods presented, which both worked of course. It gave a wonderful opportunity for them to learn new coding tools and freed up two controller buttons for them to run other macros with.
Next have them code a double click…