Accidental bad USB

I was attempting to make a macro pad but when I uploaded my program I did not realize that if (switchStatus = HIGH) would make the statement run when the switch is not pressed this made my code where whenever I plug in my pro micro it spams my computer with 'Hello!" to the point I cant reupload new code. How would I be able to fix this?

code:

int LEDpin = 2;
int buttonPin = 9;
#include "Keyboard.h"
void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(LEDpin, OUTPUT);
  Keyboard.begin();
}

void loop() {
  int switchStatus = digitalRead(buttonPin);
  digitalWrite(LEDpin, switchStatus);
  if (switchStatus = HIGH) {
    Keyboard.print("Hello!");
  };
}

= is not the same as ==

:wink:


;

};
Not needed.

  1. Disconnect board
  2. Press reset and keep it pressed.
  3. Connect your board to PC keeping the reset button pressed
  4. Start upload keeping the reset button pressed
  5. Wait till the IDE reports the memory usage and release the reset button.

This will work for the older IDEs, for IDE 2.0 instructions are slightly different.

And yes, I know that a Pro Micro does not have a reset button so you have to make a plan.

Below is how you can prevent your problem. Add a safe guard to your code so you can prevent it from sending data. It also solves the issue that a key that was pressed gets stuck in the PC because you forgot to release it.

The code uses a pin (A1) to disable sending data; you need to have one GPIO pin available. Feel free to change it to something that is suitable. Connect the pin to ground if you want to enable sending data, disconnect it if you encounter spamming or a stuck key.

#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)
  {
    // print, press and so on
    ...
    ...
  }
  // if the safety pin is not connected to ground
  else
  {
    // release any key that might be stuck
    Keyboard.releaseAll();
  }

  delay(500);
}

The lines with the triple dots represent the desired functionality / code.

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