Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MIDI.h>
- const int ROWS = 9; // Número de linhas na matriz
- const int COLS = 6; // Número de colunas na matriz
- const int MIDI_CHANNEL = 1; // Canal MIDI para envio de dados
- // Mapeamento das teclas para notas MIDI
- const byte NOTE_MAP[ROWS][COLS] = {
- {36, 37, 38, 39, 40, 41}, // Notas da primeira linha
- {42, 43, 44, 45, 46, 47}, // Notas da segunda linha
- {48, 49, 50, 51, 52, 53}, // Notas da terceira linha
- {54, 55, 56, 57, 58, 59}, // Notas da quarta linha
- {60, 61, 62, 63, 64, 65}, // Notas da quinta linha
- {66, 67, 68, 69, 70, 71}, // Notas da sexta linha
- {72, 73, 74, 75, 76, 77}, // Notas da sétima linha
- {78, 79, 80, 81, 82, 83}, // Notas da oitava linha
- {84, 0, 0, 0, 0, 0} // Última linha, apenas a primeira coluna é usada
- };
- // Pinos da matriz
- const int rowPins[ROWS] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
- const int colPins[COLS] = {11, 12, 13, A0, A1, A2};
- // Pinos da chave de 3 posições para controlar a oitava
- const int octaveSwitchPin1 = A3;
- const int octaveSwitchPin2 = A4;
- // Variável para armazenar o estado da oitava (0, 1 ou 2)
- int octaveState = 1; // Por padrão, configuração normal (oitava 1)
- // Array para armazenar o estado atual das teclas
- bool keyState[ROWS][COLS];
- unsigned long lastDebounceTime[ROWS][COLS] = {0};
- unsigned long debounceDelay = 50;
- void setup() {
- MIDI.begin(MIDI_CHANNEL);
- // Configuração dos pinos
- for (int i = 0; i < ROWS; i++) {
- pinMode(rowPins[i], INPUT_PULLUP);
- }
- for (int i = 0; i < COLS; i++) {
- pinMode(colPins[i], OUTPUT);
- digitalWrite(colPins[i], HIGH);
- }
- pinMode(octaveSwitchPin1, INPUT_PULLUP);
- pinMode(octaveSwitchPin2, INPUT_PULLUP);
- }
- void loop() {
- // Lê o estado atual das teclas
- readKeys();
- // Lê o estado atual da chave de 3 posições
- readOctaveSwitch();
- // Envia eventos MIDI para teclas pressionadas
- sendMIDI();
- }
- void readKeys() {
- for (int col = 0; col < COLS; col++) {
- // Ativa a coluna
- digitalWrite(colPins[col], LOW);
- // Verifica as linhas para ver se alguma tecla está pressionada
- for (int row = 0; row < ROWS; row++) {
- if (digitalRead(rowPins[row]) == LOW) {
- if ((millis() - lastDebounceTime[row][col]) > debounceDelay) {
- keyState[row][col] = true;
- lastDebounceTime[row][col] = millis();
- }
- } else {
- keyState[row][col] = false;
- }
- }
- // Desativa a coluna
- digitalWrite(colPins[col], HIGH);
- }
- }
- void readOctaveSwitch() {
- // Lê o estado da chave de 3 posições
- int switch1State = digitalRead(octaveSwitchPin1);
- int switch2State = digitalRead(octaveSwitchPin2);
- // Calcula o estado da oitava
- octaveState = switch1State * 2 + switch2State;
- }
- void sendMIDI() {
- for (int row = 0; row < ROWS; row++) {
- for (int col = 0; col < COLS; col++) {
- // Verifica se a tecla está pressionada e se é válida
- if (keyState[row][col] && NOTE_MAP[row][col] != 0) {
- // Ajusta a nota MIDI baseada no estado da oitava
- byte adjustedNote = NOTE_MAP[row][col] + (octaveState - 1) * 12;
- // Envia uma nota MIDI pressionada
- MIDI.sendNoteOn(adjustedNote, 127, MIDI_CHANNEL);
- } else {
- // Ajusta a nota MIDI baseada no estado da oitava
- byte adjustedNote = NOTE_MAP[row][col] + (octaveState - 1) * 12;
- // Envia uma mensagem de desligamento para a nota MIDI liberada
- MIDI.sendNoteOff(adjustedNote, 0, MIDI_CHANNEL);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement