cant tell where delay is coming from

Hello,

New to arduino and coding in general. I am trying to make a very simple button panel for PC key shortcuts.

I have a two way flip switch and im trying to get it so that when its in one position it continuously sends the key to the pc as if i was holding a key down on the actual keyboard. In the other position I want the input to stop as if i have taken my finger off they key.

Having slightly altered the basic key input example code it seems to work ok, except that for some reason there is a slightly delay after they first keystroke and the rest of them.

To try and describe it....i flip the switch, i see the first "v" (or whatever) appear on screen, there is a momentary pause and then v's start spamming across the page as i would expect. Its hard to count the time but id guess maybe half a second or so. Its slight, but very noticeable.

Any ideas what is causing the delay?


#include "Keyboard.h"

const int buttonPin = 2;
int buttonState = 0;
int prevButtonState = HIGH;

void setup() {
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(buttonPin, HIGH);
Keyboard.begin();
}

void loop() {
buttonState = digitalRead(buttonPin);
if ((buttonState != prevButtonState) && (buttonState == HIGH)) {

Keyboard.press('v');
}

if ((buttonState != prevButtonState) && (buttonState == LOW)) {
Keyboard.releaseAll();
}
prevButtonState = buttonState;
}

It's a thing that makes using your actual keyboard possible. Apparently it's adjustable, but I've never done it before so I'm not sure how accurate this link is.

In games or other programs it should behave as expected as if you were holding the key down.

awesome, cheers :smiley: