Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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
    LY = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y * 100
    LX = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X * 100
    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
share|improve this question

migrated from stackoverflow.com Feb 10 '13 at 18:24

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

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

1 Answer 1

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.