USB HID disabling Jumper&Code

Good day to you all.

So, i wanted to find a cheaper alternative to my Teensy LC as a USB HID for wiring buttons to it and acting as a gamepad/keyboard for emulators.

I bought an Arduino Leonardo, to test if it even works, and wanted to use the same code, but of course, it did not work.

So i used a different code/sketch:

void setup()
{
  pinMode(5,INPUT);
  digitalWrite(5,HIGH);

  Keyboard.begin();

  while(digitalRead(5))
  {
  }  

//Joystick and button connections
  pinMode(2, INPUT_PULLUP); //rechts oder D
  pinMode(3, INPUT_PULLUP); //links oder A
  }

void loop() {
  
// Check the switches:
int buttonState2 = digitalRead(2);
int buttonState3 = digitalRead(3);

// rechts oder D
if (buttonState2 == LOW) {
Keyboard.press('D');
}
else {
Keyboard.release('D');
}

// links oder A
if (buttonState3 == LOW) {
Keyboard.press('A');
}
else{
Keyboard.release('A');
}
}

This is just a test code to get it all started, i will add more buttons after it works.

Now my problem.
I read that other suers had one big issue. The Leonardo Clone does not have a button, so you can not make him go into programming mode. Which means, if they programmed it to act as a USB HID device, it won't let them reprogram it, as it is sending inputs constantly.

At least, that is what i understood was happening.

Now, they wrote about a jumper that would be connected to some pins. If this jumper is set, it would let the module go into USB HID mode, if you disconnect it, you can program it again, because it would stop acting as one.

As i am completely new to all this, i had problems finding a way to tell my board to use such a switch, i also failed in contacting those users and asking them to tell me how they did this.

Is someone here kind enough to explain to me how i could manage to do this (Hardware and Software)?

That would be great and i would be really grateful.

Thank you kindly and regards
Mitch

p.s. my old code included a "bounce.h", is this necessary?

The way that the code is written the program will not exit the setup() function if pin 5 is held HIGH. Try jumpering pin 5 to 5V and it should stop the loop() function from running.

UKHeliBob:
The way that the code is written the program will not exit the setup() function if pin 5 is held HIGH. Try jumpering pin 5 to 5V and it should stop the loop() function from running.

Hello, just so i am sure i understood this.
If i jumper PIN 5 to 5V, the module will execute the loop part and act as a USB HID as programmed,
but if i remove the jumper from PIN5 or 5V, it will stop executing the loop part and i could program it again without any problems and it should not recognize the board as a USB input device on my computer?

So i got that jumper part right?

Thank you very much!

If i jumper PIN 5 to 5V, the module will execute the loop part and act as a USB HID as programmed,
but if i remove the jumper from PIN5 or 5V, it will stop executing the loop part

Other way round. Jumper from pin 5 to 5V prevents execution of the loop() function.

i could program it again without any problems and it should not recognize the board as a USB input device on my computer?

Probably, but you will have to try it.

You really should look at the state change detection example. You want to press or release a key when the switch BECOMES pressed or BECOMES released (not IS).

UKHeliBob:
Other way round. Jumper from pin 5 to 5V prevents execution of the loop() function.
Probably, but you will have to try it.

Then it would be as expected. Thank you.

PaulS:
You really should look at the state change detection example. You want to press or release a key when the switch BECOMES pressed or BECOMES released (not IS).

I am sorry, but i do not at all understand what you are trying to tell me here.
I am really new to all of this and never have used C++ before this.

I read through the mentioned state change detection information and found several things.
As i am using push buttons here, the first thing might not be relevant, but if you are talking about bounce/debounce, i do not understand why this is necessary.

Or do you talk about some kind of delay in recognizing the button push?
So it will do something on being pressed not only if it is pressed?

Or am i completely wrong here? Thank you for your help.

it will do something on being pressed not only if it is pressed?

Yes, but not by using a delay. Look at the example Paul suggested to see how to do it.

I am sorry, but i do not at all understand what you are trying to tell me here.
I am really new to all of this and never have used C++ before this.

Being new to C++ is completely irrelevant.

When you press a key on your keyboard, what do you want to see on the screen? Press an a and see a or press an a and see aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?

If you want one a per press, you need to determine when the switch BECOMES pressed. If you want a bunch of as per press, then determining if the switch IS pressed makes sense.

On the other hand, releasing a key that has not been pressed does not.

As i am using push buttons here, the first thing might not be relevant, but if you are talking about bounce/debounce, i do not understand why this is necessary.

Debouncing is a separate issue.

If you are talking about https://www.arduino.cc/en/Tutorial/StateChangeDetection
then i see this is about turning on a led every x button pushes.
But i just want to send a certain letter on pressing a button and constantly sending it as long as the button is being pressed.

Also, this tutorial uses a delay.

I am sorry, but i am really overwhelmed by all of this.
What am i missing here or doing wrong with my code?

Or have i mixed this up and now it will send the inputs if the button is not pressed and stop if it is?

