Arduino Leonardo problem with Keyboard.press Function

Hi,

I built a simple wireless Power Point slide changer using Leonardo and a 433Mhz RF Tx-Rx Set.

I used 2 buttons, one for forwarding the slide and one for backwarding the slide.

I need one keystroke whether I push the button momentarily or too long.
Unfortunately using the sketch below I couldn't manage that. There is no problem with one of the buttons.
Whether I push it short or long it sends one keystroke.

If I push the other button longer than the delay(500) it sends two keystrokes. One pushing down and another one when releasing

What change should I make with the sketch? Please help...
(I used one of the built in example sketches (Keyboardmessage) and slightly modify it)

#include "Keyboard.h"

const int buttonPin = 4;          
const int buttonPin2 = 7;
int previousButtonState = HIGH;   
int previousButtonState2 = HIGH;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  Keyboard.begin();
}

void loop() {
  int buttonState = digitalRead(buttonPin);
  int buttonState2 = digitalRead(buttonPin2);
  if ((buttonState != previousButtonState)
      && (buttonState == HIGH)) {
    Keyboard.press(KEY_RIGHT_ARROW);
    Keyboard.releaseAll();
    delay(500);
  }
    if ((buttonState2 != previousButtonState2)
      && (buttonState2 == HIGH)) {
    Keyboard.press(KEY_LEFT_ARROW);
    Keyboard.releaseAll();
    delay(500);
  }
  previousButtonState = buttonState;
  previousButtonState2 = buttonState2;
}
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);

Just how ARE the switches wired? Using INPUT_PULLUP makes the wiring so much easier.

Dear PaulS,

Thank you for your answer.

I initially tried the sketch without the wireless hardware.
I used 2 tactile buttons on the breadboard. And wired them as in the example sketch.

When I saw your answer I realize that I did not check the long button press with the wireless hardware. Silly me...

The sketch is very well working with the wireless buttons....

RF433 Mhz receiver has D0-D1-D2-D3 pins for the received signal.
I wired D0 and D1 directly to the Leonardo's digital pins 4 and 7...

As i said before it works at the moment but I will search for INPUT_PULLUP method as yo suggest...

Rather then ignoring button presses for half a second on every press, I would recommend using a debounce timer:

#include "Keyboard.h"


const int buttonPin = 4;
const int buttonPin2 = 7;
int previousButtonState = HIGH;
int previousButtonState2 = HIGH;
unsigned long DebounceTimer;


void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  Keyboard.begin();
}


void loop()
{
  unsigned long currentMillis = millis();
  int buttonState = digitalRead(buttonPin);
  int buttonState2 = digitalRead(buttonPin2);


  if (buttonState != previousButtonState && currentMillis - DebounceTimer > 30)
  {
    previousButtonState = buttonState;
    DebounceTimer = currentMillis;
    if (buttonState == HIGH)
      Keyboard.write(KEY_RIGHT_ARROW);
  }


  if (buttonState2 != previousButtonState2 && currentMillis - DebounceTimer > 30)
  {
    previousButtonState2 = buttonState2;
    DebounceTimer = currentMillis;
    if (buttonState2 == HIGH)
      Keyboard.write(KEY_LEFT_ARROW);
  }
}

I tried your version and it works, thank you. I will try to learn differences between delay function and your version.
I am very very new at arduino and coding. I wish I were at school age with these things...
I have limited free/hobby time but there are so many things to learn about arduino etc... :frowning:

  if (buttonState != previousButtonState && currentMillis - DebounceTimer > 30)
  {
    previousButtonState = buttonState;
    DebounceTimer = currentMillis;

The timer part just requires that it has been 30 milliseconds since the last recognized state change before recognizing a new state change. If the input is going HIGH and bounces a few times the sketch will act on the initial change from LOW to HIGH but then ignore a bounce from HIGH to LOW and LOW to HIGH and HIGH to LOW and LOW to HIGH. By the time the timer ends the input should have stopped bouncing and should be a solid HIGH. The release (HIGH to LOW) is the same: act on the release and ignore bounces for 30 milliseconds. If the press is shorter then 30 milliseconds the transition to LOW will be processed as soon as the timer runs out (30 milliseconds). Since it isn't important for the two buttons to change within 30 milliseconds of each other, the same timer can be used for both.