Robots can't count: Microswitches

We are using a microswitch on our robot to add and subtract the balls. The MicroswitchesWe are plugged with GND on C and SIG on NO (normally open).

I will add the code when I get the chance but it is basically:


Int totalBalls = 2


public int microSwitch(int totalBalls)

If (addball.get() = true and totalBalls >= 0 and < 3
Then totalBalls += 1

If (minusball.get() = true and totalBalls > 0 and <= 3
Then totalBalls -= 1

Else if totalBalls > 3 || totalBalls < 0
Then totalBalls

return totalBalls


When I hit the switch, it calculates the addition or subtraction, but when the switch is released, it goes back to the original value. Why an’t a robot count?

If your pseudo code matches your real code, the module level variable totalBalls defined at:

Will never be updated because the variable totalBalls defined as a argument to your method will take precedence.

Try renaming the module level variable to m_totalBalls or another name. Also, you only need to check the upper limit when adding:

If (addball.get() = true and totalBalls < 3
Then totalBalls += 1

And the lower limit when subtracting:

If (minusball.get() = true and totalBalls > 0
Then totalBalls -= 1