Arduinoでメカニカルキーを使いたかったら「M5Stack用メカニカルキーボタンユニット」と「GROVE - ベースシールド」がお勧め。はんだ付けなしですぐに使えます。





GROVE - ベースシールド — スイッチサイエンス
M5Stack用メカニカルキーボタンユニット — スイッチサイエンス


「Switch Science」から「GROVE - ベースシールド」を買うとすでにピンがはんだ付けされた状態になっているのですぐに使えます。

「M5Stack用メカニカルキーボタンユニット」にはLEDも付いています。

レゴブロック (LEGO) と組み合わせ




「M5Stack用メカニカルキーボタンユニット」はレゴ (LEGO) ブロックで固定可能です。

レゴブロックは「ブリッカーズ」というサイトからバラで購入できました。
brickers.jp
使用したのは以下の通りです。

テクニック リフトアーム 1 x 11:[Red / レッド] × 2
テクニック リフトアーム 1 x 5:[Red / レッド] × 2
テクニック コネクターペグ - 滑り止め:[Black / ブラック] × 10



固定するためのものとして他にこのようなものもあるようです。
Grove - Wrapper 1 x 1 青(4個パック) — スイッチサイエンス
Grove - Wrapper 1 x 2 青(4個パック) — スイッチサイエンス

スケッチ

スケッチのサンプルです。

「M5Stack用メカニカルキーボタンユニット」がArduinoで問題なく使用できました。

#include <FastLED.h>

const int buttonON = LOW;    // ボタンが押されているとピンの値はLOW

const int buttonPin1 = 2;
const int buttonPin2 = 7;
const int buttonPin3 = 4;

const int LED_PIN1 = 3;
const int LED_PIN2 = 8;
const int LED_PIN3 = 5;

CRGB LED1[1];
CRGB LED2[1];
CRGB LED3[1];

void setup() {
  Keyboard.begin();
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  FastLED.addLeds<SK6812, LED_PIN1, GRB>(LED1, 1);
  FastLED.addLeds<SK6812, LED_PIN2, GRB>(LED2, 1);
  FastLED.addLeds<SK6812, LED_PIN3, GRB>(LED3, 1);
  LED1[0] = CRGB::Black;
  LED2[0] = CRGB::Black;
  LED3[0] = CRGB::Black;
  FastLED.setBrightness(255);
}

void loop() {
  if (digitalRead(buttonPin1) == buttonON) {
    
    //action 1

    LED1[0] = CRGB::Red;
    FastLED.show();
    while (digitalRead(buttonPin1) == buttonON);
    LED1[0] = CRGB::Black;
    FastLED.show();
    delay(200);
  }
  if (digitalRead(buttonPin2) == buttonON) {
    
    //action 2

    LED2[0] = CRGB::Red;
    FastLED.show();
    while (digitalRead(buttonPin2) == buttonON);
    LED2[0] = CRGB::Black;
    FastLED.show();
    delay(200);
  }
  if (digitalRead(buttonPin3) == buttonON) {
    
    //action 3
    
    LED3[0] = CRGB::Red;
    FastLED.show();
    while (digitalRead(buttonPin3) == buttonON);
    LED3[0] = CRGB::Black;
    FastLED.show();
    delay(200);
  }
}




このエントリーをはてなブックマークに追加