Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notas midi involuntárias #15

Open
Willame156 opened this issue Mar 28, 2021 · 2 comments
Open

Notas midi involuntárias #15

Willame156 opened this issue Mar 28, 2021 · 2 comments

Comments

@Willame156
Copy link

Boa noite.
Reproduzi esse projeto e todas as teclas estão funcionando com seus respectivos midi mas tem um problema.
Sempre que coloco um acorde de Sol MAIOR (T, 3ª e 5ª) meu teclado fica enviando notas midi involuntárias.
E a nota involuntária sempre é a de Dó sustenido.
Você tem ideia do que pode ser?
Desde já agradeço por sua atenção.

Esse é o código que estou usando:

#include <DIO2.h> // install the library DIO2
#define KEYS_NUMBER 61
#define KEY_OFF 0
#define KEY_START 1
#define KEY_ON 2
#define KEY_RELEASED 3

#define PEDAL_SUS 21

//POTENCIOMETROS
const int N_POTS = 3; //* número total de pots (slide e rotativo)
const int POT_ARDUINO_PIN[N_POTS] = {A0, A1, A2}; //* pinos de cada pot conectado diretamente ao Arduino
int potCState[N_POTS] = {0}; // estado atual da porta analogica
int potPState[N_POTS] = {0}; // estado previo da porta analogica
int potVar = 0; // variacao entre o valor do estado previo e o atual da porta analogica
int midiCState[N_POTS] = {0}; // Estado atual do valor midi
int midiPState[N_POTS] = {0}; // Estado anterior do valor midi
const int TIMEOUT = 300; //* quantidade de tempo em que o potenciometro sera lido apos ultrapassar o varThreshold
const int varThreshold = 100; //* threshold para a variacao no sinal do potenciometro
boolean potMoving = true; // se o potenciometro esta se movendo
unsigned long PTime[N_POTS] = {0}; // tempo armazenado anteriormente
unsigned long timer[N_POTS] = {0}; // armazena o tempo que passou desde que o timer foi zerado
boolean pit =false;
boolean mod=false;

byte cc = 1; //* O mais baixo MIDI CC a ser usado
void potentiometers() {

/* //Debug somente
for (int i = 0; i < N_POTS; i++) {
//Serial.print(potCState[i]); Serial.print(" ");
Serial.print(midiCState[i]); Serial.print(" ");
}
Serial.println();
*/

for (int i = 0; i < N_POTS; i++) { // Faz o loop de todos os potenciômetros

potCState[i] = analogRead(POT_ARDUINO_PIN[i]);

if(i==0){
midiCState[i] = map(potCState[i], 450, 880, 0, 127); // Mapeia a leitura do potCState para um valor utilizável em midi
}
if(i==1){
midiCState[i] = map(potCState[i], 215, 795, 0, 127);  
}
if(i==2){ 
midiCState[i] = map(potCState[i], 0, 1023, 0, 127); 
}

potVar = abs(potCState[i] - potPState[i]); // Calcula o valor absoluto entre a diferença entre o estado atual e o anterior do pot

if (potVar > varThreshold) { // Abre o portão se a variação do potenciômetro for maior que o limite (varThreshold)
  PTime[i] = millis(); // Armazena o tempo anterior
}

timer[i] = millis() - PTime[i]; // Reseta o timer 11000 - 11000 = 0ms

if (timer[i] < TIMEOUT) { // Se o timer for menor que o tempo máximo permitido, significa que o potenciômetro ainda está se movendo
  potMoving = true;
}
else {
  potMoving = false;
}

if (potMoving == true) { // Se o potenciômetro ainda estiver em movimento, envie control change
  if (midiPState[i] != midiCState[i]) {
     if(i==0){// reservado para modulation wheel  
     send_midi_eventcc(0xB0, cc+ i, midiCState[i]);
     mod=false;
     }
     if(i==1){//reservado para pitchbend
     send_midi_eventcc(0xE0, cc+ i, midiCState[i]);
     pit=false;
     }
     if(i==2){
      
     send_midi_eventcc(0xB0, 7, midiCState[i]); 
     }
    potPState[i] = potCState[i]; // Armazena a leitura atual do potenciômetro para comparar com a próxima
    midiPState[i] = midiCState[i];
  }
}
if(potMoving==false){
  if(midiCState[1]>55  && midiCState[1]<66 && pit==false){
  midiCState[1]=64;
  send_midi_eventcc(0xE0, 1, midiCState[1]);
  pit=true;
}
if(midiCState[0]<10 && mod ==false){
  midiCState[0]=0;
  send_midi_eventcc(0xB0, 1, midiCState[0]);
  mod=true;
}
}    

}
}

