I avoid some hard-coded values:
Pin access optimized:
Read for a division:
Read all division:
This is still the old code without delay:
Write/send Midi for all divisions:
Edit: I have corrected two errors
I don't have an Arduino, but the application of the loops should be shown.
- Code: Select all
const int dim1 = 8;
const int dim2 = 8;
const int hwpins = 8;
Pin access optimized:
- Code: Select all
byte keysAx[hwpins] = { 17, 16, 15, 14, 2, 3, 4, 5 };
byte keysBx[hwpins] = { 22, 24, 26, 28, 30, 32, 34, 36};
byte keysCx[hwpins] = { 53, 51, 49, 47, 45, 43, 41, 39 };
byte keysDx[hwpins] = { 18, 18, 20, 21, A15, A14, A13, A12 };
Read for a division:
- Code: Select all
void readKeys(int pin, int key, byte kb[dim1][dim2], byte pins[hwpins])
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
for (int i = 0; i <= 7; i++)
kb[key][i] = digitalRead(pins[i]);
pinMode(pin, INPUT);
}
Read all division:
- Code: Select all
void readAllKeys()
{
for (int pin = 6, k=0; pin <= 13; pin++, k++)
readKeys(pin, k, keysA, keysAx);
for (int pin = 38, k=0; pin <= 52; pin += 2, k++)
readKeys(pin, k, keysB, keysBx);
for (int pin = 37, k = 0; pin >= 23; pin -= 2, k++)
readKeys(pin, k, keysC, keysCx);
for (int pin = A11, k=0; pin <= A14; pin++, k++)
readKeys(pin, k, keysD, keysDx);
}
This is still the old code without delay:
- Code: Select all
void writeKeys(int& note, int key, int noteOn, int noteOff, byte kb[dim1][dim2], byte kbLast[dim1][dim2])
{
for (int i = 0; i <= 7; i++, note++)
{
if ((kb[key][i] == 0) and (kbLast[key][i] == 0))
{
MidiSend(noteOn, note, velocity);
kbLast[key][i] = 7;
}
if ((kb[key][i] == 1) and (kbLast[key][i] == 7))
{
MidiSend(noteOff, note, velocity);
kbLast[key][i] = 0;
}
}
}
Write/send Midi for all divisions:
- Code: Select all
void writeAllKeys()
{
int note = 36; // is automatically incremented as a reference
for (int i = 0; i <= 7; i++)
writeKeys(note, i, noteOn1, noteOff1, keysA, lastA);
note = 36;
for (int i = 0; i <= 7; i++)
writeKeys(note, i, noteOn2, noteOff2, keysB, lastB);
note = 36;
for (int i = 0; i <= 7; i++)
writeKeys(note, i, noteOn3, noteOff3, keysC, lastC);
note = 36;
for (int i = 0; i <= 7; i++)
writeKeys(note, i, noteOn4, noteOff4, keysD, lastD);
}
Edit: I have corrected two errors
I don't have an Arduino, but the application of the loops should be shown.
Last edited by vpo-organist on Sun Jul 23, 2023 7:06 am, edited 4 times in total.