AtsEXを使用してスイッチで扉の開閉を行おう２（CH552編）

__AtsEXを使用してスイッチで扉の開閉を行おう__ の続きとしてCH552でドア開閉を行ってみましょう。

https://qiita.com/usashirou/items/afb9f55f9d35a01e3ea0
# CH552とは
__CH552__ は約1ドル程度で購入可能な格安マイコンボードになります。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/285344/5663d10c-06d8-5f2f-df00-56d4e6f4a6a0.png)
__CH552__ は __Arduino__ での開発も可能です。
ただし、書込み回数に制限があり __100回程度__ で書込みできなくなるので注意してください。
また、ピン数についても、前回使用した __Raspberry Pi Pico__ に比べると少ないです。
今回は、このCH552ボードを使って、ATSexのスイッチを作ってみましょう。

__CH55x系ボード WeActStudio CH552CoreBoardをArduinoで使う__

https://qiita.com/usashirou/items/7513c13dd66c149cca53
# HID キーボードを作る
最初にHIDキーボードを試しましょう。
__ファイル⇒スケッチ例⇒Generic_Example⇒05USB⇒HidKeyboard__
をコンパイルしましょう。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/285344/1db8f5db-cbfc-b91d-5832-b5935be9b6c8.png)
# 設定
__ツール⇒USB Settingsを変更します。__
USBコードのサイズを __0B以上__ を選択します。
これは、文字列を入力するプログラムのためです。
今回は __USB CODE w/256B USB ram__ を選択しました。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/285344/9f3610b5-82c8-6f78-721e-5605040b98e1.png)
# プログラム
上記プログラムをもとにほかのポートを追加しました。
今回は、 __Keyboard_press,Keyboard_release__ で構成されています。
前回のように、 __write,press,release__ ではないので、わかりやすいプログラムになっていると思います。

