Button Switch Help

Hi guys I am trying to program a Button Switch but I cannot figure out the code to save my life. I am new to programming and would like some help with some pseudo code to help make it work.image

Hi @Jax1, welcome to the VEX Forum!

Does the code you posted compile? If so, what does it do, and how is that different from the behavior you want?

It complies. When it runs all it does is spin forward. When I push the Bumper its supposed to reverse the motion but it just continues to spin forward.

Is the bumper pushed for a very short period of time or is it held down for a period of time.

I have tried holding it down because of the pressing command that I used in the code.

I’m just gonna say the best way to do a button switch is to do a state machine

What is a state machine?

Basically you make a variable (in this example the variable would be “lift_speed”)that changes on the press or off press of buttons. In this example (below) if the button down is pressed then it will set a speed for my motors and then change the variable. (The thing that is changed, for example in this example the motors, can be substituted with anything. Also instead of the down button being pressed, you would have the button on the robot set there.) If it detects that the button is unpressed and the variable has changed, it changes the variable again and primes the button for another press. Then if the button is pressed again and it detects that the variable was changed (again) it will change the speed for the motors, and change the variable, and so on. I hope this helps!
Here’s a example in our drive program:

How does the Bumper switch play a role in this at all? Isn’t there some where that the Bumper would come into play? Maybe Im just not understanding what this all does. All I am trying to do is when the button switch is pressed then the motors will spin in the direction I want. The issue im running into is that it isnt registering when the button is pressed. I am just not understanding thanks for your patience. I am not using a button its the bumper switch if I did not clarify that. Its also a new part. I received the part about a week ago at this point

If your bumper-switch isn’t registering when it is pressed id replace it

The only two potential problems I can see with this are either that the bumper switch is on the wrong port or that there is code after the if…else block that is overriding the the spin command

image
This is all the other code I have


for better reference

The first if in that above code is overriding the if…else at the top of the loop. If you remove the

if(BumperB.pressing()){
  Motor11.spinFor(forward,360,degrees,false);
}

then the code should work

Ill try this and if it works ill be incredibly happy!

It still did not work. This is my code


is it because it is not checking to see if the bumper is pressed or something like that?

You changed the first if statement so that the motor goes reverse by default.

What do you mean? I am confused

You made it to where if the bumper is 0 it spins reverse bcz you put while 1 it spins forward and else when its 0 to spin backwards. And 0 is when its not being pressed so by default it spins backwards

In the original post you had:
[pseudo-code]
if(BumperB pressing) then spin Motor11 reverse
otherwise spin Motor11 forwards

Then you changed it to
if(BumperB pressing) then spin Motor11 forwards
otherwise spin Motor11 reverse