Go Down

Topic: HID: Only works after upload (Read 420 times) previous topic - next topic

Jan_B

Hi,

I need my Arduino Pro Micro to send a "space" key to the PC with HID. It works fine, but only after upload. When I plug in my Pro Micro and toggle the button, nothing happens. But when I upload the sketch to the board, then toggle the button, the pro Micro sends the space key.

Where is my mistake?

thanks a lot
Bests
Jan

Code: [Select]
#include <Keyboard.h>
#include <HID.h>
int inPin = 8;
int val = 0;
 
void setup() {
  pinMode(inPin, INPUT);   
  Keyboard.begin();   
}

void loop() {
 

  if(digitalRead(inPin) == HIGH && val == 0)
  {
     Keyboard.write(32);
     val = 1;   
  }

}

pert

Do you have a pull-down resistor on your button?

sterretje

I think a reset will have the same effect as an upload.

Your val variable is initialised with 0.

The first time you press the button, the if condition evaluates to true and val be be set 1 and after that the if condition will never evaluate to true so no space will be send.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Jan_B

Thanks. Yes, there is a pull-down.
Your val variable is initialised with 0.

The first time you press the button, the if condition evaluates to true and val be be set 1 and after that the if condition will never evaluate to true so no space will be send.
True. But even when I reset the arduino or if I reconnect the Arduino to the power, it does not work! Only if I upload the sketch, it works after the upload.

Is there a problem with the serial connection? That the connection is only established when/after uploading, not if I plug in the Arduino without uploading the skecth? Seems so?

sterretje

No idea. If nobody here can help you, maybe ask Sparkfun, the pro micro is their product.

I only have a Leonardo do can't really advise further.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up