Arduino Joystick Help

Hello,

I have searched the forums but couldnt find the answer to my question (or I wasn't able to word it correctly in the search!).

A long story short, I am building my own switch panel for simulators.

I have it built using a matrix. I'm using the arduino pro micro (unbranded eBay special).
I have the joystick working, and all the switches are triggering the correct keys in the matrix (checked using Joystick Show). So, the hardware side of things is not the issue i'm facing.
However, I'm trying to modify the code so that I can trigger keys by a switch being (or not being) in one position or another.

For example. there are a mixture of "on-off" and "on-off-on" switches on the board.
For the simulator, I need to be able to send the 'off' position as a trigger.
So, an "on-off" switch will currently trigger one key, on or off. But I need it to be able to trigger one key if it's on, and another if it's off. The same for a "on-off-on" switch.

I have attached a picture of my matrix plan to try and explain what I mean.
The items written in black are the physical switch connections, and the items written in green are the triggers I want to happen when a switch is in the "off" position.

So far, I've written various lines of code that I thought would work, but I can't quite seem to get it.

I've tried placing this into the code where I thought it makes sense, but nothing seems to work.
Where I place it doesnt break the code, and the joystick will still operate as normal, it just doesn't perform the function that I need. I get no errors from the complier when I verify.

Here is the whole code to see what's going on (attached as text document due to character limit for the post).
(Also note, that I have adapated this code from someone elses example, so the main joystick code is not my own, only the adaptions).

I hope this all makes sense, and if I can clarify or explain anythign further, please let me know.

Thank you in advance!

code.txt (9.11 KB)

Admins, sorry I've just realised this may not be the best topic/thread for this question.
Please feel free move it if you feel neccesary.

Thank you.

Hello and good morning
I have tried to read and understand your wording describing your issue, during I´ve had my morning tea . A translation in my native language does´nt helps, too. You are able to write in simple words a simple function desribtion you are looking for?
When do you use a button with a button-function and a button with a switch-function?
What is the current task of the joystick function in conjunction with the depicted matrix?
Best regards
Paul
take care

Hi Paul,

Thanks for your reply!

Basically, imagine say, 6 switches.
4 x "on-off" switches
2 x "on-off-on" switches

On the joystick, this will use 8 'functions', one for each of the on positions.

  1. Switch 1 ON
  2. Switch 2 ON
    ...
  3. Switch 5 ON1
  4. Switch 5 ON2
  5. Switch 6 ON1
    8 ) Switch 6 ON2

What I am trying to achieve is 14 functions, one for each of it's positions.

So, something like this:

  1. Switch 1 ON
  2. Switch 1 OFF
  3. Switch 2 ON
  4. Switch 2 OFF
    ...
  5. Switch 5 ON1
  6. Switch 5 OFF
  7. Switch 5 ON2
  8. Switch 6 ON1
  9. Switch 6 OFF
  10. Switch 6 ON2

Where each of these are assigned to their own individual joystick button.
Hope that's a bit clearer?

Hello
Second try to understand your design goal.
Each joystick position, one to eight, will result to definded output behavior, ON or OFF, of the switches, one to six?
Best regards
Paul
take care

So, imagine I have a "on-off-on" switch connected.
When it is in position 1 (on) it puts the joystick button 1 to 'HIGH'.
When it is in position 2 (off) all of the joystick buttons are 'LOW'.
When it is in position 3 (on) it put the joystick button 2 to 'HIGH'.

What I want is when it is in position 2 (off), it puts joystick button 3 to 'HIGH'.

The code I wrote to try and make this work is:

if ((buttbx.key[1].kstate) && (buttbx.key[2].kstate) == RELEASED) {   // If the nav switch is not in 'on1' or 'on2' position...
 buttbx.key[3] = HOLD;                                               // ...then it must be in the 'off' position.
}
if ((buttbx.key[1].kstate) || (buttbx.key[2].kstate) == HOLD) {       // If the nav switch is in 'on1' or 'on2' position...
 buttbx.key[3] = RELEASED;                                           // ...then it can't be in the 'off' position.
}

But this doesn't seem to work.

Hope that makes it clearer?

Hello
Third and last try to understand your design goal.
The arrangement of the six switches, homeing their switch postion, pos1=ON, pos2=OFF, pos3=ON, will generate a specific simulated joystick position, one to eight.
Paul
take care

Pretty much.
I want the “off” position (of any switch) to generate a specific joystick button position.

I´m out

Perhaps a spreadsheet showing positions and switch settings, etc will help you to get your design proposal to be understandable to others.

Paul

Is it really not understandable?

