kuroの覚え書き

96の個人的覚え書き

Arduino Pro micro その3 ATCG キーボード

さて、1キーのキーボードができたら今度はそれを複数にしていきたいわな。

ということで4キーに拡張してみた。

#include "Keyboard.h"

const int buttonPinA = 6;          // input pin for pushbutton
int previousButtonStateA = HIGH;   // for checking the state of a pushButton
const int buttonPinG = 7;          // input pin for pushbutton
int previousButtonStateG = HIGH;   // for checking the state of a pushButton
const int buttonPinC = 8;          // input pin for pushbutton
int previousButtonStateC = HIGH;   // for checking the state of a pushButton
const int buttonPinT = 9;          // input pin for pushbutton
int previousButtonStateT = HIGH;   // for checking the state of a pushButton


void setup() {
  // make the pushButton pin an input:
  pinMode(buttonPinA, INPUT_PULLUP);
  pinMode(buttonPinT, INPUT_PULLUP);
  pinMode(buttonPinC, INPUT_PULLUP);
  pinMode(buttonPinG, INPUT_PULLUP);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  // read the pushbutton:
  int buttonStateA = digitalRead(buttonPinA);
  // if the button state has changed,
  if ((buttonStateA != previousButtonStateA)
      // and it's currently pressed:
      && (buttonStateA == HIGH)) {
    // type out a message
    Keyboard.print("A");
  }
  // read the pushbutton:
  int buttonStateT = digitalRead(buttonPinT);
  // if the button state has changed,
  if ((buttonStateT != previousButtonStateT)
      // and it's currently pressed:
      && (buttonStateT == HIGH)) {
    // type out a message
    Keyboard.print("T");
      }
  // read the pushbutton:
  int buttonStateC = digitalRead(buttonPinC);
  // if the button state has changed,
  if ((buttonStateC != previousButtonStateC)
      // and it's currently pressed:
      && (buttonStateC == HIGH)) {
    // type out a message
    Keyboard.print("C");
      }
  // read the pushbutton:
  int buttonStateG = digitalRead(buttonPinG);
  // if the button state has changed,
  if ((buttonStateG != previousButtonStateG)
      // and it's currently pressed:
      && (buttonStateG == HIGH)) {
    // type out a message
    Keyboard.print("G");
  }
  // save the current button state for comparison next time:
  previousButtonStateA = buttonStateA;
  previousButtonStateT = buttonStateT;
  previousButtonStateC = buttonStateC;
  previousButtonStateG = buttonStateG;
}

単純に4キー分を並べただけ。

f:id:k-kuro:20200830002441j:plain

キーボード用のスイッチを使ったところ結構チャタリングするのでdelay入れたほうが良さそう。

これで遺伝子のコードは書き放題だね。