Arduino Switch案例,需要阵列循环帮助

时间:2015-02-14 19:49:23

标签: arduino

我是Arduino和编程的新手,我正在尝试制作一个带有4个瞬时开关的midi控制器......但是我只能通过INPUT2发送MIDI CC消息。任何帮助都将受到赞赏......

#include <MIDI.h>

int buttonPin[] = {2,3,4,5};
boolean currentState = LOW;
boolean lastState = LOW;
byte mode = 0;



void setup (){
MIDI.begin(4);
Serial.begin(115200);
int i;
for (i = 0; i < 4; i++){
pinMode(buttonPin[i], INPUT);
}
Serial.begin(115200);
}

void loop () 
{
int i;
currentState = digitalRead(buttonPin[i]);
  switch (mode)
  {
    case 0:
    if ( currentState == HIGH )
     { MIDI.sendControlChange (i+1,127,2);
  
  mode =1;
     }
   break;
  case 1:
 if (  currentState == LOW)
mode = 2;
break;
case 2:
if ( currentState ==HIGH )
 { MIDI.sendControlChange (i+1,0,2);
 
mode =3;
 }
 break;
case 3:
if ( currentState == LOW )
mode =0;
break;
  }
}

1 个答案:

答案 0 :(得分:0)

这应该是我认为的伎俩:

#include <MIDI.h>

int buttonPin[] = {2, 3, 4, 5};
byte     mode[] = {0, 0, 0, 0};

boolean currentState = LOW;
boolean lastState    = LOW;

void setup (){
    MIDI.begin(4);
    Serial.begin(115200);

    int i;
    for (i = 0; i < 4; i++) {
        pinMode(buttonPin[i], INPUT);
    }
}

// This will track which pin/mode we're using
int i = 0;

void loop () {
    currentState = digitalRead(buttonPin[i]);

    switch (mode[i]) {
        case 0:
            if (currentState == HIGH) {
                MIDI.sendControlChange (i+1, 127, 2);
                mode[i] = 1;
            }
            break;
        case 1:
            if (currentState == LOW) mode[i] = 2;
            break;
        case 2:
            if (currentState == HIGH) {
                MIDI.sendControlChange (i+1, 0, 2);
                mode[i] = 3;
            }
            break;
        case 3:
            if (currentState == LOW) mode[i] = 0;
            break;
    }

    // Increment i and rollover if necessary
    if (++i > 3) i = 0;
}