LoginSignup
5
5

More than 5 years have passed since last update.

Ableton Live等で使えるスイッチコントローラ

Posted at

スイッチを押すと、あらかじめ決められたCCをMIDI出力するものです。
scene launch,stop clips,scene up,scene down 等にアサインして使います。
前にも作ったんだけど、数が必要になったのでまた作ってみました。

あ、Duemilanoveでやるので、USB MIDI出力は今回は考えません。

sw5_to_MIDI.ino
//5 MIDI CC switches. 

//The circuit:
// * switch(momentary) 1-5 connected to digital-in 3-7 and ground.
// * digital in 1 connected to MIDI jack pin 5
// * MIDI jack pin 2 connected to ground
// * MIDI jack pin 4 connected to +5V through 220-ohm resistor
// Attach a MIDI cable to the jack.

#include <Bounce2.h>
//
const int pushed = 0;  //for push-on switches.
//const int pushed = 1; //Otherwise,use this for push -off switches.
enum { nd = 5 };  //number of digital switches.
const int msg = 176;  //controll change. note-on:144 
const long debTime = 5;  //debounce time. Increase if the output flickers.

const int ledPin = 13;

Bounce debouncer[nd] ;  //make Bounce object's array(but not yet instantiate each element here).
const int val1[nd] = {70,71,72,73,74};  //controll No or note No
int val2[nd];  //controll value or velocity

void setup()
{
  const int inPin[nd] = {3,4,5,6,7};  //input pins for switches.
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW);
  for(int i=0 ; i < nd ; i++ )
  {
    debouncer[i] = Bounce();  //Instantiates a Bounce object.
    pinMode(inPin[i] , INPUT_PULLUP);
    debouncer[i].attach(inPin[i]);  // Set pin the instance uses.
    debouncer[i].interval(debTime);  //Set debounce time.
    debouncer[i].update();
    val2[i] = isPushed( debouncer[i].read() );
  } 
  Serial.begin(31250);
}

void loop()
{
  int reading;
  for(int i = 0; i < nd; i++)
  {
    debouncer[i].update();
    reading = isPushed( debouncer[i].read() );    
    if(reading != val2[i])
    {
      val2[i] = reading;
      MIDI_TX(msg, val1[i], val2[i]);
      digitalWrite( ledPin, val2[i] / 127 );
    }
  }
}

int isPushed(int p)
{
  return p == pushed ? 127 : 0;
}

void MIDI_TX(int MESSAGE, int VAL1, int VAL2) 
{
  Serial.write(MESSAGE);
  Serial.write(VAL1);
  Serial.write(VAL2);
}

Bounce2使ってみた

スイッチのチャッタリング防止に、以前に作ったときは自前でdebounceを仕込んだのですが、今回Bounce2を使ってみました。Bounceオブジェクトというのが当初ピンとこなくてスルーしていたんですが、ピン番号と時間をセットしたらデバウンスした値を都合良く返してくれる、なんかそういう もの だと思ったら、使えそうな気がしてきました。

#include <Bounce2.h> //ライブラリを読み込む
...
Bounce debouncer = Bounce();    //Bounceオブジェクトのインスタンスを作る
pinMode(pin_number,INPUT_PULLUP);   //使用したいArduinoのピンを入力に設定
debouncer.attach(pin_number);  // Bounceオブジェクトのインスタンスに、使用するピンをセット
debouncer.interval(debTime);  //デバウンスする時間をセット
...
debouncer.update(); //値を読む直前にBounceオブジェクトのインスタンスをアップデートする
swVal = debouncer.read; //Bounceオブジェクトのインスタンスの値を読み、変数に代入

変数とかをforループで宣言するのアリ?

Bounce debouncer[5] = {Bounce(),Bounce(),Bounce(),Bounce(),Bounce()}とか書くのが嫌だっただけですが。
ばらしてforループでまわしてみたらうまくいったようです。

定数の宣言?

そんなに凄いことはしないので#defineでいいんでしょうが。
いろんな意見があるので自分の方針。

  • なるべくconstを使う
  • int型で、後で配列の宣言やswitch文で使用するものは、enumを使う

でどうでしょう?

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5