So I have worked with the Leonardo board multiple times and recently got into using it as a keyboard emulator. Well I just goofed up and can't figure out how to fix my mistake.
I accidentally put my keyboard print line into the loop and didn't realize it before uploading it. I have tried to leave it disconnected until the last second to upload my code to fix the issue, but the Leonardo is already emulating keyboard and typing. How can fix this?
Press the reset button on the Leonardo and keep it pressed.
Connect the Leonardo; keep the reset button pressed.
Start the upload while keeping the reset pressed.
When the IDE reports the memory usage, release the reset.
If you use IDE 2.0, you have some additional steps to do before the above will work; reason is that the IDE 2.0 wants to see a port before it even will allow you to start an upload.
Some options for that are
If you have another board like a Nano or Uno or if you have a TTL-to-USB converter, connect it to the PC and select its port; the target board will stay the Leonardo. Do not use another board that has native USB because the IDE will try to program that.
If you are using Windows and the IDE shows that COM1 or COM2 are available, select one of them. I'm no longer a Linux user but if the IDE shows e.g. a ttyS0, you can (more than likely) use that.
There is a third option to get a port but it's a little trickier. I will provide those instruction if needed.
PS
I suggest that you add a safeguard so you can easily stop the spamming. I will post an example later today.
The safe guard makes it easier to prevent / stop spamming.
You will have to sacrifice one pin of you board; in the example below it's A1.
#include <Keyboard.h>
#define SAFETY_ENABLED HIGH
const uint8_t safetyPin = A1;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(safetyPin, INPUT_PULLUP);
}
void loop()
{
// if the safetyPin is connected to ground
if (digitalRead(safetyPin) != SAFETY_ENABLED)
{
// send the text or press the keys
...
...
}
}
It's possible that you make mistakes and press a key but not release it; as a result, the key that was pressed is stuck in the PC. The below extends the above example to clear any keys when you disconnect the safety pin from ground.
#include <Keyboard.h>
#define SAFETY_ENABLED HIGH
const uint8_t safetyPin = A1;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(safetyPin, INPUT_PULLUP);
}
void loop()
{
// check if the safetyPin is connected to ground
if (digitalRead(safetyPin) != SAFETY_ENABLED)
{
// send the text or press the keys
...
...
}
// if the safety pin is not connected to ground
else
{
// release any key that might be stuck
Keyboard.releaseAll();
}
}
I'll have to tell you that a little later when I am doing my coding and stuff and am looking at it.
To get the Leonardo back to a safe place, I always use Blink as my program. The program I was working on was going to be linked to several buttons and when I pressed one it would automatically type repetitive code structures. I accidentally had one out of a statement and in the main, loop(). So it's just typing it over and over.
I'm having a similar problem with a Leonardo and Arduino IDE 2.0.3., on a Mac. I got the usb keyboard sketch to stop, but couldn't upload an updated sketch. The I tried the blink sketch and got this output, which is the same as the output when I tried to upload the keyboard sketch.
<-------------------------------------------------------------------------------------------------------------------->
Sketch uses 3992 bytes (13%) of program storage space. Maximum is 28672 bytes.
Global variables use 149 bytes (5%) of dynamic memory, leaving 2411 bytes for local variables. Maximum is 2560 bytes.
Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.
Programmer supports the following devices:
Device code: 0x44