i want to create a gamepad using an Arduino Micro board. In the documentation I can read this sentence a lot:
To insure you don't lose control of your computer while running a sketch with this function, make sure to set up a reliable control system before you call Keyboard.print()
But what exactly is meant by "reliable control system"?
I understood:
The reliable system is basically a hardware switch. It switches off the board's serial output.
This is important since the board can't be programmed while it sends data.
AFAIK this only applies to automated boards that send data "on it's own" without previous user action.
In my case I just want to send something to the PC when the user pressed a button or moved the joystick on the gamepad. If nothing comes in, nothing comes out.
No. It is not for switching off the serial output. It is for switching off the emulated key presses.
I'll provide a minimal example:
// WARNING: do not use this sketch in real life!
#include <Keyboard.h>
void setup() {
Keyboard.begin();
}
void loop() {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('n');
delay(20);
Keyboard.releaseAll();
}
If you uploaded this sketch to your Micro, it would start opening hundreds and hundreds of Arduino IDE windows. You can unplug the board from your computer, but as soon as you plug it back in it will start spamming Ctrl + N again, so you might have trouble uploading a different sketch to recover the board from this state.
Now, of course you would never intentionally write a sketch like this, but bugs are inevitable and sometimes a program does something completely different than you expected it to. So it is easy enough for a tiny mistake to turn a sketch that was intended to be well behaved into some nightmare like the one I shared.
This is why it can be very useful to have a mechanism for being able to take control of a sketch like this. It doesn't necessarily need to be a hardware switch. You might even just add a delay on startup to give you enough time to get a sketch uploaded before the chaos begins.
In the end, it actually is easy to recover a board even with something like the sketch I shared, but you must know the trick. The tutorials are intended to help people learn to use Arduino, so they are recommending you to be careful so that you don't have that bad experience that might be discouraging to some people just as they are getting started with Arduino.