#define MIN_TIME_MS 3000
#define MAX_TIME_MS 30000
//20000 até 80000 entre foi melhor até agora3000 40000
#define MAX_TIME_MS_N (MAX_TIME_MS - MIN_TIME_MS)

//find out the pins using a multimeter, starting from the first key
//see the picture key_scheme.png to understand how to map the inputs and outputs

//the following configuration is specific for PSR530
//thanks Leandro Meucchi, from Argentina, by the PDF
//take a look at the scheme detailed in PSR530.pdf and modify the following mapping according to the wiring of your keyboard
//===============================================================================================================================================
//===============================================================================================================================================

//Grupo de 6 teclas/notas - 2 pinos por tecla, sendo um para tom e outro para velocity

#define PIN_A1 44 //44 //6
#define PIN_A2 42 //42 //6

#define PIN_A3 40 //40 //4
#define PIN_A4 38 //38 //4

#define PIN_A5 36 //36 //2
#define PIN_A6 34 //34 //2

#define PIN_A7 32 //32 //3
#define PIN_A8 30 //30 //3

#define PIN_A9 28 //28 //1
#define PIN_A10 26 //26 //1

#define PIN_A11 24 //24 //5
#define PIN_A12 22 //22 //5

//===============================================================================================================================================
//===============================================================================================================================================

//Grupo de 6 fios/pinos, onde a cada 1 pino é responsável por uma oitava
#define PIN_B1 35
#define PIN_B2 33

#define PIN_B3 31
#define PIN_B4 29

#define PIN_B5 27
#define PIN_B6 25

#define PIN_B7 23

#define PIN_C1 45 //Essa desativei pois no esquema elétrico do meu teclado não precisou
#define PIN_C2 43

#define PIN_C3 41
#define PIN_C4 39

#define PIN_C5 37

