1. #include "BluetoothSerial.h"
  2. BluetoothSerial SerialBT;
  3. String MACadd = "28:CD:**:**:51:B6";//カート側マイコンのMACアドレスを記入
  4. uint8_t address[6] = {0x28, 0xCD, 0x**, 0x**, 0x51, 0xB6};
  5. bool connected;
  6. #define modeSW 16 // AutoStop/ManualStop
  7. char key;
  8. int modeSWState = 1;
  9. const byte ROWS = 4; //four rows
  10. const byte COLS = 4; //four columns
  11. char keys[ROWS][COLS] = {
  12.   {'1', '2', '3', 'A'},
  13.   {'4', '5', '6', 'B'},
  14.   {'7', '8', '9', 'C'},
  15.   {'*', '0', '#', 'D'}
  16. };
  17. byte rowPins[ROWS] = {13, 12, 14, 27}; //connect to the row pinouts of the keypad
  18. byte colPins[COLS] = {26, 25, 33, 32}; //connect to the column pinouts of the keypadchar key;
  19. void setup() {
  20.   Serial.begin(115200);
  21.   
  22.   SerialBT.begin("MWC_BT", true);
  23.   Serial.println("device start");
  24.   connected = SerialBT.connect(address);
  25.   if (connected) {
  26.     Serial.println("Connect OK");
  27.   } else {
  28.     while (!SerialBT.connected(10000)) {
  29.       Serial.println("No connect");
  30.     }
  31.   }
  32.   if (SerialBT.disconnect()) {
  33.     Serial.println("Disconnected Succesfully!");
  34.   }
  35.   SerialBT.connect();
  36.   for (byte i = 0; i < ROWS; i++) {
  37.     pinMode(rowPins[i], INPUT_PULLUP);
  38.   }
  39.   for (byte i = 0; i < COLS; i++) {
  40.     pinMode(colPins[i], OUTPUT);
  41.   }
  42.   pinMode(modeSW, INPUT_PULLUP);
  43. }
  44. void loop() {
  45.   modeSWState = digitalRead(modeSW);
  46.   if (modeSWState == HIGH) {
  47.     key = getKey();
  48.     if (key) {
  49.     } else {
  50.       key = 'S';
  51.     }
  52.     Serial.println(key);
  53.     SerialBT.write(key);
  54.   } else{
  55.     key = getKey();
  56.     if (key) {
  57.       Serial.println(key);
  58.       SerialBT.write(key);
  59.     }
  60.   }
  61.   delay(20);
  62. }
  63. char getKey() {
  64.   for (byte c = 0; c < COLS; c++) {
  65.     digitalWrite(colPins[c], LOW);
  66.     for (byte r = 0; r < ROWS; r++) {
  67.       if (digitalRead(rowPins[r]) == LOW) {
  68.         digitalWrite(colPins[c], HIGH);
  69.         delay(50);
  70.         return keys[r][c];
  71.       }
  72.     }
  73.     digitalWrite(colPins[c], HIGH);
  74.   }
  75.   return 0;
  76. }