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!");
};
}
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.