I hooked up a mouse to be sure… and finally with success!
This is your way for the joystick button, but it works for the mouse. I have tried this with the joystick too, but it did not work. Maybe I have done something wrong since I got really confused, but I am glad that this works.
Just a quick thought : I see you use “else if” statements. Be careful with them as it can be tricky sometimes, depending on what you test and the order of the tests…
When debugging a code, I prefer using many single “if” statements one under another, even if it’s not optimized or doesn’t seem so. This way, I’m sure that all the tests are done at least once. With “else if” it may not always be the case.
I have tried, and it’s not working with the joypad/joystick
Also, joypads do not transfer X-Y completely, they only send +512 or -512 (not 0-512 for example)
I guess the mouse is fine then. Has a lot less buttons, but I will figure something out.
Now, with the new joystick it’s still not working. Button pressed is a single event, not a event that keeps being fired while having the button pressed. With the mouse it works.
Yea, sorry, tried to get a quick and detailed answer.
Anyway, here is the code: (had to remove some junk to make it fit)
class Program
{
static Servo[] servos = new Servo[18]
{
new Servo(X_SERVOA),
new Servo(Y_SERVOA),
new Servo(Z_SERVOA),
new Servo(X_SERVOB),
new Servo(Y_SERVOB),
new Servo(Z_SERVOB),
new Servo(X_SERVOC),
new Servo(Y_SERVOC),
new Servo(Z_SERVOC),
new Servo(X_SERVOD),
new Servo(Y_SERVOD),
new Servo(Z_SERVOD),
new Servo(X_SERVOE),
new Servo(Y_SERVOE),
new Servo(Z_SERVOE),
new Servo(X_SERVOF),
new Servo(Y_SERVOF),
new Servo(Z_SERVOF),
};
static void ButtonDown(USBH_Joystick sender, USBH_JoystickEventArgs args)
{
switch (args.ChangedButton)
{
case 2:
Debug.Print("down");
servos[Y_SERVOA].Increase();
servos[Y_SERVOB].Increase();
servos[Y_SERVOC].Increase();
servos[Y_SERVOD].Increase();
==== Error lines
servos[Y_SERVOE].Increase();
servos[Y_SERVOF].Increase();
====
break;
}
}
And the error:
[quote]#### Exception System.IndexOutOfRangeException - 0xa9000000 (4) ####
#### FEZ_USB_SERIAL_new_cable.Program::ButtonDown [IP: 0061] ####
#### GHIElectronics.NETMF.USBHost.USBH_JoystickEventHandler::Invoke [IP: 5e9dc] ####
#### GHIElectronics.NETMF.USBHost.USBH_Joystick::DeviceThreadMethod [IP: 00e8] ####
A first chance exception of type ‘System.IndexOutOfRangeException’ occurred in FEZ_USB_SERIAL_new_cable.exe
An unhandled exception of type ‘System.IndexOutOfRangeException’ occurred in FEZ_USB_SERIAL_new_cable.exe[/quote]
As said, I believe there is something wrong with the array.
class Servo
{
public int id;
public int position = 1500;
public int state = 0; // 0: none
// 1: increasing
// -1: decreasing
public Servo(int id)
{
this.id = id;
}
public void Increase() { state = 1; Debug.Print("state1: " + state); }
public void Decrease() { state = -1; Debug.Print("state1: " + state); }
public void Stop() { state = 0;}
public void Process()
{
switch (state)
{
case 1: // increasing
if (position 1115)
position -= 10;
SetArmPosition(id, position);
break;