Joysticks

We are having issues with our code on programming the joysticks. When we press the button our solenoids switch but sometimes they will switch right back. The Dashboard shows that they switch and then switch back sometimes. So I know they are opening. Any suggestions. Here is the code we are using

            if(js2.getRawButton(3) == true && j2b3 == false){
                s1.changeSolenoid(a1, a1End);
                s2.changeSolenoid(a2, a2End);
                j2b2 = true;
            }
            else{
                j2b2 = false;
            }

should it be j2b2 in the primary if statement as opposed to j2b3? not sure if that was a typo in posting, if so, then I might need a little bit more detail to help you.

What you are experiencing is called “contact bounce”. From Wikipedia:

Contact bounce (also called chatter) is a common problem with mechanical switches and relays. Switch and relay contacts are usually made of springy metals that are forced into contact by an actuator. When the contacts strike together, their momentum and elasticity act together to cause bounce. The result is a rapidly pulsed electric current instead of a clean transition from zero to full current. The effect is usually unimportant in power circuits, but causes problems in some analogue and logic circuits that respond fast enough to misinterpret the on-off pulses as a data stream.

You have to implement a filter function commonly called a switch “debounce” to get rid of the problem.

How might a fix this?

I would start by searching for the word debounce… You may find something.

One thing you can try is editing your else to an else if:

 ...
} else if (js2.getRawButton(3) == false) {
...
}

This makes sure it only reverts to true when the button is released.

The easiest way to implement debounce is to have a counter where you have to read an input for a certain number of times for it to be considered valid.