Hello - just bought an arduino uno about 2 weeks ago as I have been wanting to build my own MIDI controller with just a bunch of knobs for a while now. So forgive my extreme programming ignorance -- I am just starting to learn. I did take a intro to PHP class a couple semester ago... Anyway my project uses the Arduino UNO and 3 16-channel multiplexers (Texas Instruments CD74HC4067). I am also using the MocoLUFA firmware to send MIDI over USB. The code I have written works, but the output is kinda slow - I'm guessing because it's doing 48 loops for each of the 48 loops. Here's the code, hopefully you understand what I'm getting at -- I know there are probably much better ways to do this - so help me out please:
int input[] = {
B00000000,
B00000001,
B00000010,
B00000011,
B00000100,
B00000101,
B00000110,
B00000111,
B00001000,
B00001001,
B00001010,
B00001011,
B00001100,
B00001101,
B00001110,
B00001111,
B00000000,
B00010000,
B00100000,
B00110000,
B01000000,
B01010000,
B01100000,
B01110000,
B10000000,
B10010000,
B10100000,
B10110000,
B11000000,
B11010000,
B11100000,
B11110000,
B00000000,
B00000001,
B00000010,
B00000011,
B00000100,
B00000101,
B00000110,
B00000111,
B00001000,
B00001001,
B00001010,
B00001011,
B00001100,
B00001101,
B00001110,
B00001111,}; //set array of port values, 0-15 are PORTB pins 8-11, 16-31 are PORTD pins 4-7, 32-47 are PORTC pins 0-1 and PORTD pins 2-3
int val0;
int val1;
int val2; //initialize analogRead value variables
int chanVal[48]; //initialize array to hold all 48 values to compare with old values
int oldChanVal[48]; //initialize array to hold all 48 old values to compare with new values
void setup(){
DDRB = B00001111; // set PORTB (digital pins 8-11) to output
PORTB = B00000000; // set PORTB to LOW
DDRD = DDRD | B11111100; // set PORTD (digital pins 2-7) to output while making sure not to change pins 0 and 1 (TX and RX)
PORTD = B00000000; // set PORTD to LOW
DDRC = DDRC | B000011; // set PORTC (analog pins 0-1) to output
PORTC = B000000; // set PORTC to LOW
Serial.begin(38400); //set this to 31250 for MIDI
}
void loop(){
for(int a=0; a < 48; a++){ //loop through all 48 channels
for(int i=0; i < 16; i++){ //loop through first 16 channels to change PORTB value and assign val0 to chanVal array
val0 = map(analogRead(A2), 0, 1023, 0, 127);
PORTB = input[i];
chanVal[i] = val0;
}
for(int j=16; j < 32; j++){ //loop through 2nd 16 channels to change PORTD value and assign val1 to chanVal array
val1 = map(analogRead(A3), 0, 1023, 0, 127);
PORTD = input[j];
chanVal[j] = val1;
}
for(int k=32; k < 48; k++){ //loop through 3rd 16 channels to change PORTC and PORTD value and assign val2 to chanVal array
val2 = map(analogRead(A4), 0, 1023, 0, 127);
PORTC = input[k];
PORTD = input[k];
chanVal[k] = val2;
}
if(abs(chanVal[a] - oldChanVal[a]) > 1){ //test to see if any of the values in the chanVal array have changed by more than one -- if so, print
Serial.print(0xB0);
Serial.print("::");
Serial.print(a);
Serial.print("::");
Serial.println(chanVal[a]);
}
oldChanVal[a] = chanVal[a]; //assign chanVal values to oldChanVal before starting over
}
delay(4);
}