Arduino 中的多个 IF
Multiple IFs in Arduino
我正在使用 Arduino Leonardo 构建一个启动板。该项目由连接到 Arduino Leonardo 的按钮组成,只要按下这些按钮中的任何一个,它就会向 PC 发送 MIDI(乐器数字接口的首字母缩写词)信号。随后,只要从 arduino 接收到 MIDI 信号,PC 就会播放音乐。
能够同时按下多个按钮非常重要,这样可以同时播放多种声音。我几乎完成了这个项目。我唯一还没有完成的事情是,确切地说,编码允许 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 语句。
这个假设是否正确?如何实现同时读取按钮的目标?有没有其他方法可以改进我的代码以使其更高效?
提前致谢!
首先使用数组来存储你的变量:
uint32_t buttonStates = 0;
const int button[16] = {0,1,2,3,4,5,...}; // its only the first pins
用 for 循环初始化
for (uint8_t i=0;i<16;i++) {
pinMode(buttons[i], INPUT_PULLUP);
}
以二进制编码捕捉按键(buttonStates 循环)
for (uint8_t i=0;i<16;i++) {
if (digitalRead(buttons[i]) == 1) buttonStates += pow(2,i);
}
因此,如果您按下两个按钮,您将获得组合值,而不是如果我们使用 switch case
switch(buttonStates){
case 1: //do whatever you do
break;
......
default: //do what if novalue is generated
break;
}
但是因为你的 midi 数据包是一样的
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 37, 100};
你可以引入变音
midiEventPacket_t noteOn = {0x09, 0x90 | 1, tone, 100};
所以你的 buttonstate 循环可以替换为
for (uint8_t i=0;i<16;i++) {
if (digitalRead(buttons[i]) == HIGH) {
tone = 36 + i;
midiEventPacket_t noteOn = {0x09, 0x90 | 1, tone, 100};
MidiUSB.sendMIDI(noteOn);
}
else {
tone = 36 + i;
midiEventPacket_t noteOff = {0x08, 0x80 | 1, tone, 100};
MidiUSB.sendMIDI(noteOff);
}
}
在这种情况下定义了音调uint tone = 0;
所以基本上我们可以将您的程序减少到 40-50 行紧凑代码
我正在使用 Arduino Leonardo 构建一个启动板。该项目由连接到 Arduino Leonardo 的按钮组成,只要按下这些按钮中的任何一个,它就会向 PC 发送 MIDI(乐器数字接口的首字母缩写词)信号。随后,只要从 arduino 接收到 MIDI 信号,PC 就会播放音乐。
能够同时按下多个按钮非常重要,这样可以同时播放多种声音。我几乎完成了这个项目。我唯一还没有完成的事情是,确切地说,编码允许 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 语句。 这个假设是否正确?如何实现同时读取按钮的目标?有没有其他方法可以改进我的代码以使其更高效?
提前致谢!
首先使用数组来存储你的变量:
uint32_t buttonStates = 0;
const int button[16] = {0,1,2,3,4,5,...}; // its only the first pins
用 for 循环初始化
for (uint8_t i=0;i<16;i++) {
pinMode(buttons[i], INPUT_PULLUP);
}
以二进制编码捕捉按键(buttonStates 循环)
for (uint8_t i=0;i<16;i++) {
if (digitalRead(buttons[i]) == 1) buttonStates += pow(2,i);
}
因此,如果您按下两个按钮,您将获得组合值,而不是如果我们使用 switch case
switch(buttonStates){
case 1: //do whatever you do
break;
......
default: //do what if novalue is generated
break;
}
但是因为你的 midi 数据包是一样的
midiEventPacket_t noteOn = {0x09, 0x90 | 1, 37, 100};
你可以引入变音
midiEventPacket_t noteOn = {0x09, 0x90 | 1, tone, 100};
所以你的 buttonstate 循环可以替换为
for (uint8_t i=0;i<16;i++) {
if (digitalRead(buttons[i]) == HIGH) {
tone = 36 + i;
midiEventPacket_t noteOn = {0x09, 0x90 | 1, tone, 100};
MidiUSB.sendMIDI(noteOn);
}
else {
tone = 36 + i;
midiEventPacket_t noteOff = {0x08, 0x80 | 1, tone, 100};
MidiUSB.sendMIDI(noteOff);
}
}
在这种情况下定义了音调uint tone = 0;
所以基本上我们可以将您的程序减少到 40-50 行紧凑代码