MPG用HID KeyboardのSketch

コピーしてArduino IDEに貼り付けて下さい。
プログラムのプロではないので完全ではありません。間違いやアドバイスがあればお知らせ下さい。
このHID Keyboardの難点はKeyboard.pressの後、Keyboard.releaseが入るまでキーが押された状態が続くことです。
このため、PCのキーボードのように矢印キーで連続した送りが出来ませんが、連続させる場合は次のように変更します。
但し、STOPキーが押されるまで停止しないので使い勝手の良い方を選んでください。
X_UPの例ですが、X_DN、Y_UP、_Y_DN、Z_UP、Z_DNも同様に変更します。

変更前
  if (X_UP == LOW) { // X-axis Right
    Keyboard.press(KEY_RIGHT_ARROW);
    delay(300);//delay(100~300)
    Keyboard.release(KEY_RIGHT_ARROW);
    delay(1);
  }

変更後
  if (X_UP == LOW) { // X-axis Right
    Keyboard.press(KEY_RIGHT_ARROW);
  }

HID KeyboardのSketch
#include "Keyboard.h"
/* Arduino pro microのピン配置とKeySwitchの配置を最短結線を優先した為、
 KeySwitch名とピン番号がランダムになりました。*/
int TAB = 10;
int SHIFT = 16;
int Cycle_Start = 15;
int Reset = 20;
int Jog_Mode = 14;
int Cycle_Jog_Step = 6;
int Slow_Jog_UP =5;
int Slow_Jog_DN = 4;
int Zero_X = 9;
int Zero_Y = 8;
int Zero_Z = 7;
int X_UP = 2;
int X_DN = 19;
int Y_UP = 3;
int Y_DN = 21;
int Z_UP = 1;
int Z_DN = 0;
int STOP = 18;

void setup() {
  Keyboard.begin();
  pinMode(TAB, INPUT_PULLUP);
  pinMode(SHIFT, INPUT_PULLUP);
  pinMode(Cycle_Start, INPUT_PULLUP);
  pinMode(Reset, INPUT_PULLUP);
  pinMode(Jog_Mode, INPUT_PULLUP);
  pinMode(Cycle_Jog_Step, INPUT_PULLUP);
  pinMode(Slow_Jog_UP, INPUT_PULLUP);
  pinMode(Slow_Jog_DN, INPUT_PULLUP);
  pinMode(Zero_X, INPUT_PULLUP);
  pinMode(Zero_Y, INPUT_PULLUP);
  pinMode(Zero_Z, INPUT_PULLUP);
  pinMode(X_UP, INPUT_PULLUP);
  pinMode(X_DN, INPUT_PULLUP);
  pinMode(Y_UP, INPUT_PULLUP);
  pinMode(Y_DN, INPUT_PULLUP);
  pinMode(Z_UP, INPUT_PULLUP);
  pinMode(Z_DN, INPUT_PULLUP);
  pinMode(STOP, INPUT_PULLUP);
}

