Merge Arduino Sketches (MIDI)

599 Views Asked by At

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.

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:

// 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:

int previous;
int current; 

void setup() {
}

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

There are 1 best solutions below

0
On

Ok, at first glance your variables should be fine (although you don't seem to be using 'int previous' in sketch 2?):

#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

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:

usbMIDI.sendControlChange(7, current, channel);

and if so, do you want to change/control channels before or after your sendNoteOn/Off stuff? 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...