Arduino 开关盒,需要数组循环帮助

Arduino Switch case, array loop help needed

我是 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;
  }
}

我认为这应该可以解决问题:

#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;
}