0

这是一个非常初级的问题。我需要一些关于如何将这两个草图合并为一个的指导。我对 Arduino 语言只有一些非常初级的知识。

我已经成功地独立测试了这两个草图,我现在只需要以某种方式将它们编译在一起。

任何帮助或指导将不胜感激!谢谢提前!

草图一:

// stomp using usb midi

#include <Bounce.h>

// midi channel
int channel = 1;

Bounce button1 = Bounce(2, 5);
Bounce button2 = Bounce(4, 5);
Bounce button3 = Bounce(6, 5);
Bounce button4 = Bounce(8, 5);

void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);

}

void loop()
{
  button1.update();
  button2.update();
  button3.update();
  button4.update();

  if (button1.fallingEdge())
  {
    usbMIDI.sendNoteOn(60, 99, channel); // 60 = C4
  }
  if (button2.fallingEdge())
  {
    usbMIDI.sendNoteOn(62, 99, channel); // 62 = D4
  }
  if (button3.fallingEdge())
  {
    usbMIDI.sendNoteOn(64, 99, channel); // 64 = E4
  }
  if (button4.fallingEdge())
  {
    usbMIDI.sendNoteOn(68, 99, channel); // 68 = F4
  }


//Note On message when button is released

if (button1.risingEdge()) {
    usbMIDI.sendNoteOff(60, 0, channel); 
  }
  if (button2.risingEdge()) {
    usbMIDI.sendNoteOff(62, 0, channel); 
  }
  if (button3.risingEdge()) {
    usbMIDI.sendNoteOff(64, 0, channel); 
  }
  if (button4.risingEdge()) {
    usbMIDI.sendNoteOff(68, 0, channel); 

}
}

草图2:

int previous;
int current; 

void setup() {
}

void loop () {
  current = map(analogRead(11), 136, 1023, 0 , 127);
  usbMIDI.sendControlChange(7, current, 1);
  delay(5); 
}
4

1 回答 1

0

好的,乍一看你的变量应该没问题(虽然你似乎没有在草图2中使用'int previous'?):

#include <Bounce.h>

int channel = 1; 
Bounce button1 = Bounce(2, 5); 
Bounce button2 = Bounce(4, 5); 
Bounce button3 = Bounce(6, 5); 
Bounce button4 = Bounce(8, 5); 
int previous; 
int current;

设置看起来也很简单,因为您没有在第二个草图中设置任何内容:

void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);

}

最后一点是将两个循环缝合在一起。据我所知,“通道”变量是链接它们的原因,即当你说

usbMIDI.sendControlChange(7, current, 1);

最后一个“1”是频道号,可以从 1 到 16 选择。这是否意味着您可以执行以下操作:

usbMIDI.sendControlChange(7, current, channel);

如果是这样,您想在 sendNoteOn/Off 之前或之后更改/控制频道吗?例如,如果以前,以下工作:

// stomp using usb midi

#include <Bounce.h>

// midi channel
int channel = 1;
Bounce button1 = Bounce(2, 5);
Bounce button2 = Bounce(4, 5);
Bounce button3 = Bounce(6, 5);
Bounce button4 = Bounce(8, 5);
int previous;
int current;

void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);    
}

void loop()
{
  current = map(analogRead(11), 136, 1023, 0 , 127);
  usbMIDI.sendControlChange(7, current, channel);
  delay(5); 

  button1.update();
  button2.update();
  button3.update();
  button4.update();

  if (button1.fallingEdge())
  {
    usbMIDI.sendNoteOn(60, 99, channel); // 60 = C4
  }
  if (button2.fallingEdge())
  {
    usbMIDI.sendNoteOn(62, 99, channel); // 62 = D4
  }
  if (button3.fallingEdge())
  {
    usbMIDI.sendNoteOn(64, 99, channel); // 64 = E4
  }
  if (button4.fallingEdge())
  {
    usbMIDI.sendNoteOn(68, 99, channel); // 68 = F4
  }


//Note On message when button is released

if (button1.risingEdge()) {
    usbMIDI.sendNoteOff(60, 0, channel); 
  }
  if (button2.risingEdge()) {
    usbMIDI.sendNoteOff(62, 0, channel); 
  }
  if (button3.risingEdge()) {
    usbMIDI.sendNoteOff(64, 0, channel); 
  }
  if (button4.risingEdge()) {
    usbMIDI.sendNoteOff(68, 0, channel); 

}
}

我没有midi设备,所以我无法测试这个......

于 2014-06-01T09:16:37.203 回答