Created
December 4, 2023 12:55
-
-
Save RyoKosaka/32dac530ebd2bbb90a8422011805d1ae to your computer and use it in GitHub Desktop.
プロダクトデザイン応用実習サンプルコード - Bluetooth経由でMIDIを受けてサーボを動かす
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// プロダクトデザイン応用実習サンプルコード - Bluetooth経由でMIDIを受けてサーボを動かす | |
#include <ESP32Servo.h> //ESP32でサーボを扱うライブライを使いますという宣言 | |
Servo myservo; // 扱うサーボにmyservoと名前をつける | |
// BLE経由でMIDIを扱うライブラリ「BLE-MIDI」を使いますという宣言 | |
#include <BLEMIDI_Transport.h> | |
#include <hardware/BLEMIDI_ESP32.h> | |
BLEMIDI_CREATE_DEFAULT_INSTANCE() | |
void setup() | |
{ | |
// MIDIの初期設定と受け付けるチャンネルの指定 | |
MIDI.begin(1); // チャンネルが1のMIDIだけを受け付ける | |
// サーボを22番ピンに接続するという宣言 | |
myservo.attach(22); | |
} | |
void loop() | |
{ | |
// 送られてきたMIDIの情報を格納する変数を用意する | |
int controlNumber; // コントロール番号 | |
int controlValue; // コントールバリュー | |
// MIDIが送られてきたときの動きを定義するif文 | |
if (MIDI.read() == true) | |
{ | |
// swich case文でMIDIの中身を読んで場合わけする | |
switch (MIDI.getType()) | |
{ | |
case midi::ControlChange: // MIDIの中身がコントロールチェンジだったとき | |
controlNumber = MIDI.getData1(); // 変数controlNumberにコントロール番号を格納 | |
controlValue = MIDI.getData2(); // 変数controlValueにコントロールバリューを格納 | |
if (controlNumber == 2) | |
{ | |
controlValue = map(controlValue, 0, 127, 0, 180); // 0から127を0から180に変換する | |
myservo.write(controlValue); // サーボを動かす | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment