ArduinoUno

/*

  Sketch to MIDIfy Galanti Pedal Board

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

  The pedal board is scanned and the MIDI code is generated on Channel 1 

  The pedal switches are arranged in a 4 x 8 diode isolated matrix. 

  The four common lines are sequentially driven LOW and are connected as follows to four Arduino Outputs:

  Pedal ("D" connector) >>> Arduino pins

               12              A0                Lowest "octave"

               11              A1            

               10              A2            

                9              A3

  The eight "octave" input lines are then scanned. Although normally held HIGH by internal pullup resistors, 

  when a key switch is closed, that line goes LOW. 

  Pedal ("D" connector) >>> Arduino pins

               1              2                Note C1

               2              3            

               3              4            

               4              5                                            

               5              6                

               6              7            

               7              8           

               8              9           

  Equipment: Arduino Uno with MIDI Out using two 220 ohm resistors connected to 5V and Tx (pin 1).

  created 2019 JAN 6

  modified 2023 MAR 4

  by John Coenraads

*/

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

int i, j, k;               //Counters (old Fortran habit)

int colCount, rowCount;    //matrix column (output:drive LOW) and row (input:normally HIGH) count

byte inputBit;             //input data bit

byte noteNumber;           //noteNumber for keyboard scan, low C = 36

const byte debounceCount = 6;           //Note ON if count = 6, OFF if count = 0

byte debouncePedalArray [110];          //holds debounce count for each pedal keyswitch

byte noteOnPedalArray [110];            //tracks which notes are turned on Pedal

void setup()

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

{

  Serial.begin(31250);       //  Set MIDI baud rate:

  //Initialize pedal lines for output (normally high, drive low)  Arduino pins A0, A1, A2, A3

  for (i = 14; i < 18; i++)

  {

    pinMode (i, OUTPUT);

    digitalWrite (i, HIGH);

  }

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

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

    {

       pinMode (i, INPUT_PULLUP);

    }

  //Initialize debounce count array to zero

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

  {

    debouncePedalArray[i] = 0;

  }

  //Initialize noteOn arrays to zero (note off) (false)

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

  {

    noteOnPedalArray [i] = 0;

  }

}

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

void loop()

{

  scanPedal();

  delay (1);              //slow down debounce

}

//Scan pedal input, convert to MIDI and output via port 0, channel 1 

void scanPedal ()

{

  noteNumber = 36;    //start at low C

  for (colCount = 14; colCount < 18; colCount++)

    {

    digitalWrite (colCount, LOW);

    for (rowCount = 2; rowCount < 10; rowCount++)

      {

         inputBit  = digitalRead(rowCount);    //read bit

         if (inputBit == HIGH)                 //bit HIGH, switch open

           {

              turnPedalNoteOFF();

           }

         else                                  //bit LOW, switch closed

           {

              turnPedalNoteON();

           }   

      noteNumber = noteNumber + 1;                    //move on to next note

    }

    digitalWrite (colCount, HIGH);

  }

}

//Turn note on. Debouncing is achieved by requiring that several turnNoteON requests

//are received before sending out noteOn MIDI message

void turnPedalNoteON ()

{

  if (debouncePedalArray[noteNumber] < debounceCount)

  {

    debouncePedalArray [noteNumber] = debouncePedalArray [noteNumber] + 1;

    if ((debouncePedalArray[noteNumber] == debounceCount) && ( !noteOnPedalArray[noteNumber]))

    {

      Serial.write (0x90);          //note ON, channel 1,

      Serial.write (noteNumber);

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

      noteOnPedalArray[noteNumber] = 1;                //note now ON

    }

  }

}

//Turn note off. Debouncing is achieved by requiring that several turnNoteOFF requests

//are received before sending out noteOff MIDI message

void turnPedalNoteOFF ()

{

  if (debouncePedalArray[noteNumber] > 0)

  {

    debouncePedalArray [noteNumber] = debouncePedalArray [noteNumber] - 1;

    if ((debouncePedalArray[noteNumber] == 0) && (noteOnPedalArray[noteNumber]))

    {

      Serial.write (0x90);          //note ON, channel 1,

      Serial.write (noteNumber);

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

      noteOnPedalArray[noteNumber] = 0;                  //note now OFF

    }

  }

}   

void trace (byte info)   // used during debugging

{

  Serial.write (0xF3);

  Serial.write (info);

}