gpt4 book ai didi

Arduino Micro 无接触触发

转载 作者:行者123 更新时间:2023-12-04 05:16:44 25 4
gpt4 key购买 nike

我正在使用 Arduino Micro 在我拥有的前端触发特定事件。然而,出于某种原因,一些关键组合只是随机触发。发生这种情况时,我什至没有接触 arduino。

我已经设置好了,当你按下一个按钮时,它只触发一次击键,而不是在按下按钮时重复它。

我错过了什么,或者搞砸了什么?

int escButton = 12, valEsc, valEsc2, stateEsc;
int volUp = 11, valVup, valVup2, stateVup;
int volDown = 10, valVdown, valVdown2, stateVdown;
int rebootButton = 9, valReboot, valReboot2, stateReboot;

char ctrlKey = KEY_LEFT_GUI;
char shiftKey = KEY_LEFT_SHIFT;

void pressKeyCombos(int combo){
switch(combo) {
case 1:
Keyboard.press(KEY_ESC);
delay(100);
Keyboard.releaseAll();
break;

case 2:
Keyboard.press(shiftKey);
Keyboard.press(KEY_F1);
delay(100);
Keyboard.releaseAll();
break;

case 3:
Keyboard.press(shiftKey);
Keyboard.press(KEY_F3);
delay(100);
Keyboard.releaseAll();
break;

case 4:
Keyboard.press(shiftKey);
Keyboard.press(KEY_F2);
delay(100);
Keyboard.releaseAll();
break;
}
}

void setup() {
pinMode(escButton, INPUT);
stateEsc = digitalRead(escButton);
Keyboard.begin();
}

void loop(){
valEsc = digitalRead(escButton);
delay(10);
valEsc2 = digitalRead(escButton);

valVup = digitalRead(volUp);
delay(10);
valVup2 = digitalRead(volUp);

valVdown = digitalRead(volDown);
delay(10);
valVdown2 = digitalRead(volDown);

valReboot = digitalRead(rebootButton);
delay(10);
valReboot2 = digitalRead(rebootButton);

if (valVup == valVup2) {
if (valVup != stateVup) {
if (valVup == LOW) {
pressKeyCombos(4);
}
}
stateVup = valVup;
}

if (valVdown == valVdown2) {
if (valVdown != stateVdown) {
if (valVdown == LOW) {
pressKeyCombos(3);
}
}
stateVdown = valVdown;
}

if (valReboot == valReboot2) {
if (valReboot != stateReboot) {
if (valReboot == LOW) {
pressKeyCombos(2);
}
}
stateReboot = valReboot;
}

if (valEsc == valEsc2) {
if (valEsc != stateEsc) {
if (valEsc == LOW) {
pressKeyCombos(1);
}
}
stateEsc = valEsc;
}
}

最佳答案

默认情况下,输入为高阻抗。根据您对行为的描述,我可以猜测您没有添加外部上拉/下拉电阻并且引脚连接到常开开关。随机输入将与预期的一样,并在 digitalRead() 的文档中注明:

Note

If the pin isn't connected to anything, digitalRead() can return
either HIGH or LOW (and this can change randomly).

see reference page for digitialRead()



微型确实有能力提供内部上拉。
您的代码未显示您已启用内部上拉电阻。这是通过写入输入引脚以一种看起来很奇怪的方式完成的。来自 Arduino 文档:

There are also convenient 20K pullup resistors built into the Atmega chip that can be accessed from software. These built-in pullup resistors are accessed in the following manner.

pinMode(pin, INPUT); // set pin to input
digitalWrite(pin, HIGH); // turn on pullup resistors

see Digital Pins



因此,在您的情况下,在 setup() 中添加四个 digitalWrite() 以启用上拉,您看起来不错。

关于Arduino Micro 无接触触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14174162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com