[英]Merge Arduino Sketches (MIDI)
This is a very beginner level question. 这是一个非常初学者的问题。 I need some guidance on how to merge these two sketches into one. 我需要一些有关如何将这两个草图合并为一个的指导。 I have only some very beginner level knowledge of Arduino language. 我对Arduino语言只有一些非常初级的知识。
I have successfully tested both sketches independently, I just need to compile them together in some way now. 我已经成功地独立测试了两个草图,现在只需要以某种方式将它们编译在一起即可。
Any help or guidance would be appreciated! 任何帮助或指导将不胜感激! Thank you advance! 谢谢你提前!
Sketch 1: 草图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);
}
}
Sketch 2: 草图2:
int previous;
int current;
void setup() {
}
void loop () {
current = map(analogRead(11), 136, 1023, 0 , 127);
usbMIDI.sendControlChange(7, current, 1);
delay(5);
}
Ok, at first glance your variables should be fine (although you don't seem to be using 'int previous' in sketch 2?): 好的,乍一看,您的变量应该没问题(尽管您似乎未在草图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;
Setup also looks straightforward, as you don't set anything up in the 2nd sketch: 安装程序看起来也很简单,因为您没有在第二个草图中进行任何设置:
void setup()
{
pinMode(2, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
}
The final bit is stitching together the two loops. 最后一位将两个循环缝合在一起。 From what I can tell, the 'channel' variable is what links them, ie when you say 据我所知,“ channel”变量是链接它们的内容,即当您说
usbMIDI.sendControlChange(7, current, 1);
the last '1' is the channel number, selectable from 1 to 16. So does this mean you could do the following: 最后一个“ 1”是通道号,可以在1到16之间选择。这是否意味着您可以执行以下操作:
usbMIDI.sendControlChange(7, current, channel);
and if so, do you want to change/control channels before or after your sendNoteOn/Off stuff? 如果是这样,您是否要在sendNoteOn / Off之前或之后更改/控制频道? eg if before, would the following work: 例如,如果以前可以进行以下工作:
// 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);
}
}
I don't have a midi device so I can't test this... 我没有Midi设备,所以无法测试...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.