# I was wondering if there was a better way to improve my XNA joystick code? (Vb. Net)

I'm on a high school robotics team and I'm trying to code a Xbox 360 controller so that it'll controller the motors with variable speed. So far it works alright, but where I'm quite the novice coder I'd like to have more experienced eyes peer at my code and see if they can find a better way, or more effective way to do the same thing. As of now, as the code shows, when the sticks are moved it causes the value (-1 to 1) to be multiplied by 100 and sent to the motor controller as a value for its speed. A timer is also attached to continuously update the values to have the most accuracy possible. So far the only issue I've encountered is a minor dead zone at each perfect diagonal. I've pondered just coding in the diagonals just like the other directions but I've had little luck. Any pointers would be appreciated! Here's the current code:

Private Sub JoySticks()
'Left Joystick drive motor controls.
Dim LY As Integer
Dim LX As Integer
HScrollLX.Value = LX
VScrollLY.Value = LY
If LY > 0 Then
MC1.Velocity(0) = LY
MC1.Velocity(1) = LY
ElseIf LY < 0 Then
MC1.Velocity(0) = LY
MC1.Velocity(1) = LY
ElseIf LX > 0 Then
MC1.Velocity(0) = LX
MC1.Velocity(1) = LX * -1
ElseIf LX < 0 Then
MC1.Velocity(0) = LX
MC1.Velocity(1) = LX * -1
ElseIf LX Or LY = 0 Then
MC1.Velocity(0) = 0
MC1.Velocity(1) = 0
End If
End Sub

Private Sub TimerJoysticks_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerJoysticks.Tick
JoySticks()
End Sub

-

## migrated from stackoverflow.comFeb 10 '13 at 18:24

This question came from our site for professional and enthusiast programmers.

As the code is working without issue, this would be more appropriate on codereview.SE – Sam Axe Feb 9 '13 at 0:45

Just on the if statements. Could you do something like:

    Dim LY As Integer = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y * 100
Dim LX As Integer = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X * 100

HScrollLX.Value = LX
VScrollLY.Value = LY

SetVelocity(LY);
SetVelocity(LX,-1);

// Private method to set the Velocity values
private sub SetVelocity(position, factor = 1)
dim v as integer

if position = 0 then
MC1.Velocity(0) = 0
MC1.Velocity(1) = 0
else
MC1.Velocity(0) = position
MC1.Velocity(1) = position * factor
end if
end sub

-