How to make a switch function more abundant

Sorry to bother you
I have a Arduino Leonardo Pro Micro
Try a switch for the first practice to control the keyboard output string,
As below xx.ino

#include “Keyboard.h”

void setup() {
pinMode(2, INPUT_PULLUP);
Keyboard.begin();
}

void loop()
{
if(digitalRead(2)==LOW)
{
delay(100);
Keyboard.press(KEY_RIGHT_CTRL);
Keyboard.press(32);
Keyboard.releaseAll();
Keyboard.write(‘H’);
Keyboard.write(‘e’);
Keyboard.write(‘l’);
Keyboard.write(‘l’);
Keyboard.write(‘o’);
Keyboard.write(9);
Keyboard.write(‘W’);
Keyboard.write(‘o’);
Keyboard.write(‘r’);
Keyboard.write(‘l’);
Keyboard.write(‘d’);
Keyboard.write(9);
Keyboard.write(32);
delay(200);
}
}

==========================

Now I want to use a switch to achieve
Press once, keyboard input “Hello World”
Press the switch twice, and enter “Hello Taiwan” on the keyboard
Press the switch three times in succession, and enter “Hello Taoyuan” on the keyboard

Not sure if the switch case can achieve my function?
Ask everyone for professional guidance. Thank you very much


Switch-case can do this easily. The tricky part is getting something like 1, 2, or 3 into a variable so that the switch statement can act on it.

You need to count the number of keypresses within a certain time limit. Once the time limit has expired, any you have one or more keypresses, send the appropriate message and reset the counter. Start by deciding how long to wait between key presses, before acting on the current number of key presses that have been counted.

6v6gt:
You need to count the number of keypresses within a certain time limit. Once the time limit has expired, any you have one or more keypresses, send the appropriate message and reset the counter. Start by deciding how long to wait between key presses, before acting on the current number of key presses that have been counted.

Hello ,
This is a great idea! Thank you for your suggestion
It's a pity that I just started learning, the programming language is not very understandable
I'm sorry that English is not my native language. Is there a webpage that I can refer to or your experience to guide you?
thank you very much

vaj4088:
Switch-case can do this easily. The tricky part is getting something like 1, 2, or 3 into a variable so that the switch statement can act on it.

Hello ,
Yes this part is where i am stuck
How to count from the number of presses and convert the corresponding switch-case judgment,
I will try to search by keywords to see internet information
Thank you

6v6gt told you how to do this in Reply #2.

Don't use delay(...) because it will (mostly) prevent you from doing this. Learn to use millis(). See "Using millis() for timing. A beginners guide" which is a well-written guide by UKHeliBob.

As 6v6gt wrote, "Start by deciding how long to wait between key presses, before acting on the current number of key presses that have been counted."

Using millis(), store the time at which you get the first keypress and set the counter to one. For each subsequent keypress, increment the counter by one. When you run out of time (as determined by another millis() reading), use your switch-case statement.

Do NOT use the switch-case statement until you run out of time. You are probably going to need a variable (when used for this purpose, it gets called a state variable) that tells you whether or not it is the right time to use your switch-case statement. You may be able to share this function with the counter.

I don't know Arduino Leonardo Pro Micro so I do not know how you get keypresses so I cannot help you further.

Walk before you try to run. Begin by properly detecting a single key press. There are button switch examples sketches available in the IDE. You need to

  1. get rid of all those slow delay() calls.
  2. detect a change of state of the switch, not just the state.

Thank you all for your help
Later I referred to this method to use OneButton.h library
Solve the problem that I can open the application after frequently entering password verification at work
Thanks again