Keyboard.h Problems

So I messed up. I'm trying to build a macro keyboard using an Arduino Micro, and I uploaded a short bit of code for testing that should have pressed and held a key while a switch was pressed, and released the key otherwise. I didn't think this through very well before uploading my code to the Micro, and there was a mistake in my code. Now while the Micro is plugged into my PC via USB it continuously hits my 'a' key. I tried reuploading an empty sketch, but while trying to upload the code the Arduino takes over and tries to type infinite 'a's all over my sketch. I opened notepad thinking that I could press upload and then immediately click in the notepad so that all of the 'a's were typed in a throw-away note instead. This seemed to work, but then the upload never completes. I don't get an error message or anything unless I unplug the board or reset it while the IDE is trying to upload the code. The IDE just shows that it is uploading code indefinitely.

Does anyone know how I can get around this issue and flash a blank sketch to my Arduino so I can upload new code that isn't broken?

Thank you in advanced!

Try holding reset on the board, launch the code upload and when you see the computer trying to upload release the reset button. With luck the boot loader will catch the upload command before handing over the control to your sketch. You might have to try a few times

It’s always good to RTFM…

A word of caution on using the Mouse and Keyboard libraries: if the Mouse or Keyboard library is constantly running, it will be difficult to program your board. Functions such as Mouse.move() and Keyboard.print() will move your cursor or send keystrokes to a connected computer and should only be called when you are ready to handle them. It is recommended to use a control system to turn this functionality on, like a physical switch or only responding to specific input you can control. Refer to the Mouse and Keyboard examples for some ways to handle this.

When using the Mouse or Keyboard library, it may be best to test your output first using Serial.print(). This way, you can be sure you know what values are being reported.

Worked on the first try, thank you! Definitely will be paying more attention when I'm using this library now because that was a hassle to say the least

You can use what I call a safety pin, see code example in Keyboard Code - #10 by sterretje. That way you can prevent spamming.

Also be aware that your PC will remember Keyboard.press; do not forget to use Keyboard.release / Keyboard.releaseAll. Else the key that is pressed by the Arduino will be stuck in the PC.

1 Like

Glad it helped !