Estou criando um sistema na onde um programa musical que uso, ele manda informações midi para o arduino, quando ele manda a nota ligada, o LED liga, mas quando ele manda a nota delisgada, o LED continua ligado. Como faço para resolver isso.
Segue abaixo o codigo:
int val;
int availableBytesToRead;
int prevstate = 1;
int midC = 60; // MIDI note value for middle C on a standard keyboard
byte noteON = 144; // the function of the button, 144 = Channel 1 Note on
byte note = 63;
byte velocity = 127;
byte buf[256];
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(13, OUTPUT); // The LED on GND and PIN 34
Serial.begin(115200);
}
// the loop function runs over and over again forever
void loop() {
// Read incoming MIDI from USB and control the LED
availableBytesToRead = Serial.available();
if (availableBytesToRead > 0) {
Serial.readBytes(buf, availableBytesToRead);
if(buf[0] == 0x90){
digitalWrite(13, HIGH);
}
}
delay(10);
}
void MIDImessage(byte status, byte data1, byte data2)
{
Serial.write(status);
Serial.write(data1);
Serial.write(data2);
}
Se bem me lembro (já faz muito tempo que não trabalho com MIDI), o código para note on é 0x09, não 0x90 e o código para note off é 0x08. Então seu " if " poderia ficar assim:
Funcionou para desligar o LED.Mas você teria como me ajudar é que eu também quero identificar qual a nota e dependendo de qual a nota ligar um LED espessifico.
Eu não tenho um dispositivo midi para gerar notas e verificar, tente o seguinte código para ver o que acontece.
/*
Por exemplo para a nota 62 que acende um led conectado ao pino 2
e nota 63 que acende um led conectado ao pino 3
*/
byte commandByte, noteByte, velocityByte;
const byte noteON = 144; // 144 = Channel 1 Note on
const noteOFF = 128; // 128 = Channel 1 Note 0ff
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT); // pino de led para nota 62
pinMode(3, OUTPUT); // pino de led para nota 63
//Adiciona os leds necessários.
}
void loop() {
if (Serial.available()) {
commandByte = Serial.read();//read first byte
noteByte = Serial.read();//read next byte
velocityByte = Serial.read();//read final byte
switch (noteByte) {
//Nota 63
case 63 :
switch (commandByte) {
case noteON :
digitalWrite (2, HIGH);
break;
case noteOFF :
digitalWrite (2, LOW);
break;
}
break;
//Nota 64
case 64 :
switch (commandByte) {
case noteON :
digitalWrite (3, HIGH);
break;
case noteOFF:
digitalWrite (3, LOW);
break;
}
break;
//Adiciona as notas necessárias.
}
}
}
Arduino: 1.8.19 (Windows 10), Placa:"Arduino Uno"
C:\Users\guilh\AppData\Local\Temp\arduino_modified_sketch_239263\sketch_jul12a.ino: In function 'void loop()':
sketch_jul12a:57:16: error: the value of 'noteON' is not usable in a constant expression
case noteON :
^~~~~~
C:\Users\guilh\AppData\Local\Temp\arduino_modified_sketch_239263\sketch_jul12a.ino:13:6: note: 'byte noteON' is not const
byte noteON = 144; // 144 = Channel 1 Note on
^~~~~~
sketch_jul12a:57:16: error: the value of 'noteON' is not usable in a constant expression
case noteON :
^~~~~~
C:\Users\guilh\AppData\Local\Temp\arduino_modified_sketch_239263\sketch_jul12a.ino:13:6: note: 'byte noteON' is not const
byte noteON = 144; // 144 = Channel 1 Note on
^~~~~~
sketch_jul12a:63:16: error: the value of 'noteOFF' is not usable in a constant expression
case noteOFF :
^~~~~~~
C:\Users\guilh\AppData\Local\Temp\arduino_modified_sketch_239263\sketch_jul12a.ino:15:6: note: 'byte noteOFF' is not const
byte noteOFF = 128; // 128 = Channel 1 Note 0ff
^~~~~~~
sketch_jul12a:63:16: error: the value of 'noteOFF' is not usable in a constant expression
case noteOFF :
^~~~~~~
C:\Users\guilh\AppData\Local\Temp\arduino_modified_sketch_239263\sketch_jul12a.ino:15:6: note: 'byte noteOFF' is not const
byte noteOFF = 128; // 128 = Channel 1 Note 0ff
^~~~~~~
sketch_jul12a:79:16: error: the value of 'noteON' is not usable in a constant expression
case noteON :
^~~~~~
C:\Users\guilh\AppData\Local\Temp\arduino_modified_sketch_239263\sketch_jul12a.ino:13:6: note: 'byte noteON' is not const
byte noteON = 144; // 144 = Channel 1 Note on
^~~~~~
sketch_jul12a:79:16: error: the value of 'noteON' is not usable in a constant expression
case noteON :
^~~~~~
C:\Users\guilh\AppData\Local\Temp\arduino_modified_sketch_239263\sketch_jul12a.ino:13:6: note: 'byte noteON' is not const
byte noteON = 144; // 144 = Channel 1 Note on
^~~~~~
sketch_jul12a:85:16: error: the value of 'noteOFF' is not usable in a constant expression
case noteOFF :
^~~~~~~
C:\Users\guilh\AppData\Local\Temp\arduino_modified_sketch_239263\sketch_jul12a.ino:15:6: note: 'byte noteOFF' is not const
byte noteOFF = 128; // 128 = Channel 1 Note 0ff
^~~~~~~
sketch_jul12a:85:16: error: the value of 'noteOFF' is not usable in a constant expression
case noteOFF :
^~~~~~~
C:\Users\guilh\AppData\Local\Temp\arduino_modified_sketch_239263\sketch_jul12a.ino:15:6: note: 'byte noteOFF' is not const
byte noteOFF = 128; // 128 = Channel 1 Note 0ff
^~~~~~~
exit status 1
the value of 'noteON' is not usable in a constant expression
Se meu erro ao definir as variáveis devem ser constantes. O código corrigido:
/*
Por exemplo para a nota 62 que acende um led conectado ao pino 2
e nota 63 que acende um led conectado ao pino 3
*/
byte commandByte, noteByte, velocityByte;
const byte noteON = 144; // 144 = Channel 1 Note on
const byte noteOFF = 128; // 128 = Channel 1 Note 0ff
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT); // pino de led para nota 62
pinMode(3, OUTPUT); // pino de led para nota 63
//Adiciona os leds necessários.
}
void loop() {
if (Serial.available()) {
commandByte = Serial.read();//read first byte
noteByte = Serial.read();//read next byte
velocityByte = Serial.read();//read final byte
Serial.println (commandByte);
Serial.println ( noteByte);
Serial.println (velocityByte);
switch (noteByte) {
//Nota 63
case 63 :
switch (commandByte) {
case noteON :
digitalWrite (2, HIGH);
break;
case noteOFF :
digitalWrite (2, LOW);
break;
}
break;
//Nota 64
case 64 :
switch (commandByte) {
case noteON :
digitalWrite (3, HIGH);
break;
case noteOFF:
digitalWrite (3, LOW);
break;
}
break;
//Adiciona as notas necessárias.
}
}
}
Eu alterei para o 3 para entrada 13 do led da placa. E estou usando um conversor chamado Hairless MIDI Serial, para converter os dados midi do programa, para serial na placa.
Modifiquei o código do post#6 para que ele imprima a nota recebida no monitor serial, recarregue-a e envie uma nota para o arduino e me diga o que o monitor serial mostra.