I cut out the analogRead(s0Pin... and added the lines for reading the analog pins. I also added the lines for mux 2 and 3.
//Give convenient names to the control pins
#define CONTROL0 5 //S3
#define CONTROL1 4 //S2
#define CONTROL2 3 //S1
#define CONTROL3 2 //S0
//create temporary variables for analog input
int tempmux0;
int tempmux1;
int tempmux2;
//create char look up tables for MIDI CC
char control0[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
char control1[] = { 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32 };
char control2[] = { 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48 };
//create variables which hold the pinnumbers the three muxs send their data to
int COM1 = 0;
int COM2 = 1;
int COM3 = 2;
//Create arrays for data from the the MUXs
int mux0array[16];
int mux1array[16];
int mux2array[16];
void setup()
{
//Set MUX control pins to output
pinMode(CONTROL0, OUTPUT);
pinMode(CONTROL1, OUTPUT);
pinMode(CONTROL2, OUTPUT);
pinMode(CONTROL3, OUTPUT);
//Default speed of MIDI serial port
Serial.begin(31250);
}
void loop()
{
//This for loop is used to scroll through and store the 16 inputs on the FIRST multiplexer
for (int i=0; i<16; i++)
{
//The following 4 commands set the correct logic for the control pins to select the desired input
digitalWrite(CONTROL0, (i&15)>>3);
digitalWrite(CONTROL1, (i&7)>>2);
digitalWrite(CONTROL2, (i&3)>>1);
digitalWrite(CONTROL3, (i&1));
tempmux0 = analogRead(COM1)/8; //divide by 8 to get in the range of 1-127
if( abs(mux0array[i] - tempmux0) > 2) {
mux0array[i] = tempmux0;
MidiTX(char(0xB0), control0[i], char(mux0array[i]) ); //control[i] is a char look up table for the controller number to change
}
}
//This for loop is used to scroll through and store the 16 inputs on the SECOND multiplexer
for (int i=0; i<16; i++)
{
//The following 4 commands set the correct logic for the control pins to select the desired input
digitalWrite(CONTROL0, (i&15)>>3);
digitalWrite(CONTROL1, (i&7)>>2);
digitalWrite(CONTROL2, (i&3)>>1);
digitalWrite(CONTROL3, (i&1));
tempmux1 = analogRead(COM2)/8; //divide by 8 to get in the range of 1-127
if( abs(mux1array[i] - tempmux1) > 2) {
mux1array[i] = tempmux1;
MidiTX(char(0xB0), control1[i], char(mux1array[i]) ); //control[i] is a char look up table for the controller number to change
}
}
//This for loop is used to scroll through and store the 16 inputs on the THIRD multiplexer
for (int i=0; i<16; i++)
{
//The following 4 commands set the correct logic for the control pins to select the desired input
digitalWrite(CONTROL0, (i&15)>>3);
digitalWrite(CONTROL1, (i&7)>>2);
digitalWrite(CONTROL2, (i&3)>>1);
digitalWrite(CONTROL3, (i&1));
tempmux2 = analogRead(COM3)/8; //divide by 8 to get in the range of 1-127
if( abs(mux2array[i] - tempmux2) > 2) {
mux2array[i] = tempmux2;
MidiTX(char(0xB0), control2[i], char(mux2array[i]) ); //control[i] is a char look up table for the controller number to change
}
}
}
void MidiTX(unsigned char MESSAGE, unsigned char CONTROL, unsigned char VALUE) //pass values out through standard Midi Command
{
Serial.write(MESSAGE);
Serial.write(CONTROL);
Serial.write(VALUE);
}
I'm not getting anything in the midi monitor yet, but this may be due to other factors than the code?
Furthermore, I don't understand exactly how this works:
void MidiTX(unsigned char MESSAGE, unsigned char CONTROL, unsigned char VALUE) //pass values out through standard Midi Command
{
Serial.write(MESSAGE);
Serial.write(CONTROL);
Serial.write(VALUE);}
I think it writes char(message), char(control) and char(value) - which would be char(0xB0), control and char(muxarray*) - to the serial port. But why then do we serial.write MESSAGE, CONTROL & VALUE, instead of:*
*_ <em>*{ Serial.write(char(0xB0)); Serial.write(control[i]); Serial.write(char(muxarray[i]));*</em> _*