byte output_pins [] = {
PIN_B7, //C0
PIN_B7,

PIN_B6,
PIN_B6,
PIN_B6,
PIN_B6,
PIN_B6,
PIN_B6,
PIN_B6,
PIN_B6,
PIN_B6,
PIN_B6,
PIN_B6,
PIN_B6,

PIN_B5,
PIN_B5,
PIN_B5,
PIN_B5,
PIN_B5,
PIN_B5,
PIN_B5,
PIN_B5,
PIN_B5,
PIN_B5,
PIN_B5, //C1
PIN_B5,

PIN_B4,
PIN_B4,
PIN_B4,
PIN_B4,
PIN_B4,
PIN_B4,
PIN_B4,
PIN_B4,
PIN_B4,
PIN_B4,
PIN_B4,
PIN_B4,
 
PIN_B3, //
PIN_B3, //
PIN_B3, //
PIN_B3, //
PIN_B3, //
PIN_B3, //
PIN_B3, //
PIN_B3, //
PIN_B3, //
PIN_B3, //
PIN_B3, //C
PIN_B3, //C

PIN_B2, //C#
PIN_B2, //C#
PIN_B2, //D
PIN_B2, //D
PIN_B2, //D#
PIN_B2, //D#
PIN_B2, //E
PIN_B2, //E
PIN_B2, //F
PIN_B2, //F
PIN_B2, //F#
PIN_B2, //F#

PIN_B1, //G
PIN_B1, //G
PIN_B1, //G#
PIN_B1, //G#
PIN_B1, //A
PIN_B1, //A
PIN_B1, //A#
PIN_B1, //A#
PIN_B1, //B
PIN_B1, //B

// PIN_B1,
// PIN_B1,
// PIN_B1,
// PIN_B1,
// PIN_B1,
// PIN_B1,

// PIN_C1,
// PIN_C1,
// PIN_C1,
// PIN_C1,

PIN_C1, //C5
PIN_C1, //C5

PIN_C2, //C#5
PIN_C2, //C#5
PIN_C2, //D5
PIN_C2, //D5 
PIN_C2, //D#5
PIN_C2, //D#5
PIN_C2, //E5
PIN_C2, //E5
PIN_C2, //F5
PIN_C2, //F5
PIN_C2, //F#5
PIN_C2, //F#5

PIN_C3, //G5
PIN_C3, //G5
PIN_C3, //G#5
PIN_C3, //G#5
PIN_C3, //A5
PIN_C3, //A5
PIN_C3, //A#5
PIN_C3, //A#5
PIN_C3, //B5 
PIN_C3, //B5
PIN_C3, //C76  
PIN_C3, //C76

PIN_C4, //C#6
PIN_C4, //C#6
PIN_C4, //D6
PIN_C4, //D6 
PIN_C4, //D#6
PIN_C4, //D#6
PIN_C4, //E6
PIN_C4, //E6
PIN_C4, //F6
PIN_C4, //F6
PIN_C4, //F#6
PIN_C4, //F#6

PIN_C5, //G6
PIN_C5, //G6
PIN_C5, //G#6 
PIN_C5, //G#6
PIN_C5, //A6 93
PIN_C5, //A6
PIN_C5, //A#6 94
PIN_C5, //A#6
PIN_C5, //B6  95
PIN_C5, //B6 
PIN_C5, //C7  96 
PIN_C5  //C7

};
byte input_pins [] = {

//***************************************************************************************************************************************
//1º / 12º grupo de 1 tecla/nota - // #define PIN_B ??????????
PIN_A6, //C - 36
PIN_A5, //C

//***************************************************************************************************************************************
//2º / 11º grupo de 6 teclas/notas - // #define PIN_B ????????
PIN_A7, //C#2 - 37
PIN_A8, //C#2

PIN_A10,   //D2  - 38
PIN_A9,    //D2

PIN_A11,   //D#2 - 39
PIN_A12,   //D#2

PIN_A2,    //E2  - 40
PIN_A1,    //E2

PIN_A3,    //F2  - 41
PIN_A4,    //F2

PIN_A6,    //F#2 - 42
PIN_A5,    //F#2

//***************************************************************************************************************************************
//3º / 10º grupo de 6 teclas/notas - // #define PIN_B ????????
PIN_A7, //G2 - 43
PIN_A8, //G2

PIN_A10,   //Ab2 - 44
PIN_A9,    //Ab2

PIN_A11,   //A2  - 45
PIN_A12,   //A2

PIN_A2,    //Bb2 - 46
PIN_A1,    //Bb2

PIN_A3,    //B2  - 47
PIN_A4,    //B2

PIN_A6,    //C3  - 48
PIN_A5,    //C3

//***************************************************************************************************************************************
//4º / 9º grupo de 6 teclas/notas - // #define PIN_B ????????
PIN_A7, //C#3 - 49
PIN_A8, //C#3

PIN_A10,   //D3  - 50
PIN_A9,    //D3

PIN_A11,   //D#3 - 51
PIN_A12,   //D#3

PIN_A2,    //E3  - 52
PIN_A1,    //E3

PIN_A3,    //F3  - 53
PIN_A4,    //F3

PIN_A6,    //F#3 - 54
PIN_A5,    //F#3

//***************************************************************************************************************************************
//5º / 8º grupo de 6 teclas/notas - // #define PIN_B ????????
PIN_A7, //G3 - 55
PIN_A8, //G3

PIN_A10,   //Ab3 - 56
PIN_A9,    //Ab3

PIN_A11,   //A3  - 57
PIN_A12,   //A3

PIN_A2,    //Bb3 - 58
PIN_A1,    //Bb3

PIN_A3,    //B3  - 59
PIN_A4,    //B3

PIN_A6,    //C4  - 60
PIN_A5,    //C4

//***************************************************************************************************************************************
//6º / 7º grupo de 6 teclas/notas - // #define PIN_B ????????
PIN_A7, //C#4 - 61
PIN_A8, //C#4

PIN_A10,   //D4  - 62
PIN_A9,    //D4

PIN_A11,   //D#4 - 63
PIN_A12,   //D#4

PIN_A2,    //E54  - 64
PIN_A1,    //E54

PIN_A3,    //F54  - 65
PIN_A4,    //F54

PIN_A6,    //F#4 - 66
PIN_A5,    //F#4

//***************************************************************************************************************************************
//7º / 6º grupo de 5 teclas/notas - // #define PIN_B ????????
PIN_A7, //G4 - 67
PIN_A8, //G4

PIN_A10,   //Ab4 - 68
PIN_A9,    //Ab4

PIN_A11,   //A4  - 69
PIN_A12,   //A4

PIN_A2,    //Bb4 - 70       Faltam a 70 e 71
PIN_A1,    //Bb4

PIN_A3,    //B4  - 71       Faltam a 70 e 71
PIN_A4,    //B4

/=================================== Ref. aos pinos: PIN_C1, PIN_C2, PIN_C3... ==================================================/
//***************************************************************************************************************************************
//8º / 5º grupo de 1 tecla/nota - // #define PIN_C1 // OBS: Somente uma tecla
PIN_A6, //C5 - 72
PIN_A5, //C5

//****************************************************************************************************************************************
//9º / 4º grupo de 6 teclas/notas - // #define PIN_C2
PIN_A7, //C#5 - 73
PIN_A8, //C#5

PIN_A10,   //D5  - 74
PIN_A9,    //D5

PIN_A11,   //D#5 - 75
PIN_A12,   //D#5

PIN_A2,    //E5  - 76
PIN_A1,    //E5

PIN_A3,    //F5  - 77
PIN_A4,    //F5

PIN_A6,    //F#5 - 78
PIN_A5,    //F#5

//****************************************************************************************************************************************
//10º / 3º grupo de 6 teclas/notas - // #define PIN_C3
PIN_A7, //G5 - 79
PIN_A8, //G5

PIN_A10,   //Ab5 - 80
PIN_A9,    //Ab5

PIN_A11,   //A5  - 81
PIN_A12,   //A5

PIN_A2,    //Bb5 - 82
PIN_A1,    //Bb5

PIN_A3,    //B5  - 83
PIN_A4,    //B5

PIN_A6,    //C6  - 84
PIN_A5,    //C6

//****************************************************************************************************************************************
//11º / 2º grupo de 6 teclas/notas - // #define PIN_C4
PIN_A7, //C#6 - 85
PIN_A8, //C#6

PIN_A10,   //D6  - 86
PIN_A9,    //D6

PIN_A11,   //D#6 - 87
PIN_A12,   //D#6

PIN_A2,    //E6  - 88
PIN_A1,    //E6

PIN_A3,    //F6  - 89
PIN_A4,    //F6

PIN_A6,    //F#6 - 90
PIN_A5,    //F#6

//****************************************************************************************************************************************
//12º / 1º grupo de 6 teclas/notas - // #define PIN_C5
PIN_A7, //G6 - 91
PIN_A8, //G6

PIN_A10,   //Ab6 - 92
PIN_A9,    //Ab6

PIN_A11,   //A6  - 93
PIN_A12,   //A6

PIN_A2,    //Bb6 - 94
PIN_A1,    //Bb6

PIN_A3,    //B6  - 95
PIN_A4,    //B6

PIN_A6,    //C7  - 96
PIN_A5     //C7

//***************************************************************************************************************************************
/=================================== Ref. aos pinos: PIN_C1, PIN_C2, PIN_C3... ==================================================/

};