void loop() {
  TAB = digitalRead(10);
  SHIFT = digitalRead(16);
  Cycle_Start = digitalRead(15);
  Reset = digitalRead(20);
  Jog_Mode = digitalRead(14);
  Cycle_Jog_Step = digitalRead(6);
  Slow_Jog_UP = digitalRead(5);
  Slow_Jog_DN = digitalRead(4);
  Zero_X = digitalRead(9);
  Zero_Y = digitalRead(8);
  Zero_Z = digitalRead(7);
  X_UP = digitalRead(2);
  X_DN = digitalRead(19);
  Y_UP = digitalRead(3);
  Y_DN = digitalRead(21);
  Z_UP = digitalRead(1);
  Z_DN = digitalRead(0);
  STOP = digitalRead(18);

  if (TAB == LOW) { // TAB-KEY
    (KEY_TAB);
    delay(100);
    Keyboard.releaseAll();
    delay(200);
  }
  if (Cycle_Start == LOW) {  // Cycle_Start
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('r');
    delay(100);
    Keyboard.releaseAll();
    delay(100);
  }
  if (Reset == LOW) {  // RESET
    Keyboard.press('[');//何故か'['入力で'@'を出力(@が目的なのに文字化け)
    delay(100);
    Keyboard.releaseAll();
    delay(200);
  }
  if (Cycle_Jog_Step == LOW) {  // Cycle_Jog_Step/Velocity
    if (SHIFT == LOW) {  // Step/Velocity:OEM=304,Hotkey=34900
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_ALT);      
    }
    else {  // Cycle_Jog_Step:OEM=171,Hotkey=2132
      Keyboard.press(KEY_LEFT_ALT);
    }
    Keyboard.press('t');
    delay(100);
    Keyboard.releaseAll();
    delay(200);
  }
  if (Jog_Mode == LOW) {  // Jog_Mode/Velocity Onry
    if (SHIFT == LOW) {  // Velocity Onry:OEM=303,Hotkey=34893
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_ALT);
    }
    else {  // Jog Mode:OEM=245,Hotkey=2125
      Keyboard.press(KEY_LEFT_ALT);
    }
    Keyboard.press('m');
    delay(100);
    Keyboard.releaseAll();
    delay(200);
  }
  if (Slow_Jog_UP == LOW) {  // Slow_Jog_UP/Single Step
    if (SHIFT == LOW) {  // Single Step:OEM=305,Hotkey=34901
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_ALT);
    }
    else {  // Slow_Jog_UP:OEM=111,Hotkey=2133
      Keyboard.press(KEY_LEFT_ALT);
    }
    Keyboard.press('u');
    delay(100);
    Keyboard.releaseAll();
    delay(200);
  }
  if (Slow_Jog_DN == LOW) {
    if (SHIFT == LOW) {  // Slow_Jog_UP:OEM=306,Hotkey=34884
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_ALT);
    }
    else {  // Slow_Jog_UP:OEM=112,Hotkey=2116
      Keyboard.press(KEY_LEFT_ALT);
    }
    Keyboard.press('d');
    delay(100);
    Keyboard.releaseAll();
    delay(200);
  }
  if (Zero_X == LOW) { // Select X-axis for MPG 1/Zero-X-axis
    if (SHIFT == LOW) { // Zero_X-axis 
      Keyboard.press(KEY_LEFT_ALT);
    }
    else { // Select X-axis for MPG 1
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press(KEY_LEFT_CTRL);
    }
    Keyboard.press('x');
    delay(100);
    Keyboard.releaseAll();
    delay(200);
  }
  if (Zero_Y == LOW) { // Select Y-axis for MPG 1/Zero_Y-axis
    if (SHIFT == LOW) { // Zero_Y-axis
      Keyboard.press(KEY_LEFT_ALT);
    }
    else { // Select Y-axis for MPG 1
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press(KEY_LEFT_CTRL);
    }
    Keyboard.press('y');
    delay(100);
    Keyboard.releaseAll();
    delay(200);
  }
  if (Zero_Z == LOW) { // Select Z-axis for MPG 1/Zero_Z-axis
    if (SHIFT == LOW) { // Zero_Z-axis
      Keyboard.press(KEY_LEFT_ALT);
    }
    else { // Select Z-axis for MPG 1
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press(KEY_LEFT_CTRL);
    }
    Keyboard.press('z');
    delay(100);
    Keyboard.releaseAll();
    delay(200);
  }
  if (X_UP == LOW) { // X-axis Right
    Keyboard.press(KEY_RIGHT_ARROW);
    delay(300);//delay(100~300)
    Keyboard.release(KEY_RIGHT_ARROW);
    delay(1);
  }
  if (X_DN == LOW) { // X-axis Left
    Keyboard.press(KEY_LEFT_ARROW);
    delay(300);//delay(100~300)
    Keyboard.release(KEY_LEFT_ARROW);
    delay(1);
  }
  if (Y_UP == LOW) { // Y-axis Forward
    Keyboard.press(KEY_UP_ARROW);
    delay(300);//delay(100~300)
    Keyboard.release(KEY_UP_ARROW);
    delay(1);
  }
  if (Y_DN == LOW) { // Y-axis BackForward
    Keyboard.press(KEY_DOWN_ARROW);
    delay(300);//delay(100~300)
    Keyboard.release(KEY_DOWN_ARROW);
    delay(1);
  }
  if (Z_UP == LOW) { // Z-axis UP
    Keyboard.press(KEY_PAGE_UP);
    delay(300);//delay(100~300)
    Keyboard.release(KEY_PAGE_UP);
    delay(1);
  }
  if (Z_DN == LOW) { // Z-axis Down/Probe
    if (SHIFT == LOW) { // Probe to Z-axis :OEM=707,Hotkey=32847
      Keyboard.press(KEY_LEFT_CTRL);    
      Keyboard.press('o');
    }
    else { // Probe to Z-axis
    Keyboard.press(KEY_PAGE_DOWN);
    }
    delay(300);
    Keyboard.releaseAll();
    delay(1);
  }
  if (STOP == LOW) { // STOP/ Goto Zero
    if (SHIFT == LOW) { // Goto Zero
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_ALT);
    }
    else { // STOP
      Keyboard.press(KEY_LEFT_ALT);
    }
    Keyboard.press('s');
    delay(100);
    Keyboard.releaseAll();
    delay(200);
  }
}
inserted by FC2 system