Arduino中的多个IF

问题描述 投票:0回答:1

我正在使用Arduino Leonardo构建启动板。该项目包含连接到Arduino Leonardo的按钮,然后只要按下这些按钮中的任何一个,就会将MIDI(乐器数字接口的缩写)信号发送到PC。随后,只要PC从arduino接收MIDI信号,它就会播放音乐。

能够同时按下各种按钮,以便可以同时播放多种声音,这一点至关重要。我已经设法完成了这个项目。准确地说,我还没有完成的事情就是编码某种东西,以使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语句。此假设正确吗?如何实现同时阅读按钮的目标?我还有其他方法可以改善代码以使其更高效吗?

谢谢你!

loops if-statement arduino arduino-uno arduino-ide
1个回答
0
投票

首先使用数组来存储您的变量:

 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(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行的紧凑代码

© www.soinside.com 2019 - 2024. All rights reserved.