Control Surface  1.1.1
MIDI Control Surface library for Arduino
Keyboard-Matrix-BCD.ino

Keyboard-Matrix-BCD

This examples shows how to use a button matrix to read many switches using a BD to decimal decoder.
https://forum.arduino.cc/index.php?topic=651047

Boards:
AVR, AVR USB, Due, Nano 33, Teensy 3.x, ESP32

Written by Pieter P, 2019-12-01
https://github.com/tttapa/Control-Surface

#include <AH/STL/bitset>
// Pick one MIDI interface to use:
USBDebugMIDI_Interface midi(115200); // baud rate, use for easy debugging
// USBMIDI_Interface midi; // Use for final sketch with MIDI firmware
// HairlessMIDI_Interface midi; // Use for Hairless Serial <-> MIDI bridge
// Dimensions of the keyboard matrix
constexpr uint8_t rows = 10;
constexpr uint8_t columns = 8;
static_assert(rows <= 10, "Error: TC4028 only has 10 outputs");
// The note numbers corresponding to the buttons in the matrix
{1, 2, 3, 4, 5, 6, 7, 8}, //
{9, 10, 11, 12, 13, 14, 15, 16}, //
// 8 more rows ...
}};
// The MIDI channel to use
const Channel channel = CHANNEL_1;
// Address pins A, B, C & D of TC4028
const PinList<4> addressPins = {{2, 3, 4, 5}};
// The pins connected to the columns of the keyboard matrix (with external
// pull-down resistors)
const PinList<columns> columnPins = {{6, 7, 8, 9, 10, 11, 12, 13}};
// Make the given line [0, 9] of the TC4028 high, the nine others low
void selectLine(uint8_t line) {
uint8_t mask = 1;
for (pin_t addressPin : addressPins) {
digitalWrite(addressPin, (line & mask) != 0);
mask <<= 1;
}
}
void setup() {
for (pin_t addressPin : addressPins)
pinMode(addressPin, OUTPUT);
for (pin_t columnPin : columnPins)
pinMode(columnPin, INPUT);
midi.begin();
}
// Array of bits to remember previous switch states for state change detection.
std::bitset<rows * columns> previousStates;
void loop() {
for (uint8_t row = 0; row < rows; ++row) {
selectLine(row);
for (uint8_t col = 0; col < columns; ++col) {
bool newState = digitalRead(columnPins[col]);
uint8_t index = row * columns + col; // index in the bit array
// If the state of the switch changed
if (newState != previousStates[index]) {
// Send a MIDI note message
// (note on message with zero velocity == note off)
uint8_t velocity = newState * 0x7F;
midi.sendNoteOn({notes[row][col], channel}, velocity);
// Remember this new state
previousStates[index] = newState;
// TODO: add debouncing
}
}
}
midi.update(); // read and discard MIDI input
}
AH::ExtIO::pinMode
void pinMode(pin_t pin, uint8_t mode)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:36
Channel
A type-safe class for MIDI channels.
Definition: Channel.hpp:13
AH::ExtIO::digitalRead
int digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:60
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
Control_Surface.h
The main header file that includes all Control-Surface header files.
AddressMatrix
Array2D< uint8_t, nb_rows, nb_cols > AddressMatrix
Definition: Def.hpp:26
INPUT
const uint8_t INPUT
Definition: ExtendedInputOutput.hpp:49
OUTPUT
const uint8_t OUTPUT
Definition: ExtendedInputOutput.hpp:50
AH::ExtIO::digitalWrite
void digitalWrite(pin_t pin, uint8_t val)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:48
USBDebugMIDI_Interface
A class for debug MIDI interfaces sending and receiving human-readable MIDI messages over the USB CDC...
Definition: DebugMIDI_Interface.hpp:115
CHANNEL_1
constexpr Channel CHANNEL_1
Definition: Channel.hpp:111