```
/*
  HID Keyboard example
  created 2020
  by Deqing Sun for use with CH55xduino
  This example code is in the public domain.
  cli board options: usb_settings=user148
*/

//For windows user, if you ever played with other HID device with the same PID C55D
//You may need to uninstall the previous driver completely

#ifndef USER_USB_RAM
#error "This example needs to be compiled with a USER USB setting"
#endif

#include "src/userUsbHidKeyboard/USBHIDKeyboard.h"

#define BUTTON0_PIN 10
#define BUTTON1_PIN 11

#define BUTTON4_PIN 14
#define BUTTON5_PIN 15
#define BUTTON6_PIN 16
#define BUTTON7_PIN 17

#define LED_BUILTIN 30
#define BUTTON31_PIN 31
#define BUTTON32_PIN 32
#define BUTTON33_PIN 33
#define BUTTON34_PIN 34
#define BUTTON35_PIN 35

bool button0PressPrev = false;
bool button1PressPrev = false;
bool button4PressPrev = false;
bool button5PressPrev = false;
bool button6PressPrev = false;
bool button7PressPrev = false;
bool button31PressPrev = false;
bool button32PressPrev = false;
bool button33PressPrev = false;
bool button34PressPrev = false;
bool button35PressPrev = false;

void setup() {
  USBInit();
  pinMode(BUTTON0_PIN, INPUT_PULLUP);
  pinMode(BUTTON1_PIN, INPUT_PULLUP);
  pinMode(BUTTON4_PIN, INPUT_PULLUP);
  pinMode(BUTTON5_PIN, INPUT_PULLUP);
  pinMode(BUTTON6_PIN, INPUT_PULLUP);
  pinMode(BUTTON7_PIN, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(BUTTON31_PIN, INPUT_PULLUP);
  pinMode(BUTTON32_PIN, INPUT_PULLUP);
  pinMode(BUTTON33_PIN, INPUT_PULLUP);
  pinMode(BUTTON34_PIN, INPUT_PULLUP);
  pinMode(BUTTON35_PIN, INPUT_PULLUP);
}

void loop() {
  //button 0 is mapped to letter 'w'
  bool button0Press = !digitalRead(BUTTON0_PIN);
  if (button0PressPrev != button0Press) {
    button0PressPrev = button0Press;
    if (button0Press) {
      Keyboard_press('e');
    } else {
      Keyboard_release('e');
    }
  }


  //button 1 is mapped to letter 'e'
  bool button1Press = !digitalRead(BUTTON1_PIN);
  if (button1PressPrev != button1Press) {
    button1PressPrev = button1Press;
    if (button1Press) {
      Keyboard_press('r');
    } else {
      Keyboard_release('r');
    }
  }

  //button 4 is mapped to letter 'r'
  bool button4Press = !digitalRead(BUTTON4_PIN);
  if (button4PressPrev != button4Press) {
    button4PressPrev = button4Press;
    if (button4Press) {
      Keyboard_press('t');
    } else {
      Keyboard_release('t');
    }
  }

  //button 5 is mapped to Capslock
  bool button5Press = !digitalRead(BUTTON5_PIN);
  if (button5PressPrev != button5Press) {
    button5PressPrev = button5Press;
    if (button5Press) {
      Keyboard_press(KEY_RETURN);
    } else {
      Keyboard_release(KEY_RETURN);
    }
  }

  //button 6 is mapped to Capslock
  bool button6Press = !digitalRead(BUTTON6_PIN);
  if (button6PressPrev != button6Press) {
    button6PressPrev = button6Press;
    if (button6Press) {
      Keyboard_press(0xDE);
    } else {
      Keyboard_release(0xDE);
    }
  }

  //button 7 is mapped to Capslock
  bool button7Press = !digitalRead(BUTTON7_PIN);
  if (button7PressPrev != button7Press) {
    button7PressPrev = button7Press;
    if (button7Press) {
      Keyboard_press(0xDF);
    } else {
      Keyboard_release(0xDF);
    }
  }


  //button 31 is mapped to letter 's'
  bool button31Press = !digitalRead(BUTTON31_PIN);
  if (button31PressPrev != button31Press) {
    button31PressPrev = button31Press;
    if (button31Press) {
      Keyboard_press('o');
    } else {
      Keyboard_release('o');
    }
  }

  //button 32 is mapped to letter 'd'
  bool button32Press = !digitalRead(BUTTON32_PIN);
  if (button32PressPrev != button32Press) {
    button32PressPrev = button32Press;
    if (button32Press) {
      Keyboard_press('i');
    } else {
      Keyboard_release('i');
    }
  }

  //button 33 is mapped to letter 'd'
  bool button33Press = !digitalRead(BUTTON33_PIN);
  if (button33PressPrev != button33Press) {
    button33PressPrev = button33Press;
    if (button33Press) {
      Keyboard_press('u');
    } else {
      Keyboard_release('u');
    }
  }

  //button 34 is mapped to letter 'd'
  bool button34Press = !digitalRead(BUTTON34_PIN);
  if (button34PressPrev != button34Press) {
    button34PressPrev = button34Press;
    if (button34Press) {
      Keyboard_press('g');
    } else {
      Keyboard_release('g');
    }
  }

  //button 35 is mapped to letter 'd'
  bool button35Press = !digitalRead(BUTTON35_PIN);
  if (button35PressPrev != button35Press) {
    button35PressPrev = button35Press;
    if (button35Press) {
      Keyboard_press('h');
    } else {
      Keyboard_release('h');
    }
  }

  //map capsLock to LED
  //Bit 0: NUM lock, Bit 1: CAPS lock, Bit 2: SCROLL lock, Bit 3: Compose, Bit 4: Kana,
  if (Keyboard_getLEDStatus() & 2) {
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    digitalWrite(LED_BUILTIN, LOW);
  }

  delay(50);  //naive debouncing
}
```

# 動作の様子
<blockquote class="twitter-tweet" data-media-max-width="560"><p lang="ja" dir="ltr">BVE CH552でドア開閉をする<br>AtsEX <a href="https://t.co/lvc8Gqjs2R">pic.twitter.com/lvc8Gqjs2R</a></p>&mdash; usashirou (@usashirou1) <a href="https://twitter.com/usashirou1/status/1744007962480537814?ref_src=twsrc%5Etfw">January 7, 2024</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>