// teclados baratos geralmente têm as teclas pretas mais suaves ou mais duras do que as brancas
// descomente a próxima linha para permitir uma correção suave
// #define BLACK_KEYS_CORRECTION

#ifdef BLACK_KEYS_CORRECTION
#define MULTIPLIER 192 // 127 is the central value (corresponding to 1.0)
byte black_keys[] = {
0,1,0,1,0,0,1,0,1,0,1,0,
0,1,0,1,0,0,1,0,1,0,1,0,
0,1,0,1,0,0,1,0,1,0,1,0,
0,1,0,1,0,0,1,0,1,0,1,0,
0,1,0,1,0,0,1,0,1,0,1,0,
0
};
#endif

//uncomment the next line to inspect the number of scans per seconds
//#define DEBUG_SCANS_PER_SECOND

/*
426 cyles per second (2,35ms per cycle) using standard digitalWrite/digitalRead
896 cyles per second (1,11ms per cycle) using DIO2 digitalWrite2/digitalRead2
*/

//uncoment the next line to get text midi message at output
//#define DEBUG_MIDI_MESSAGE

byte keys_state[KEYS_NUMBER];
unsigned long keys_time[KEYS_NUMBER];
boolean signals[KEYS_NUMBER * 2];
boolean pedal_enabled;
boolean is_sus;

