Hey,
I'm using an Arduino Mega trying to read a 8 x 10 keyboard matrix with the help of interrupts. Right now I have the 8 rows connected to port K so I can get a port readout quickly. I'm probably going to use port B in the final sketch but the pinout of my keyboard makes port K more convenient right now.
If I've understood it correctly, I should be able to use some ports as interrupts, which will sense if any of the pins of the port changes and then fire an interrupt.
How do I set this up? I've been reading a lot about interrupts but I still can't seem to grasp it. Would be very thankful if someone could tell me what I should read up more on.
albinjalbinj:
I'm using an Arduino Mega trying to read a 8 x 10 keyboard matrix with the help of interrupts.
Then you are doing it completely wrong!
albinjalbinj:
Right now I have the 8 rows connected to port K so I can get a port readout quickly.
Whatever makes you imagine there is any need whatsoever to read a keyboard quickly?
You apparently do not comprehend the scale of microprocessor time. The microprocessor works in the realm of microseconds while your fingers work a few times a second. The difference is a factor of ten thousand times!
And in the middle of these two is something called "contact bounce" which must be taken into account by your code.
albinjalbinj:
I'm probably going to use port B in the final sketch but the pinout of my keyboard makes port K more convenient right now.
Whatever!
albinjalbinj:
If I've understood it correctly, I should be able to use some ports as interrupts, which will sense if any of the pins of the port changes and then fire an interrupt.
You could do that, but it would be completely inappropriate. It is very difficult and in fact, wastes processor time to manage contact bounce in interrupt code. The proper way to do it is to poll your keyboard in the main loop() which if properly written, will cycle at least every millisecond. This allows you, using millis(), to verify keyboard contact changes over at least ten milliseconds and reject bounce. And you can do this in a byte-wide fashion.
albinjalbinj:
How do I set this up? I've been reading a lot about interrupts but I still can't seem to grasp it.
This is going to be a MIDI Keyboard and it was my impression that MIDI Keyboards usually use interrupts to be as quick as possible. I will add more functions to it that might take a lot of processor time so I figured that interrupts were the correct choice instead of polling.
If that's not the case, I'd be very happy because polling would be so much easier for me to understand and modify.
If each key was connected to a separate pin, then using interrupts might be slightly faster than polling (but still not worth it, because polling would still be very fast).
But with a matrix, you have to scan the matrix, you can't read any individual key without doing that. If you are scanning, you might as well be polling, using interrupts would give zero benefit.
For pin you generally need to configure two registers (Mask/function and enable interrupt) and and a interrupt handler.
For pin change it is the PCMSK and PCIFR registers.
Each time a key is pressed or released you will get one or more interrupts.
Often a timer interrupt that polls the keys at regular intervals (Like a row each ms) and do debounce will be a better solution.
Again, the difference between several milliseconds (or a few tens) and the microsecond scale at which the microcontroller runs, is the important point. On a musical keyboard, debounce is just as important and that is a matter of milliseconds.
And you do not scan individual keys, but a byte at a time using register reads (and writes); you perform debounce a byte at a time and only determine which individual bit is active outside of that process.
albinjalbinj:
This is going to be a MIDI Keyboard and it was my impression that MIDI Keyboards usually use interrupts to be as quick as possible.
I wonder where you might have been given such impression? I think it pretty unlikely.
albinjalbinj:
I will add more functions to it that might take a lot of processor time so I figured that interrupts were the correct choice instead of polling.
So what might these functions be? You may misunderstand the principles of "state machine" coding which is essential to most tasks.
I can imagine very little happening in a MIDI keyboard that is not strictly consequential to key actions.
OK this is the first mention that it is a music keyboard if makes a difference to a normal key entry device.
The other things planned for the keyboard are pitch and mod wheels, velocity, aftertouch, octave up/down and control voltage outs.
Is it going to need to be a velocity sensitive input?
If so you need two sets of contacts on each key and you need to detect the time between the normal closed being open and the normally open being closed.
As to velocity, aftertouch that is not implement in many MIDI sound generators and when it is it is a global value for all keys not for individual keys.