Skip to content

Instantly share code, notes, and snippets.

@RyoKosaka
Created December 4, 2023 12:53
Show Gist options
  • Save RyoKosaka/d53987ddbe026fd9fecbf1432322aca2 to your computer and use it in GitHub Desktop.
Save RyoKosaka/d53987ddbe026fd9fecbf1432322aca2 to your computer and use it in GitHub Desktop.
プロダクトデザイン応用実習サンプルコード - USB経由で単純なMIDI(ノートオンオフ)を受ける
// プロダクトデザイン応用実習サンプルコード - USB経由で単純なMIDI(ノートオンオフ)を受ける
// 参考 : switch case文 http://www.musashinodenpa.com/arduino/ref/index.php?f=0&pos=126
// USB経由でMIDIを扱うライブラリ「USB-MIDI」を使いますという宣言
#include <USB-MIDI.h>
USBMIDI_CREATE_DEFAULT_INSTANCE();
void setup()
{
// MIDIの初期設定と受け付けるチャンネルの指定
MIDI.begin(1); // チャンネルが1のMIDIだけを受け付ける
// 13番ピン(内蔵LED)を出力に使うという宣言
pinMode(13, OUTPUT);
}
void loop()
{
// 送られてきたMIDIの情報を格納する変数を用意する
int note; // ノート番号
int velocity; // ベロシティ(強さ)
// MIDIが送られてきたときの動きを定義するif文
if (MIDI.read() == true)
{
// swich case文でMIDIの中身を読んで場合わけする
switch (MIDI.getType())
{
case midi::NoteOn: // MIDIの中身がnoteOnだったとき
note = MIDI.getData1(); // 変数noteにノート番号を格納
velocity = MIDI.getData2(); // 変数velocityにベロシティを格納
if (note == 60)
{ // さらにif文でノート番号が60のときの動きを定義する
digitalWrite(13, HIGH);
}
break;
case midi::NoteOff: // MIDIの中身がnoteOffだったとき
note = MIDI.getData1(); // 変数noteにノート番号を格納
velocity = MIDI.getData2(); // 変数velocityにベロシティを格納
if (note == 60)
{
digitalWrite(13, LOW);
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment