With this code, when I connect pin 5 directly to ground, I simulate the press of "W" on my keyboard.
The only problem is that it is not detected as a "continuous press".
In fact when I try this in any simple game I see the character going step by step instead of running, just like when pressing "W" multiple times instead of keeping it pressed.
Could you help me understanding what is causing this behaviour?
I know nothing about the HID firmware that you are using, but the program that you posted seems to call the releaseKey() function every time through loop if state is not equal to 1. Try adding an else to the if statement and only call releaseKey() when state equals 1
Thanks for the reply UKHeliBob. The firmware that I am using is the one that I found here:
I also tried the unojoy firmware as suggested here in the arduino website (which also has a more clear coding going with it in my opinion) but I can't get that one to work so I used this second one.
Could you tell me more exactly what I should write in the code? Sorry, I am just starting out with this!
Two quick questions: the informations sent to identify the W key are universal across all USB HID devices? Should I expect the same correct behaviour if I now connect the Arduino to a Mac or gaming console?
My second question is about the code: I see that if the state is LOW the "26" is written in buf[2] ELSE the key is released.
I do not understand why is not the other way around - if pin 5 is connected to ground (HIGH) then the information is written in declared unit8_t.
I do not understand why is not the other way around - if pin 5 is connected to ground (HIGH) then the information is written in declared unit8_t.
If pin 5 is connected to ground, the pin will read LOW, which, because the pullup resistors are enabled, means that the switch is pressed, so sending the key pressed data makes perfect sense to me. When the pin reads HIGH, it means that the switch is not pressed, so sending the key released data makes perfect sense to me.
What does NOT make sense is sending garbage in position 1 in the array.
the informations sent to identify the W key are universal across all USB HID devices?
I don't know, but it may well be the case. Why not try it and see ?
I do not understand why is not the other way around
The input pin's pinMode() is set as INPUT_PULLUP which keeps the input HIGH unless you connect it to GND which causes it to go LOW
So
state = digitalRead(PIN_UP);
if (state == LOW)
tests whether the pin has been connected to GND, ie has been pressed, assuming that the switch is wired to take the pin to GND when the button is pressed.
Unfortunately I only have the last key (F) working this way.
Look carefully at what the code does. If any key except PIN_DOWN is LOW the code will get to the final else clause and call releaseKey()
Change the tests into if/else if/else if and so on so that first one to return true sets the value in buf[] and writes it. Then only if no keys are pressed will the final else be entered and call releaseKey()
If you are going to have more than 4 key inputs then there are better ways to manage it using arrays but get the current version working first.
UKHeliBob:
Look carefully at what the code does. If any key except PIN_DOWN is LOW the code will get to the final else clause and call releaseKey()
Change the tests into if/else if/else if and so on so that first one to return true sets the value in buf[] and writes it. Then only if no keys are pressed will the final else be entered and call releaseKey()
If you are going to have more than 4 key inputs then there are better ways to manage it using arrays but get the current version working first.
I understand what you mean. I have tried changing the conditionals to if/else if/else if/else if/else but, having the state variable in between the conditional statements gave me the error "else if without if before".
Hence I followed evanmars suggestion and declared different variables for each key, getting a more compact code:
The one thing that I am looking for is a way for the computer to detect multiple keystrokes at the same time because right now only one is detected and I cannot send a new one without releasing the previous.
I'm just wondering how to implement the above mentioned functionality of sending detecting multiple keypress at the same time.
Can you please be more explicit about what you want to do. Perhaps give some examples of the multiple simultaneous key presses that you want to detect and what you want to send as a result of them being pressed
Detecting the states of multiple digital inputs is easy, if that is what you have in mind. I would start by reading all of the inputs using a for loop at the beginning of loop() and putting the values in an array for use in the rest of the program.
Being more explicit: right now if I use arduino with this code as a game controller my character can only go in one direction (up, left, down, right - WASD) and cannot go in diagonal as if I were pressing the W+A (or D) keys. I suppose this is because with this code I can only send one key press at a given time?