 const int READ_SELECT_3 = 5;
 
 // pins for reading from multiplexers
-const int READ_MUX_1 = 22;
-const int READ_MUX_2 = 23; // actually there's only 1 at the moment
+const int READ_MUX_PINS[] = {22, 23, 24, 25, 26, 27, 28, 29};
 
 // pins for selecting demux channel for writing
 const int WRITE_SELECT_1 = 10;

  pinMode(READ_SELECT_1,OUTPUT);
  pinMode(READ_SELECT_2,OUTPUT);
  pinMode(READ_SELECT_3,OUTPUT);
  pinMode(READ_MUX_1,INPUT);
  pinMode(READ_MUX_2,INPUT);
  pinMode(WRITE_SELECT_1,OUTPUT);
  pinMode(WRITE_SELECT_2,OUTPUT);
  pinMode(WRITE_SELECT_3,OUTPUT);

bool gate = false;

void loop() {
  int i,j,k,m;
  
  // supply 5V to each "write" demultiplexer in turn
  for(i=0;i<2;i++) {
    digitalWrite(DEMUX_SELECT_1,bitRead(i,0));
    digitalWrite(DEMUX_SELECT_2,bitRead(i,1));
    digitalWrite(DEMUX_SELECT_3,bitRead(i,2));
    
    // supply 5V to each pin in turn
    for(j=0;j<8;j++) {
      digitalWrite(WRITE_SELECT_1,bitRead(j,0));
      digitalWrite(WRITE_SELECT_2,bitRead(j,1));
      digitalWrite(WRITE_SELECT_3,bitRead(j,2));
      
      // select "read" multiplexer pins in turn (for all multiplexers simultaneously)
      for(k=0;k<8;k++) {
        digitalWrite(READ_SELECT_1,bitRead(k,0));
        digitalWrite(READ_SELECT_2,bitRead(k,1));
        digitalWrite(READ_SELECT_3,bitRead(k,2));
        
        // read from each multiplexer in turn
        for(m=0;m<2;m++) {
          if(i*8+j<m*8+k) {
            if(digitalRead(READ_MUX_PINS[m])==HIGH) {
              Serial.print(i*8 + j);
              Serial.print("-");
              Serial.print(m*8 + k);
              Serial.print("\n");
            }
          }
        }
      }
    }
  }
  
  //checkMidi();
  for(byte i = 0; i < 0; i ++) {
    checkMidi();
