Pretty much a Uno with less pins and small form factor
Congrats You Win! although still doesn't bring me any closer to a solution. It's like when you get the first thing in your head and it always sticks, when I first started dabbling with arduino stuff I ordered Nano's by mistake and I keep calling all these little boards Nano, BTW If you want some Nano Boards I got 4 brand new ones that I don't have a use for. ![]()
OK I can certainly say that I do not understand what you are trying to accomplish.
You have a button matrix and some other periperal input devices. The Arduinno listens to them and acts like a joystick w/ buttons to the receiving hardware (a PC)?
I have been working on the assumption that
- you want to be replace one matrix pushbutton with a toggle switch
- that, when flipped either way, will act like that replaced pushbutton was briefly pressed
The code I pointed you to illustrated something of interest, but was not exactly what you need.
By using the code from that thread, you are on track to add a total unassociated new toggle switch and do with it.
So first, do you want to replace a button with a toggle switch? This is what I meant to find
The thread lays out the same problem and I give a solution. The solution will require knowing what the program is doing!
I was overwhelmed by your code and the libraries, but I think
buttbx.getKeys();
scans the keyboard matrix and fills out some information like
buttbx.key[i].stateChanged
so that your subsequent logic can send
Joystick.setButton(buttbx.key[i].kchar, 1);
sequences that look like joystick or switch actions.
Do you know if Joystick.setButton sends out a sequence immediately it is called?
So. After the getKeys and before Joystick.setButton,a special case must be made for the particular switch that will be a toggle switch.
When you detect that button being pressed OR released, you do two things:
- Send a button press to the joystick and
- make a note to send a button release some time in the near future automatically
The near future could be the next time you call checkAllButtons, it could be on the 7th subsequent call or it could be done X milliseconds later.
It's straight ahead conceptually but is a hack and mightn't be so easy to surgical place into your existing code.
a7
Here is a simple demonstration of the hack:
I hope it is obvious that where I Serial.print messages is where you would want to be talikng to the joystick.
#include <Keypad.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(115200);
Serial.println("matrix toggle hack world!\n");
}
void loop() {
static unsigned needRelease = 0;
char key = keypad.getKey();
unsigned char xx = keypad.getState();
// Serial.print(" state "); Serial.println(xx, HEX); delay(5000);
if (needRelease && ((xx == RELEASED) || (xx == IDLE))) {
Serial.println(" second SEVEN...");
needRelease = 0;
}
if (key != NO_KEY) {
// special handle key 7
if (key == '7') {
Serial.print("special "); Serial.println(key);
Serial.println(" first SEVEN...");
needRelease = 1;
return; // all done special case
}
// normal key presses
Serial.print("regular "); Serial.println(key);
}
}
The slide switch (toggle stand-in) is wired across the membrane '7' contacts. If you leave the slide switch closed, the membrane keypad will not function. That is where the diodes may figure in.
a7
So you can see the rig. So American Truck simulator and Euro Truck Sim in their programing only accept a momentary joystick function for the ignition key switch. So for example I can turn the key on and that turns on the ignition, but when I turn off the key (which releases the Joystick Button) of course nothing happens as their programing is waiting for another button press, so if I turn the key on again it actually turns off the ignition, so then to turn back on you have to turn off the key switch and re turn it back on. If the ATS programing was better it would accept that the switch is on or off, but there in lies the problem. So would it be better to create a Small Keyboard Matrix because I can designate a Key press for the function, doesn't have to be a Joystick function. As I have freed up pins 6 and 7 which are Digital pins on the Pro Micro, can I make the Keyboard matrix on these pins and leave the joystick code alone? And yes i approach most coding as a hacker
OK so you could add an entirely separate toggle switch, and have code that calls through to the joystick functions.
@alto777's heroic effort in #25 falls short, as it does not send the "button up" message that might be expected and is delivered by the other code you have posted above.
So one last try. This is adding an entirely separate switch to an unused i/o pin, all you need is the switch wired from the pin to ground, and use pinMode INPUT_PULLUP.
The sketch below has one function that just watches the one switch and injects joystick button events into the stream. I think.
If that isn't what the joystick calls do, the hack may have to elaborated upon a bit. But all these schemes share the central idea of finding both the closing and the opening of the contacts and doing the same thing opon each.
# define SWITCH 7
void setup() {
Serial.begin(115200);
Serial.println("toggle hack world!\n");
pinMode(SWITCH, INPUT_PULLUP);
}
void loop() {
handleToggle(); // just call it frequently in your loop
}
void handleToggle()
{
static unsigned char switchState = HIGH;
static unsigned long lastAttention;
static unsigned char needRelease = 0;
unsigned long now;
if (needRelease) {
Serial.println("automatic - send joystick released 'K'\n");
needRelease = 0;
// guessing some character? :
// Joystick.setButton('K', 0);
}
now = millis();
if (now - lastAttention < 20) return;
unsigned char theSwitch = digitalRead(SWITCH);
if (theSwitch != switchState) {
if (theSwitch) {
Serial.println("switch left - send joystick pressed 'K'");
needRelease = 1;
// guessing some character?:
// Joystick.setButton('K', 1);
}
else {
Serial.println("switch right - send joystick pressed 'K'");
needRelease = 1;
// guessing some character? :
// Joystick.setButton('K', 1);
}
switchState = theSwitch;
lastAttention = now;
}
}
Of course I did:
It produces two messages for each change in the switch position , so it mimics pressing and releasing a button each time. The presses come from the user and the releases are generated automatically.
I write both the if and the else clauses, they are identical because it doesn't matter if you are toggling open or toggling closed, the output (button press and release) is identical.
Of course someone has pointed out that thre is no feedback so your "UP" may not end up being "ON". Perhaps there will be some consistent behaviour you can anticipate and exploit (also poers up running, whatever).
a7
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