@PaulS
If i push the button for "a" i want to read aaaaaaaaaaaaaaaaaaaaaaaaaaaa as long as i hold that button.
Since this is for a gamepad, it will make sense to send this command as long as i push that button.
Or am i wrong here?

Let's say that pin2 is wired to a button which should make my character in a game turn left, i want it to turn left as long as i hold that button wired to pin2.

If you are talking about https://www.arduino.cc/en/Tutorial/StateChangeDetection
then i see this is about turning on a led every x button pushes.

NO IT IS NOT.

It is all about determining that the switch CHANGED state. What you do when you determine that a switch changed state is completely irrelevant.

PaulS:
NO IT IS NOT.

It is all about determining that the switch CHANGED state. What you do when you determine that a switch changed state is completely irrelevant.

As i understand, it is about counting how often this switch changed state and then doing something every x state changes.
It seems a bit unnecessary to count those, if i do not need and use this information.

If i assume right, you want me to use this method and ditch the counting part.
But it would help me a lot if you could explain to me why this method, being much more complicated, is better than the one i used. It may be all clearly visible to you, but i can't see the mistake i made with this code.

As i understand, it is about counting how often this switch changed state

The example shows how to determine that the switch changes state. In the process, it counts the number of times the switch BECOMES pressed.

Quit focusing on what the example accomplishes and focus on HOW it detects a state change. THAT is what you need to get from the example.

Sorry for trying your patience here.
I wish this would all be an open book to me, too.

So, i could be able to do a release only if the button becomes pressed, is this what this is about?

const int buttonPin2 = 2;     // the number of the pushbutton pin (d or right)
const int buttonPin3 = 3;     // the number of the pushbutton pin (a or left)

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
  pinMode(5,INPUT);
  digitalWrite(5,HIGH);

  Keyboard.begin();

  while(digitalRead(5))
  {  

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin2);
  buttonState = digitalRead(buttonPin3);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // hit d button:
Keyboard.press('D');
  } else {
    // release d button
Keyboard.release('D');
  }

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // hit a button:
Keyboard.press('A');
  } else {
    // release a button
Keyboard.release('A');
  }
}
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin2);
  buttonState = digitalRead(buttonPin3);

Read your comment again. Read THE state of THE switch. Not the state of BOTH switches, not using ONE variable.

  if (buttonState == HIGH) {
    // hit d button:
Keyboard.press('D');
  } else {
    // release d button
Keyboard.release('D');
  }

If you are not pressing the switch, release D over and over and over... If you are pressing the switch, press D over and over and over.

Did you miss the part where the example compared the current state of ONE switch to the previous state of the same switch? You code should do NOTHING if the state is not changed.

What it does when the state changes is to press OR release a key, based on what the state changed to.

Fleder:
Now my problem.
I read that other suers had one big issue. The Leonardo Clone does not have a button, so you can not make him go into programming mode. Which means, if they programmed it to act as a USB HID device, it won't let them reprogram it, as it is sending inputs constantly.

At least, that is what i understood was happening.

The Leonardos enter programming mode via serial open with 1200 baud AFAIR,
they don't need a programming button or a programm created stop the HID button.
The USB Interface attaches and detaches in that process, so the HID stuff should not really matter.

so the HID stuff should not really matter.

But if you have a HID program loaded on the Arduino that is constantly sending keystrokes via USB then it makes programming it via the IDE very "interesting"

I broke off the USB connector on my Leonardo, so I can not test it.

If it is sending Alt-F4 permanentely, its hard to keep any program open.

Just "acting as a gamepad/keyboard" should not be a problem if you do not press buttons.

Just "acting as a gamepad/keyboard" should not be a problem if you do not press buttons.

I agree. The fun really starts when you use mouse control to continually move the cursor. Don't ask how I know....

@PaulS
Well, i think i understood the original code, but this is a completely new thing to me.

Is this anywhere close to working code?

const int rightPin = 2;     // the number of the pushbutton pin (d or right)
const int leftPin = 3;     // the number of the pushbutton pin (a or left)

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
  pinMode(5,INPUT);
  digitalWrite(5,HIGH);

  Keyboard.begin();

  while(digitalRead(5))
  {  

  // initialize the pushbutton pin as an input:
  pinMode(rightPin, INPUT);
  pinMode(leftPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonStateRight = digitalRead(rightPin);
  buttonStateLeft = digitalRead(rightPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonStateRight == HIGH) {
    // hit d button:
Keyboard.press('D');
    // if it is LOW
  if (buttonState == LOW) {
    // release d button
Keyboard.release('D');
  }

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonStateLeft == HIGH) {
    // hit a button:
Keyboard.press('A');
  } else {
    // release a button
Keyboard.release('A');
  }
}

I know that it still releases the button all the time when not pressed, but i can't seem to find the part in https://www.arduino.cc/en/Tutorial/Button where it is described how to only release it if pressed.

@UKHeliBob
I am not going to use a joystick or a mouse, only button presses, so this should not be a problem then, according to @Whandall?

so this should not be a problem then

As long as you are not continually sending keystrokes or releases to the PC you should be OK.