Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Robots can't count: Microswitches (http://www.chiefdelphi.com/forums/showthread.php?t=103615)

NS_Radication 22-02-2012 13:47

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:
Code:

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?

Brian Selle 22-02-2012 15:18

Re: Robots can't count: Microswitches
 
If your pseudo code matches your real code, the module level variable totalBalls defined at:

Quote:

Originally Posted by NS_Radication (Post 1132648)
Int totalBalls = 2

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

Quote:

Originally Posted by NS_Radication (Post 1132648)
public int microSwitch(int totalBalls)


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


All times are GMT -5. The time now is 09:53.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi