include

define BTN_S 2

define BTN_1 3

define BTN_2 4

define BTN_3 5

// 前回の状態を保持する変数
bool lastStateS = HIGH;
bool lastState1 = HIGH;
bool lastState2 = HIGH;
bool lastState3 = HIGH;

void setup() {
pinMode(BTN_S, INPUT_PULLUP);
pinMode(BTN_1, INPUT_PULLUP);
pinMode(BTN_2, INPUT_PULLUP);
pinMode(BTN_3, INPUT_PULLUP);

Keyboard.begin();
}

void loop() {
// 各ボタンの現在の状態を取得
bool currentS = digitalRead(BTN_S);
bool current1 = digitalRead(BTN_1);
bool current2 = digitalRead(BTN_2);
bool current3 = digitalRead(BTN_3);

// ボタンが離されたとき(前回LOW → 今回HIGH)
if (lastStateS == LOW && currentS == HIGH) {
Keyboard.press(‘s’);
delay(10);
Keyboard.release(‘s’);
}
if (lastState1 == LOW && current1 == HIGH) {
Keyboard.press(‘1’);
delay(10);
Keyboard.release(‘1’);
}
if (lastState2 == LOW && current2 == HIGH) {
Keyboard.press(‘2’);
delay(10);
Keyboard.release(‘2’);
}
if (lastState3 == LOW && current3 == HIGH) {
Keyboard.press(‘3’);
delay(10);
Keyboard.release(‘3’);
}

// 前回の状態を更新
lastStateS = currentS;
lastState1 = current1;
lastState2 = current2;
lastState3 = current3;

delay(5); // チャタリング対策&CPU負荷軽減
}

Follow me!

音楽

前の記事

恋はポッピン