我正在使用 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;
}
}