Leonardo Keyboard goof-up

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?

Hold down the reset button until upload first tries, then release it.

  1. Disconnect the Leonardo
  2. Load an innocent sketch in the IDE (e.g. blink).
  3. Press the reset button on the Leonardo and keep it pressed.
  4. Connect the Leonardo; keep the reset button pressed.
  5. Start the upload while keeping the reset pressed.
  6. 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

  1. 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.
  2. 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.
  3. 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 tried that multiple times, unfortunately to no avail.

What shows in the ide status window when you release the reset button.
What program were you trying to upload?

I'll try what you suggested a little later today. Hopefully something will work.

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

<-------------------------------------------------------------------------------------------------------------------->

@doug_pomerenke

That is not an error message. I admit that the message is extremely annoying.

So whatever your exact problem is, that is not the cause.

I got it to work simply by creating a new sketch with the same example blink test.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.