A switch that has on and off positions cannot physically send a signal to the board when it is in the off position.
I want to tell the board that when the switch is not in an on position (so, it's off) that it turns on a different joystick button.

I really don't know else to explain this :confused:

Does this picture help?

Due to the fact that you use a switch with three stages this can generate three logical stages, offoff, onoff, offon, only. All other logical stages are magic or based fuzzy logic. :slight_smile:

There are some problems with the if conditions you have written.

There are 4 states for a key which give the corresponding value as follows:

Key State Corresponding Value
IDLE 0
PRESSED 1
HOLD 2
RELEASED 3

The if conditions you have written use boolean operators (&& and ||) with two terms and try to see if that is equal to a key state. (ex. A && B == C)
As boolean operators can only return true(1) or false(0). When you write

(buttbx.key[1].kstate) && (buttbx.key[2].kstate) == RELEASED

or

(buttbx.key[1].kstate) || (buttbx.key[2].kstate) == HOLD

The result of the boolean operation will either be 0(false) or 1(true), not the key state you are checking for
It is better to do this instead:

(buttbx.key[1].kstate) == RELEASED && (buttbx.key[2].kstate) == RELEASED

You first check if the key state of buttbx.key[1] is RELEASED,
then you check if the key state of buttbx.key[2] is RELEASED,
AND if both conditions are true(1), then the if condition is executed.

Looking at the attached code, one set of the if conditions has the following statement:

buttbx.key[34].kstate = HOLD;

The rest have something similar to this:

buttbx.key[33] = HOLD;

Why the difference between them?

Thanks for your reply naeems!

I'll start by answering your second questions about the differences.
That's just an error by myself. They should all be:

buttbx.key[34].kstate = HOLD;

I've been making changed on just the first switch to try and get it to work, rather than making a change to EVERY switch each time. Just makes it quicker to keep adjusting the code, uploading to the board and then trying it out again.

In terms of your main reply, what you've said makes perfect sense, thank you.
I've written it like this now:

if ((buttbx.key[10].kstate) == RELEASED && (buttbx.key[9].kstate) == RELEASED) {
              buttbx.key[34].kstate = HOLD;                        
            } else {
              buttbx.key[34].kstate = RELEASED;
            }

But, unfortunatley, there is still no change at all. The joystick still works fine (no matter where in the loop I physically place this code).
I've recorded a little video which I'll try and upload showing exactly what's going on.

kavster:
But, unfortunatley, there is still no change at all. The joystick still works fine (no matter where in the loop I physically place this code).
I've recorded a little video which I'll try and upload showing exactly what's going on.

Yup, for that, I have to point towards the positioning of the if conditions.
You set the key states for the OFF triggers, but don't do anything to the Joystick after that.
When the new loop starts and the board reads the keypad again, do the key states you set remain? or are they overwritten?

I've tried placing the conditions in different places within the loop.
I'm still pretty new to all this, so if I'm missing something obvious, I apologise.

But, as the switch will physically hold its position until it's changed by the user, the code should keep the keystate the same?

Or are you telling me that it isn't keeping it's keystate, and I need to go back to the drawing board? haha!

kavster:
But, as the switch will physically hold its position until it's changed by the user, the code should keep the keystate the same?
Or are you telling me that it isn't keeping it's keystate, and I need to go back to the drawing board? haha!

I don't have a keypad right now to check, but that was what I was wondering.
buttbx.getKeys() might not keep whatever you stored as a key state in the previous loop iteration.
But leaving that aside, here's what I would do if I was you:
In the checkAllButtons() function I would take all the if conditions out of the for loop
and place these if conditions after the for loop
After that, instead of setting buttbx.key[n].kstate in the if condition,
I would just trigger the appropriate Joystick button instead, like:

if ((buttbx.key[10].kstate) == IDLE && (buttbx.key[9].kstate) == IDLE) {
              Joystick.setButton(34, 1);
            } else {
              Joystick.setButton(34, 0);
            }

If this works, there is no real need for having extra buttons defined for the OFF state in the button box.

I really thought that was gonna be it!
But, unfortunately there was no change.

I even tried this, and it didn't work either:

if ((buttbx.key[10].kstate) == RELEASED && (buttbx.key[9].kstate) == RELEASED)) {
   Joystick.setButton(buttbx.key[34].kchar, 1);
} else {
   Joystick.setButton(buttbx.key[34].kchar, 0);
}

I'm sure it's something obvious, but I just can't see it.

I even tried literally copying the main 'switch' statement in the 'for' loop and writing so that if key[10] was off alone that key[34] would go on. But that didn't work either.

I placed it after the for loop, but within the buttbx.getKeys() statement.

I'm reading through the library manual again now to see if I can see anything else, but right now, I have no idea!

Thank you for your help so far btw, if you do have any more suggestions, I would be very grateful!