Korg Debounce

/*

Korg 61 note Keyboard Scan (elided version)

==========================

This sketch is designed to receive input from a 61 note Korg keyboard, perform a

scan and output MIDI codes on channel 1.

With the exception of bottom C, the contacts are arranged in a 12 x (5 + 5) matrix. One set of contacts, near the the top of the keystroke, is normally closed whereas the second set, towards the bottom, is normally open. When a NO contact is closed and the note is not already ON, a TurnOn message is sent. When a NC contact is closed and the note is not already OFF, a TurnOff

message is sent. The "time-of-flight" between the operation of the two switches provides the necessary debounce. Bottom C has its own line and outputs on Octave 1. The additional lines are 12 in number running from C# to C.

Pin Assignments:

NC bus:

Pin 2 = PD2 = Oct 5 Brown wire to Korg

...

Pin 6 = PD6 = Oct 1

Pin 7 = PD7 = not used

... 10 wires ...

NO bus:

Pin 8 = PB0 = Oct 1

...

Pun 12 = PB4 = Oct 5 Yellow wire to Korg

Pin 13 = PB5 = Not used (LED + resistor to GRD)

Note Lines:

Pin 22 = PA0 (Low C connects to Oct 1) Brown wire to Korg

Pin 23 = PA1 = C#

... ... 12 wires ...

Pin 34 = PC3 = C Green wire to Korg

Equipment: ArduinoMega with one MIDI shield

created 2016 DEC 31

modified 2016 DEC 31

by John Coenraads

*/

// Declarations==========================================

//Counters (old Fortran habit)

int i, j, k;

byte noteArray [100]; //stores note status, ON = 1, OFF = 0 for MIDI notes 36 to 96

//Initialize =========================================================

void setup()

{

// Set MIDI baud rate:

Serial.begin(31250);

//Initialize Port B for input (default). Normally high (via internal pullups)

for (i = 8; i < 13; i++)

{ pinMode (i, INPUT_PULLUP);}

//Initialize Port D for input (default). Normally high (via internal pullups)

for (i = 2; i < 8; i++)

{ pinMode (i, INPUT_PULLUP);}

// Set Ports A and C for output (normally, high, set low to activate)

for (i =22; i < 35; i++)

{

pinMode (i, OUTPUT);

digitalWrite (i, HIGH);

}

//Initialize note status array to zero (OFF)

for (i= 0; i < 100; i++)

{noteArray[i] = 0;}

}

//Main Loop ===========================================================

void loop()

{

// Special Case: bottom C

digitalWrite (22, LOW);

if ((digitalRead(6) == LOW) && (noteArray[36] == 1)) TurnNoteOff(36); //NC contact closed

if ((digitalRead(8) == LOW) && (noteArray[36] == 0)) TurnNoteOn(36); //NO contact closed

digitalWrite (22, HIGH);

// C# Octave 1 - 5

digitalWrite (23, LOW);

if ((digitalRead(6) == LOW) && (noteArray[37] == 1)) TurnNoteOff(37); //NC contact closed

if ((digitalRead(8) == LOW) && (noteArray[37] == 0)) TurnNoteOn(37); //NO contact closed

if ((digitalRead(5) == LOW) && (noteArray[49] == 1)) TurnNoteOff(49); //NC contact closed

if ((digitalRead(9) == LOW) && (noteArray[49] == 0)) TurnNoteOn(49); //NO contact closed

if ((digitalRead(4) == LOW) && (noteArray[61] == 1)) TurnNoteOff(61); //NC contact closed

if ((digitalRead(10) == LOW) && (noteArray[61] == 0)) TurnNoteOn(61); //NO contact closed

if ((digitalRead(3) == LOW) && (noteArray[73] == 1)) TurnNoteOff(73); //NC contact closed

if ((digitalRead(11) == LOW) && (noteArray[73] == 0)) TurnNoteOn(73); //NO contact closed

if ((digitalRead(2) == LOW) && (noteArray[85] == 1)) TurnNoteOff(85); //NC contact closed

if ((digitalRead(12) == LOW) && (noteArray[85] == 0)) TurnNoteOn(85); //NO contact closed

digitalWrite (23, HIGH);

// D Octave 1 - 5

digitalWrite (24, LOW);

if ((digitalRead(6) == LOW) && (noteArray[38] == 1)) TurnNoteOff(38); //NC contact closed

if ((digitalRead(8) == LOW) && (noteArray[38] == 0)) TurnNoteOn(38); //NO contact closed

if ((digitalRead(5) == LOW) && (noteArray[50] == 1)) TurnNoteOff(50); //NC contact closed

if ((digitalRead(9) == LOW) && (noteArray[50] == 0)) TurnNoteOn(50); //NO contact closed

if ((digitalRead(4) == LOW) && (noteArray[62] == 1)) TurnNoteOff(62); //NC contact closed

if ((digitalRead(10) == LOW) && (noteArray[62] == 0)) TurnNoteOn(62); //NO contact closed

if ((digitalRead(3) == LOW) && (noteArray[74] == 1)) TurnNoteOff(74); //NC contact closed

if ((digitalRead(11) == LOW) && (noteArray[74] == 0)) TurnNoteOn(74); //NO contact closed

if ((digitalRead(2) == LOW) && (noteArray[86] == 1)) TurnNoteOff(86); //NC contact closed

if ((digitalRead(12) == LOW) && (noteArray[86] == 0)) TurnNoteOn(86); //NO contact closed

digitalWrite (24, HIGH);

// ETC.

// ETC.

// ETC.

// C Octave 1 - 5

digitalWrite (34, LOW);

if ((digitalRead(6) == LOW) && (noteArray[48] == 1)) TurnNoteOff(48); //NC contact closed

if ((digitalRead(8) == LOW) && (noteArray[48] == 0)) TurnNoteOn(48); //NO contact closed

if ((digitalRead(5) == LOW) && (noteArray[60] == 1)) TurnNoteOff(60); //NC contact closed

if ((digitalRead(9) == LOW) && (noteArray[60] == 0)) TurnNoteOn(60); //NO contact closed

if ((digitalRead(4) == LOW) && (noteArray[72] == 1)) TurnNoteOff(72); //NC contact closed

if ((digitalRead(10) == LOW) && (noteArray[72] == 0)) TurnNoteOn(72); //NO contact closed

if ((digitalRead(3) == LOW) && (noteArray[84] == 1)) TurnNoteOff(84); //NC contact closed

if ((digitalRead(11) == LOW) && (noteArray[84] == 0)) TurnNoteOn(84); //NO contact closed

if ((digitalRead(2) == LOW) && (noteArray[96] == 1)) TurnNoteOff(96); //NC contact closed

if ((digitalRead(12) == LOW) && (noteArray[96] == 0)) TurnNoteOn(96); //NO contact closed

digitalWrite (34, HIGH);

}

void TurnNoteOff (int noteNumber)

{

noteArray[noteNumber] = 0;

Serial.write (0x90); //channel 1

Serial.write (noteNumber);

Serial.write (0); //zero velocity = OFF

}

void TurnNoteOn (int noteNumber)

{

noteArray[noteNumber] = 1;

Serial.write (0x90); //channel 1

Serial.write (noteNumber);

Serial.write (0x7f); //medium velocity = ON

}

void trace (byte info)

{

Serial.write (0xF3);

Serial.write (info);

}