Advertisement
NicoChan

GPT - MIDI + Chave + Debounce

May 6th, 2024 (edited)
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | Source Code | 0 0
  1. #include <MIDI.h>
  2.  
  3. const int ROWS = 9;  // Número de linhas na matriz
  4. const int COLS = 6;  // Número de colunas na matriz
  5. const int MIDI_CHANNEL = 1;  // Canal MIDI para envio de dados
  6.  
  7. // Mapeamento das teclas para notas MIDI
  8. const byte NOTE_MAP[ROWS][COLS] = {
  9.   {36, 37, 38, 39, 40, 41},  // Notas da primeira linha
  10.   {42, 43, 44, 45, 46, 47},  // Notas da segunda linha
  11.   {48, 49, 50, 51, 52, 53},  // Notas da terceira linha
  12.   {54, 55, 56, 57, 58, 59},  // Notas da quarta linha
  13.   {60, 61, 62, 63, 64, 65},  // Notas da quinta linha
  14.   {66, 67, 68, 69, 70, 71},  // Notas da sexta linha
  15.   {72, 73, 74, 75, 76, 77},  // Notas da sétima linha
  16.   {78, 79, 80, 81, 82, 83},  // Notas da oitava linha
  17.   {84, 0, 0, 0, 0, 0}         // Última linha, apenas a primeira coluna é usada
  18. };
  19.  
  20. // Pinos da matriz
  21. const int rowPins[ROWS] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
  22. const int colPins[COLS] = {11, 12, 13, A0, A1, A2};
  23.  
  24. // Pinos da chave de 3 posições para controlar a oitava
  25. const int octaveSwitchPin1 = A3;
  26. const int octaveSwitchPin2 = A4;
  27.  
  28. // Variável para armazenar o estado da oitava (0, 1 ou 2)
  29. int octaveState = 1; // Por padrão, configuração normal (oitava 1)
  30.  
  31. // Array para armazenar o estado atual das teclas
  32. bool keyState[ROWS][COLS];
  33. unsigned long lastDebounceTime[ROWS][COLS] = {0};
  34. unsigned long debounceDelay = 50;
  35.  
  36. void setup() {
  37.   MIDI.begin(MIDI_CHANNEL);
  38.  
  39.   // Configuração dos pinos
  40.   for (int i = 0; i < ROWS; i++) {
  41.     pinMode(rowPins[i], INPUT_PULLUP);
  42.   }
  43.   for (int i = 0; i < COLS; i++) {
  44.     pinMode(colPins[i], OUTPUT);
  45.     digitalWrite(colPins[i], HIGH);
  46.   }
  47.   pinMode(octaveSwitchPin1, INPUT_PULLUP);
  48.   pinMode(octaveSwitchPin2, INPUT_PULLUP);
  49. }
  50.  
  51. void loop() {
  52.   // Lê o estado atual das teclas
  53.   readKeys();
  54.  
  55.   // Lê o estado atual da chave de 3 posições
  56.   readOctaveSwitch();
  57.  
  58.   // Envia eventos MIDI para teclas pressionadas
  59.   sendMIDI();
  60. }
  61.  
  62. void readKeys() {
  63.   for (int col = 0; col < COLS; col++) {
  64.     // Ativa a coluna
  65.     digitalWrite(colPins[col], LOW);
  66.  
  67.     // Verifica as linhas para ver se alguma tecla está pressionada
  68.     for (int row = 0; row < ROWS; row++) {
  69.       if (digitalRead(rowPins[row]) == LOW) {
  70.         if ((millis() - lastDebounceTime[row][col]) > debounceDelay) {
  71.           keyState[row][col] = true;
  72.           lastDebounceTime[row][col] = millis();
  73.         }
  74.       } else {
  75.         keyState[row][col] = false;
  76.       }
  77.     }
  78.  
  79.     // Desativa a coluna
  80.     digitalWrite(colPins[col], HIGH);
  81.   }
  82. }
  83.  
  84. void readOctaveSwitch() {
  85.   // Lê o estado da chave de 3 posições
  86.   int switch1State = digitalRead(octaveSwitchPin1);
  87.   int switch2State = digitalRead(octaveSwitchPin2);
  88.  
  89.   // Calcula o estado da oitava
  90.   octaveState = switch1State * 2 + switch2State;
  91. }
  92.  
  93. void sendMIDI() {
  94.   for (int row = 0; row < ROWS; row++) {
  95.     for (int col = 0; col < COLS; col++) {
  96.       // Verifica se a tecla está pressionada e se é válida
  97.       if (keyState[row][col] && NOTE_MAP[row][col] != 0) {
  98.         // Ajusta a nota MIDI baseada no estado da oitava
  99.         byte adjustedNote = NOTE_MAP[row][col] + (octaveState - 1) * 12;
  100.        
  101.         // Envia uma nota MIDI pressionada
  102.         MIDI.sendNoteOn(adjustedNote, 127, MIDI_CHANNEL);
  103.       } else {
  104.         // Ajusta a nota MIDI baseada no estado da oitava
  105.         byte adjustedNote = NOTE_MAP[row][col] + (octaveState - 1) * 12;
  106.        
  107.         // Envia uma mensagem de desligamento para a nota MIDI liberada
  108.         MIDI.sendNoteOff(adjustedNote, 0, MIDI_CHANNEL);
  109.       }
  110.     }
  111.   }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement