Arduino 当按住按钮时会显示一个连续的字母
Arduino When keep pressing button a continuous Letter is show
首先抱歉我的英语不好
我有阿杜诺莱昂纳多
我有一个按钮好吗?
当我点击按钮时,字母 'W' 被打印到记事本
好吗?
我想要当我一直按住按钮时打印 'w' 字母
为什么?就像在游戏中,当我一直按住 'W' 字母时,玩家会移动,然后当我松开手指时,玩家会停止。
请拜托拜托我需要你的帮助因为我是初学者
这是我的代码
#include "Keyboard.h"
const int buttonPin = 4; // input pin for pushbutton
int previousButtonState = HIGH; // for checking the state of a pushButton
void setup() {
// make the pushButton pin an input:
pinMode(buttonPin, INPUT);
// initialize control over the keyboard:
Keyboard.begin();
}
void loop() {
// read the pushbutton:
int buttonState = digitalRead(buttonPin);
// if the button state has changed,
if ((buttonState != previousButtonState)
// and it's currently pressed:
&& (buttonState == HIGH)) {
// type out a message
Keyboard.print("W");
}
// save the current button state for comparison next time:
previousButtonState = buttonState;
}
首先,为什么你的代码只表现得好像按钮被按下一次的原因是以下几行
// if the button state has changed,
if ((buttonState != previousButtonState)
// and it's currently pressed:
&& (buttonState == HIGH)) {
因此,这只会在按下按钮后出现一次。如果您删除之前的状态检查并且仅检查按钮当前是否处于高电平,则每次程序循环时都会触发按下。不过这会有副作用,比如短按可能会触发多次。
幸运的是,键盘库提供了另一个函数来解决这个问题:Keyboard.press()
When called, Keyboard.press() functions as if a key were pressed and held on your keyboard. Useful when using modifier keys. To end the key press, use Keyboard.release() or Keyboard.releaseAll().
https://www.arduino.cc/reference/en/language/functions/usb/keyboard/keyboardpress/
因此,如果您像这样修改代码:
void loop() {
// read the pushbutton:
int buttonState = digitalRead(buttonPin);
// if the button state has changed,
if (buttonState != previousButtonState){
if( buttonState == HIGH ) {
Keyboard.press('w');
}else{
Keyboard.release('w');
}
}
// save the current button state for comparison next time:
previousButtonState = buttonState;
}
它的行为就像一直按下键盘按钮一样。
请注意,由于弹跳,目前脚本可能会像每次按下按钮一样按下几次。您可以通过在按钮按下和释放部分之后添加一个小延迟来解决此问题。这将使按钮有时间稳定到新状态。在此处阅读有关弹跳的更多信息:https://www.allaboutcircuits.com/textbook/digital/chpt-4/contact-bounce/
首先抱歉我的英语不好
我有阿杜诺莱昂纳多 我有一个按钮好吗?
当我点击按钮时,字母 'W' 被打印到记事本 好吗?
我想要当我一直按住按钮时打印 'w' 字母 为什么?就像在游戏中,当我一直按住 'W' 字母时,玩家会移动,然后当我松开手指时,玩家会停止。 请拜托拜托我需要你的帮助因为我是初学者
这是我的代码
#include "Keyboard.h"
const int buttonPin = 4; // input pin for pushbutton
int previousButtonState = HIGH; // for checking the state of a pushButton
void setup() {
// make the pushButton pin an input:
pinMode(buttonPin, INPUT);
// initialize control over the keyboard:
Keyboard.begin();
}
void loop() {
// read the pushbutton:
int buttonState = digitalRead(buttonPin);
// if the button state has changed,
if ((buttonState != previousButtonState)
// and it's currently pressed:
&& (buttonState == HIGH)) {
// type out a message
Keyboard.print("W");
}
// save the current button state for comparison next time:
previousButtonState = buttonState;
}
首先,为什么你的代码只表现得好像按钮被按下一次的原因是以下几行
// if the button state has changed,
if ((buttonState != previousButtonState)
// and it's currently pressed:
&& (buttonState == HIGH)) {
因此,这只会在按下按钮后出现一次。如果您删除之前的状态检查并且仅检查按钮当前是否处于高电平,则每次程序循环时都会触发按下。不过这会有副作用,比如短按可能会触发多次。
幸运的是,键盘库提供了另一个函数来解决这个问题:Keyboard.press()
When called, Keyboard.press() functions as if a key were pressed and held on your keyboard. Useful when using modifier keys. To end the key press, use Keyboard.release() or Keyboard.releaseAll(). https://www.arduino.cc/reference/en/language/functions/usb/keyboard/keyboardpress/
因此,如果您像这样修改代码:
void loop() {
// read the pushbutton:
int buttonState = digitalRead(buttonPin);
// if the button state has changed,
if (buttonState != previousButtonState){
if( buttonState == HIGH ) {
Keyboard.press('w');
}else{
Keyboard.release('w');
}
}
// save the current button state for comparison next time:
previousButtonState = buttonState;
}
它的行为就像一直按下键盘按钮一样。
请注意,由于弹跳,目前脚本可能会像每次按下按钮一样按下几次。您可以通过在按钮按下和释放部分之后添加一个小延迟来解决此问题。这将使按钮有时间稳定到新状态。在此处阅读有关弹跳的更多信息:https://www.allaboutcircuits.com/textbook/digital/chpt-4/contact-bounce/