Leonardo Keyboard - Need help

hello...

i need help with my code.

i ve a arduino leonardo and 3 push buttons

button 1 on pin 2
button 2 on pin 3
button 3 on pin 4

if i push button 3 short, the arduino should write (as keyboard) the string 'f' (ascii 121), if
i push the button long, the arduino should write 'g' (ascii 122).
but the arduino print always 'fg'. >:(

void setup()

{     

    pinMode(2, INPUT);

    pinMode(3, INPUT);

    pinMode(4, INPUT);

    digitalWrite(4, LOW);

  

    Keyboard.begin(); 

}



void loop()

{

//    -------------------------------------------------- Encoder 1

    

    buttonStateLeftRE1 = digitalRead(2);            // Encoder 1: Drehe Links 

    if (buttonStateLeftRE1 == HIGH)

    {

        Keyboard.press(100);

        delay(100);

        Keyboard.releaseAll();

    }



    buttonStateRightRE1 = digitalRead(3);            // Encoder 1: Drehe Rechts

    if (buttonStateRightRE1 == HIGH)

    {

        Keyboard.press(101);

        delay(100);

        Keyboard.releaseAll();

    }

    

    buttonStateButtonRE1 = digitalRead(4);            // Encoder 1: Drücke Button

    if(buttonStateButtonRE1 == HIGH)

    {

      while(buttonStateButtonRE1 == HIGH)            // Bleibe in Schleife, solange Button 1 gedrückt wird

      {

          delay(100);

          timer1 = 1;

          delay(1500);

          buttonStateButtonRE1 = digitalRead(4);

          timer2 = 2;

          buttonStateButtonRE1 = digitalRead(4);

      }

    }

    

    if (timer1 == 1)

    {   

        Keyboard.press(102);

        delay(100);

        Keyboard.releaseAll();

        timer1 = 0;

        }

        

    if (timer1 == 2)

    {   

        Keyboard.press(103);

        delay(100);

        Keyboard.releaseAll();

        timer1 = 0;

    }



}

You need to look at the state change detection example. Record when the switch becomes pressed, but do nothing else. When the switch becomes released, record the time, and compare that to the became pressed time to determine how long the switch was pressed. Send the appropriate keystroke based on the time. NO delay() calls needed.

It can NOT be done in a single pass through loop(). Or, rather, should not.

Hmm, OK. Im New in arduino world.
Perhaps anyone can correct my code.

You have to put some effort in it please.
PaulS refers to this example : https://www.arduino.cc/en/Tutorial/StateChangeDetection
Try that example, just to see what happens.

PaulS also wrote that you can record the moment the switch is pressed. That is done with millis(). Using millis() takes some time to get to know how to use it.
This example is the first step in using millis() : https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
Maybe you should try that example as well, just for fun.

There are three things:
1 ) Detecting a state change of a button.
2 ) Measure the duration of how long the button is pressed.
3 ) Send a keystroke to the computer.

Try to keep those three things seperated in your sketch.
The better you keep it seperated, the easier you can change things and it makes it also easier to test.
You might need to add a software debounce for the buttons later on.

thx for the links and your answers.

i read and try both.

but sorry, i havent not so many experience with C/C++.
i dont know, what i must do. perhaps anyone can write
me the correct code. thx...

regard

We don't write the complete sketch for you. Sorry.
Did you try the State Change Detection example ? Do you understand it ?

yes, i had try both examples.

I think i understand the half of the code. but dont know,
what parts of this code i need for my problem. :frowning:

for what i need the state of the pin?

i want:

short press of my pushbutton -> send key x
long press of my pushbutton -> send key y

I know you want that !

If you want a quick project, do one of the smaller projects at https://learn.adafruit.com
What you want requires some real programming. It requires to detect the state change, to record the time, and to send a keystroke. And I don't want to write the whole sketch for you.

It is often like that. What seems simple in the real world requires some programming skills to put that into programming code.

As I wrote in my Reply #3, start with the state change. Can you make that work for your all your three buttons ?
If you can see the text in the serial monitor when they are pressed and released, you are on the right track.
Show us that state change, and we help you to add millis() to it.

I'm thinking of a quick way to get some results, using delay() as you do in your sketch. But I think that will run into problems. So I only know the normal way, and that is step-by-step solving this.

i try now this, i hope i’m on the rigth way.

but unfortunately nothing happens

buttonStateButtonRE1 = digitalRead(4); // Encoder 1: PushButton
if (digitalRead(buttonStateButtonRE1) == HIGH)
{
timerStart = millis();
while(digitalRead(buttonStateButtonRE1) == HIGH)
{
timerEnd = millis();
}

if (timerEnd - timerStart < 1000)
{
Keyboard.press(45);
delay(100);
Keyboard.releaseAll();
}

if (timerEnd - timerStart >= 1000)
{
Keyboard.press(46);
delay(100);
Keyboard.releaseAll();
}

} // End If

That is not according the StateChangeDetection example.
Waiting in a while loop as long a the button is pressed might work, but I don't want to go there. I should first test that, and my all Arduino boards are stored in boxes at the moment.

Please read number 7 on this page about code tags : http://forum.arduino.cc/index.php/topic,148850.0.html

Please post the whole sketch : http://snippets-r-us.com/
You have not yet shown the complete sketch, so I don't know the type of variables.

Could you make the StateChangeDetection for all your three buttons ? or do you want to continue with waiting in the while-loop ?

If you can make one button work you can make three work. Focus on one for now. It's doable without using any delay() calls. It's important that you get the state change concept. All you do is have a variable to store the last known state of the pin. Then every time loop() executes read the pin and compare your last known state with the current reading. If they are not equal then a state change has occurred. That's when you can save the new state and set timers and flags.

Your code is't going to work. First you read pin 4 and save that reading in buttonStateButtonRE1. Then you use that result as the pin to read in your if(). You have to think about what is going to happen when you use a function like digitalRead().

  buttonStateButtonRE1 = digitalRead(4);      // Encoder 1: PushButton
  if (digitalRead(buttonStateButtonRE1) == HIGH)
    {
    timerStart = millis();
    while(digitalRead(buttonStateButtonRE1) == HIGH)
    {
        timerEnd = millis();
    }