这是一个非常初学者的问题。我需要一些关于如何将这两个草图合并为一个的指导。我只有一些非常初学的Arduino语言知识。
我已经成功地独立测试了两个草图,我现在只需要以某种方式将它们编译在一起。
任何帮助或指导将不胜感激!谢谢你提前!
草图1:
// 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);
}
答案 0 :(得分:0)
好的,乍一看你的变量应该没问题(虽然你似乎没有在草图2中使用&#39; int&#39;):
#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);
}
最后一位将两个环拼接在一起。据我所知,这个频道&#39;变量是链接它们的东西,即当你说
时usbMIDI.sendControlChange(7, current, 1);
最后一个&#39; 1&#39;是通道编号,可在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设备所以我无法测试这个...