Hello everyone,
nice project! It reminds me of the old and defect home organ which sadly went to the trash a few years ago. Sadly, I did not know anything about Teensy and Arduino at this point of time, so I would have saved it 
But back to topic:
Can you give an example how to use millis() or elapsedMillis?
Yes, the example uses "elapsedMicros" (using microseconds instead of milliseconds), but you could easily use "elapsedMillis" instead:
Code:
// if you need longer times, you can also use elapsedMillis instead of elapsedMicros
elapsedMicros myTimer = 0; // elapsedMillis myTimer = 0; //
uint16_t myLoopCounter = 0;
uint16_t myloopCount = 5000;
void setup()
{
// setup stuff here
}
void loop()
{
// reset timer at the beginning of you loop
myTimer = 0;
// do all you loop stuff like scanning keys, debouncing etc. here ...
// ...
// ...
// at the end of the loop:
// increase loop counter, print the loop data every 'myloopCount' times (increase 'myloopCount' if the serial monitor gets to crowded)
// you could also calculate the average loop time
myLoopCounter++;
if (myLoopCounter >= myloopCount) {
myLoopCounter = 0;
Serial.println("Loop Time in us:");
Serial.println(myTimer);
}
}
How to switch to 400khz clock from 100khz, can you give me some tutorial or sample code?
I2C setup:
Code:
Wire.begin(); //Join the bus as master.
//By default .begin() will set I2C SCL to Standard Speed mode of 100kHz
Wire.setClock(400000); //Optional - set I2C SCL to High Speed Mode of 400kHz
Source see here.
Also have a look at the optimized Teensy I2C library.
I do not know if it is at this time of the project an option for you to change the hardware, fanatic606, but I would say the same as Revalogics:
maybe you should have a look into mulitplexing (matrix scanning). I am not sure, if you can achieve the goal of low latency with the multiple port expanders. I guess with mulitplexing you can get down your latency.
The diode for each key would be required for polyphony.
You can find the concepts of input matrix scanning (especially for musical instruments) here. Scroll down to 'Matrix Scanning Options' and see the futher links.
I am currently working on the option using 'Parallel-out shift register mux’d with uC' in my project.
You can also check the Teensy forum thread I openeded on debouncing a button matrix, there are the schematics and some code. Maybe there are better ways (as I am also learning), but the results look promising so far.
Hope that helps 
Best wishes,
afterwards