0

I am working on a project trying to use the wiimote to control a robotic arm. But first I want to test the wiimote in c#. Basically I want my wiimote to be updated each time I press a button and have a GUI interface that displays which button has been pressed in a check box list. But when I run my code and press a button the window form closes the program. I have tried for several hours to figure out this problem but I'm still stuck.

Wiimote wm = new Wiimote();

public Wiimote1()
{
    InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
    wm.WiimoteChanged += wm_WiimoteChanged;
    wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged;
    wm.Connect();
    wm.SetReportType(InputReport.IRAccel, true);
    wm.SetLEDs(true, false, false, false);
}

private void wm_WiimoteExtensionChanged(object sender, WiimoteExtensionChangedEventArgs args)
{

    if (args.Inserted)
        wm.SetReportType(InputReport.IRExtensionAccel, true);
    else
        wm.SetReportType(InputReport.IRAccel, true);
}


private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    wm.Disconnect();
}

private void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args)
{

    WiimoteState wmi = args.WiimoteState;

    buttonA = wmi.ButtonState.A;
    buttonB = wmi.ButtonState.B;
    buttonHome = wmi.ButtonState.Home;
    buttonOne = wmi.ButtonState.One;
    buttonTwo = wmi.ButtonState.Two;
    buttonPlus = wmi.ButtonState.Plus;
    buttonMinus = wmi.ButtonState.Minus;
    buttonUp = wmi.ButtonState.Up;
    buttonDown = wmi.ButtonState.Down;
    buttonLeft = wmi.ButtonState.Left;
    buttonRight = wmi.ButtonState.Right;
    AccelX = wmi.AccelState.Values.X.ToString();
    AccelY = wmi.AccelState.Values.Y.ToString();
    AccelZ = wmi.AccelState.Values.Z.ToString();

    switch (wmi.ExtensionType)
    {
        case ExtensionType.Nunchuk:
            nunC = wmi.NunchukState.C;
            nunZ = wmi.NunchukState.Z;
            nunAccelX = wmi.NunchukState.AccelState.Values.X.ToString();
            nunAccelY = wmi.NunchukState.AccelState.Values.Y.ToString();
            JoyStickX = wmi.NunchukState.Joystick.X.ToString();
            JoyStickY = wmi.NunchukState.Joystick.Y.ToString();
            break;

        case ExtensionType.ClassicController:
            clsA = wmi.ClassicControllerState.ButtonState.A;
            clsB = wmi.ClassicControllerState.ButtonState.B;
            clsX = wmi.ClassicControllerState.ButtonState.X;
            clsY = wmi.ClassicControllerState.ButtonState.Y;
            clsPlus = wmi.ClassicControllerState.ButtonState.Plus;
            clsMinus = wmi.ClassicControllerState.ButtonState.Minus;
            clsHome = wmi.ClassicControllerState.ButtonState.Home;
            clsUp = wmi.ClassicControllerState.ButtonState.Up;
            clsDown = wmi.ClassicControllerState.ButtonState.Down;
            clsLeft = wmi.ClassicControllerState.ButtonState.Left;
            clsRight = wmi.ClassicControllerState.ButtonState.Right;
            clsTriggerL = wmi.ClassicControllerState.ButtonState.TriggerL;
            clsTriggerR = wmi.ClassicControllerState.ButtonState.TriggerR;
            clsZL = wmi.ClassicControllerState.ButtonState.ZL;
            clsZR = wmi.ClassicControllerState.ButtonState.ZR;
            break;
    }

    checkList();

}


private void checkList()
{
    checkedListBox1.SetItemChecked(0, buttonA);
    checkedListBox1.SetItemChecked(1, buttonB);
    checkedListBox1.SetItemChecked(2, buttonMinus);
    checkedListBox1.SetItemChecked(3, buttonPlus);
    checkedListBox1.SetItemChecked(4, buttonHome);
    checkedListBox1.SetItemChecked(5, buttonOne);
    checkedListBox1.SetItemChecked(6, buttonTwo);
    checkedListBox1.SetItemChecked(7, buttonUp);
    checkedListBox1.SetItemChecked(8, buttonDown);
    checkedListBox1.SetItemChecked(9, buttonRight);
    checkedListBox1.SetItemChecked(10, buttonLeft);
}
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
e_hello
  • 51
  • 1
  • 1
  • 5

2 Answers2

0

If you are using a 64bit Windows, exceptions that are thrown in Form_Load event are silently swallowed. Maybe it is a good idea to move your code away from the Form_Load event. That makes future debugging much easier.

public Wiimote1()
{
    InitializeComponent();
    InitWiimote();
}


private void InitWiimote()
{
    wm.WiimoteChanged += wm_WiimoteChanged;
    wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged;
    wm.Connect();
    wm.SetReportType(InputReport.IRAccel, true);
    wm.SetLEDs(true, false, false, false);
}
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
0

You find it is much easier to figure out the error when you add exception handling to the code. Look up try...catch

If a caught exception doesn't make sense to you, you can ask questions about that here.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • Ok will read it up im new to c# wat is a try and catch. One more thing i tried running the same code on my laptop but it doesnt recognize wiimote on the windows form application i.e. when wiimote is not connected it still runs but which shouldnt happen because i havent connected the wiimote the program should return an error telling me it cant find a wiimote device. – e_hello Nov 10 '11 at 11:49
  • Thanks i found the error using the try and catch exception handling. It was a cross threading of different platforms error from the wiimote to visual studios. so i used the keyword delegate. – e_hello Nov 12 '11 at 03:42