void setup() {

Serial.begin(115200);
int i;
for (i = 0; i < KEYS_NUMBER; i++)
{
keys_state[i] = KEY_OFF;
keys_time[i] = 0;
}
for (byte pin = 0; pin < sizeof(output_pins); pin++)
{
pinMode(output_pins[pin], OUTPUT);
}
for (byte pin = 0; pin < sizeof(input_pins); pin++)
{
pinMode(input_pins[pin], INPUT_PULLUP);
}
pinMode(PEDAL_SUS, INPUT_PULLUP);
is_sus = false;
}

void send_midi_event(byte status_byte, byte key_index, unsigned long time)
{
unsigned long t = time;

#ifdef BLACK_KEYS_CORRECTION
if (black_keys[key_index])
{
t = (t * MULTIPLIER) >> 7;
}
#endif
if (t > MAX_TIME_MS)
t = MAX_TIME_MS;
if (t < MIN_TIME_MS)
t = MIN_TIME_MS;
t -= MIN_TIME_MS;

//unsigned long velocity = 127 - (t * 127 / MAX_TIME_MS_N);
//byte vel = (((velocity * velocity) >> 7) * velocity) >> 7;
byte vel = 127 - map(t, 0, MAX_TIME_MS_N, 0, 90);
//byte vel = 127 - map(t, MIN_TIME_MS, MAX_TIME_MS, 1, 90);
//Serial.println(vel);
// 0 ou tempo minimo?
byte key = 36 + key_index;
#ifdef DEBUG_MIDI_MESSAGE
char out[32];
sprintf(out, "%02X %02X %03d %d", status_byte, key, vel, t);
Serial.println(out);
#else
Serial.write(status_byte);
Serial.write(key);
Serial.write(vel);
#endif
}
void send_midi_eventcc(byte status_byte, byte key_index, unsigned long time)
{
unsigned long t = time;
#ifdef DEBUG_MIDI_MESSAGE
char out[32];
sprintf(out, "%02X %02X %03d %d", status_byte, key_index, vel, t);
Serial.println(out);
#else
Serial.write(status_byte);
Serial.write(key_index);
Serial.write(t);
#endif
}

void loop() {
//
potentiometers();

byte a = digitalRead(PEDAL_SUS);
//Serial.println(a);
if(a == 0){
if(is_sus == true){
send_midi_eventcc(0xB0, 64, 127);
is_sus = false;
}
}
else{
if(is_sus == false){
send_midi_eventcc(0xB0, 64, 0);
is_sus = true;
}
}
#ifdef DEBUG_SCANS_PER_SECOND
static unsigned long cycles = 0;
static unsigned long start = 0;
static unsigned long current = 0;
cycles++;
current = millis();
if (current - start >= 1000)
{
Serial.println(cycles);
cycles = 0;
start = current;
}
#endif

boolean *s = signals;
for (byte i = 0; i < KEYS_NUMBER * 2; i++)
{
byte output_pin = output_pins[i];
byte input_pin = input_pins[i];
digitalWrite2(output_pin, LOW);
*(s++) = !digitalRead2(input_pin);
digitalWrite2(output_pin, HIGH);
}

byte *state = keys_state;
unsigned long *ktime = keys_time;
boolean *signal = signals;
for (byte key = 0; key < KEYS_NUMBER; key++)
{
for (byte state_index = 0; state_index < 2; state_index++)
{
switch (*state)
{
case KEY_OFF:
if (state_index == 0 && *signal)
{
*state = KEY_START;
*ktime = micros();
}
break;
case KEY_START:
if (state_index == 0 && !*signal)
{
*state = KEY_OFF;
break;
}
if (state_index == 1 && *signal)
{
*state = KEY_ON;
unsigned long t= micros() - *ktime;
send_midi_event(0x90, key,t);
}
break;
case KEY_ON:
if (state_index == 1 && !*signal)
{
*state = KEY_RELEASED;
*ktime = micros();
}
break;
case KEY_RELEASED:
if (state_index == 0 && !*signal)
{
*state = KEY_OFF;
unsigned long t= micros() - *ktime;
send_midi_event(0x80, key,t);
}
break;
}
signal++;
}
state++;
ktime++;
}
}

@oxesoft
Copy link
Owner

oxesoft commented Mar 28, 2021 via email

@Willame156
Copy link
Author

Agradeço por sua atenção e velocidade ao me responder.
Mas acredito que era só mau-contato, porque limpei a placa com álcool isopropílico e passou a funcionar bem. Obrigado.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants