das funktioniert bei mir einwandfrei:
/*
* Arduino Pro Micro - 2 Tastendrücke
* https://forum.arduino.cc/t/einen-tastendruck-ausgeben/1028454/
* by noiasca
* 2022-09-07
* board Resetten und halten
* Serial auf die angegebenen umschalten
* Kompilieren&Hochladen drücken
* Wenn fertig Reset loslassen
*
*/
#include "Keyboard.h"
constexpr byte schalterRechtsPin = 2;
constexpr byte schalterLinksPin = 3;
class Button { // class to debounce switches
const byte buttonPin; // the pin for the button
static constexpr byte debounceDelay = 40; // the debounce time - one value for all buttons (hence "static")
const bool active; // is the button HIGH or LOW active
bool lastButtonState = HIGH; // previous state of pin
byte lastDebounceTime = 0; // previous debounce time (we just need short debounce delay, so the rollover of the byte variable works fine and spares 3 bytes compared to an unsigned long)
public:
Button(byte attachTo, bool active = LOW) : buttonPin(attachTo), active(active) {}
void begin() { // sets the pinMode and optionally activates the internal pullups
if (active == LOW)
pinMode(buttonPin, INPUT_PULLUP);
else
pinMode(buttonPin, INPUT);
}
bool wasPressed() {
bool buttonState = LOW;
byte reading = LOW;
if (digitalRead(buttonPin) == active) reading = HIGH;
if (((millis() & 0xFF ) - lastDebounceTime) > debounceDelay) {
if (reading != lastButtonState && lastButtonState == LOW) {
buttonState = HIGH;
}
lastDebounceTime = millis() & 0xFF; // we store just the last byte from millis()
lastButtonState = reading;
}
return buttonState;
}
};
Button buttonA(schalterRechtsPin); // create a button object on this pin, by default, activate internal pullups
Button buttonB(schalterLinksPin);
void setup()
{
Serial.begin(115200);
Serial.println(F("Start..."));
buttonA.begin();
buttonB.begin();
}
void loop()
{
if (buttonA.wasPressed())
{
Keyboard.print('a');
}
if (buttonB.wasPressed())
{
Keyboard.print('b');
}
}
gibt sofort den Buchstaben aus auch wenn der Kontakt länger geschlossen bleibt.