Я строю стартовую площадку с Arduino Leonardo. Этот проект состоит из кнопок, подключенных к Arduino Leonardo, который затем отправляет MIDI-сигналы (акроним для цифрового интерфейса музыкальных инструментов) на P C всякий раз, когда нажимается любая из этих кнопок. P C впоследствии воспроизводит звуки музыки c всякий раз, когда он получает MIDI-сигналы от Arduino.
Очень важно иметь возможность нажимать sh различных кнопок одновременно, чтобы одновременно можно было воспроизводить несколько звуков. Мне удалось почти завершить sh проект. Единственное, чего я еще не достиг, это, собственно, кодирование чего-то, что позволяет Arduino одновременно определять сигналы кнопки.
Вот мой код:
#include <frequencyToNote.h>
#include <MIDIUSB.h>
#include <MIDIUSB_Defs.h>
#include <pitchToFrequency.h>
#include <pitchToNote.h>
const int buttonA = 0;
const int buttonB = 1;
const int buttonC = 2;
const int buttonD = 3;
const int buttonE = 4;
const int buttonF = 5;
const int buttonG = 6;
const int buttonH = 7;
const int buttonI = 8;
const int buttonJ = 9;
const int buttonK = 10;
const int buttonL = 11;
const int buttonM = 12;
const int buttonN = 13;
const int buttonO = A0;
const int buttonP = A1;
#define CHANNEL 1 // MIDI channel number
boolean buttonAState;
boolean buttonBState;
boolean buttonCState;
boolean buttonDState;
boolean buttonEState;
boolean buttonFState;
boolean buttonGState;
boolean buttonHState;
boolean buttonIState;
boolean buttonJState;
boolean buttonKState;
boolean buttonLState;
boolean buttonMState;
boolean buttonNState;
boolean buttonOState;
boolean buttonPState;
void setup() {
pinMode(buttonA, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
pinMode(buttonC, INPUT_PULLUP);
pinMode(buttonD, INPUT_PULLUP);
pinMode(buttonE, INPUT_PULLUP);
pinMode(buttonF, INPUT_PULLUP);
pinMode(buttonG, INPUT_PULLUP);
pinMode(buttonH, INPUT_PULLUP);
pinMode(buttonI, INPUT_PULLUP);
pinMode(buttonJ, INPUT_PULLUP);
pinMode(buttonK, INPUT_PULLUP);
pinMode(buttonL, INPUT_PULLUP);
pinMode(buttonM, INPUT_PULLUP);
pinMode(buttonN, INPUT_PULLUP);
pinMode(buttonO, INPUT_PULLUP);
pinMode(buttonP, INPUT_PULLUP);
}
int noterest(){
delay(70);
}
void loop() {
buttonAState = digitalRead(buttonA);
buttonBState = digitalRead(buttonB);
buttonCState = digitalRead(buttonC);
buttonDState = digitalRead(buttonD);
buttonEState = digitalRead(buttonE);
buttonFState = digitalRead(buttonF);
buttonGState = digitalRead(buttonG);
buttonHState = digitalRead(buttonH);
buttonIState = digitalRead(buttonI);
buttonJState = digitalRead(buttonJ);
buttonKState = digitalRead(buttonK);
buttonLState = digitalRead(buttonL);
buttonMState = digitalRead(buttonM);
buttonNState = digitalRead(buttonN);
buttonOState = digitalRead(buttonO);
buttonPState = digitalRead(buttonP);
if (buttonAState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 36, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 36, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonBState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 37, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 37, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonCState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 38, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 38, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonDState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 39, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 39, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonEState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 40, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 40, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonFState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 41, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 41, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonGState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 42, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 42, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonHState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 43, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 43, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonIState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 44, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 44, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonJState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 45, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 45, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonKState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 46, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 46, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonLState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 47, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 47, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonMState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 48, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 48, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonNState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 49, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 49, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonOState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 50, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 50, 100};
MidiUSB.sendMIDI(noteOff);
}
if (buttonPState == HIGH)
{
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 51, 100};
MidiUSB.sendMIDI(noteOn);
noterest();
} else {
midiEventPacket_t noteOff = {0x08, 0x80 | 1, 51, 100};
MidiUSB.sendMIDI(noteOff);
}
}
Как вы можете видеть, я предполагаю, что Arduino считывает оператор IF на l oop линейно. Это предположение верно? Как я могу достичь своей цели, читая кнопки одновременно? Есть ли другой способ улучшить мой код, чтобы сделать его более эффективным?
Заранее спасибо!