Arduino/Midi/Controlleur

De Wiki LOGre
Aller à : navigation, rechercher


Voici un exemple de controlleur MIDI simplifie a base d'Arduino et d'encodeur digital

Plus d'infos sur le MIDI et Arduino ici et la

Le sketch :

/*
  Simple MIDI controller with an arduino and a digital encoder
  Any changing encoder position is sent as a control change to the specified channel
  Arduino pins TX, GND and 5V (through an 220 ohms resistor) must be connected to 
  pins 5, 2 and 4 of a standard MIDI cable
  See here :
  http://www.tigoe.com/pcomp/code/communication/midi/
  http://itp.nyu.edu/physcomp/Labs/MIDIOutput
 
  This code is GPL
 */
 
//#define USE_ASCII    /* uncomment this to only print the encoder value as ASCII to the serial monitor */
 
int pinA = 2;    // these two pins are
int pinB = 3;    // connected to the encoder directly
int cmd = 0xB0;  // control change command for channel 1
int ctl = 3;     // controller 3
 
void setup() {                
  pinMode(pinA, INPUT);
  pinMode(pinB, INPUT);
#ifdef USE_ASCII
  Serial.begin( 9600);
#else
  Serial.begin( 31250);
#endif
}
 
int pa = -1, pb = -1;
int pos = 0, ppos = 0;    // this is the absolute encoder position, between 0 and 128
void loop() {
  int a,b;
  digitalWrite( pinA, 1);
  a = digitalRead( pinA);
  digitalWrite( pinB, 1);
  b = digitalRead( pinB);
  if (pa == -1)
    pa = a;
  if (pb == -1)
    pb = b;
  if ((!pa&&pb&&!a&&!b) ||
    (pa&&pb&&!a&&b) ||
    (!pa&&!pb&&a&&!b) ||
    (pa&&!pb&&a&&b))
    if (pos > 0)
      pos--;
  if ((pa&&!pb&&!a&&!b) ||
    (!pa&&!pb&&!a&&b) ||
    (pa&&pb&&a&&!b) ||
    (!pa&&pb&&a&&b))
    if (pos < 128)
      pos++;
  if (ppos != pos)
  {
#ifdef USE_ASCII
    Serial.println(pos);
#else
    Serial.print(cmd,BYTE);
    Serial.print(ctl,BYTE);
    Serial.print(pos,BYTE);
#endif
  }
  ppos = pos;
  pa = a;
  pb = b;
  delay(1);              // wait for 1 millisecond
}