Midi Foot Controller - Need help - Can not figure out Midi receive

Status
Not open for further replies.

miasei

Member
I'm working on a Midi Foot Controller and use a Teensy 2.0. I made a workable code (sorry for that I am a newbie and this is only from parts of other projects) for 3 buttons with toggle function and sending Midi from Button 1 and Button 2. This is working quite well. Tried to add a midi receive, but it did not work. I don't understand how to do that.

So if a software (via preset) sends an on message from control 85 (for button1), the LED1 of the controller should go on (and vice versa). Here is the working code with Midi send and Buttons/LEDs.


Code:

//Buttons, LEDs
#include <ButtonSwitch.h>

int buttonPin1 = 0; // define Pin XXX as the input of the button signal
int LED1 = 11; // define Pin XXX as the input of the LED signal
int buttonPin2 = 1;
int LED2 = 12;
int buttonPin3 = 2;
int LED3 = 13;

// declare button as a data type of Button_Switch connected to the buttonPin (in this case pin 12 of the Arduino),
buttonSwitch button1(buttonPin1);
buttonSwitch button2(buttonPin2);
buttonSwitch button3(buttonPin3);

// the setup function runs once when you press reset or power the board


//Midi Send Bounce
#include <Bounce.h> // Bounce library makes button change detection easy
const int channel = 1;

Bounce switch1 = Bounce(buttonPin1, 5); // 5 = 5 ms debounce time
Bounce switch2 = Bounce(buttonPin2, 5); // which is appropriate for good

void setup() {

pinMode(LED1, OUTPUT); // initialize digital pin LED_BUILTIN as an output
pinMode(buttonPin1, INPUT_PULLUP); // initialize digital pin for the push button as an input

pinMode(LED2, OUTPUT);
pinMode(buttonPin2, INPUT_PULLUP);

pinMode(LED3, OUTPUT);
pinMode(buttonPin3, INPUT_PULLUP);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(LED1, button1.SwitchI()); // turns the builtin LED on or off if the button was pushed
digitalWrite(LED2, button2.SwitchI());
digitalWrite(LED3, button3.SwitchI());

delay(10); // little delay needed to debounce the push button


//Loop Midi Send
switch1.update();
switch2.update();

// Note On messages when each button is pressed
if (switch1.fallingEdge()) {
usbMIDI.sendControlChange(85, 127, 1);
}
if (switch2.fallingEdge()) {
usbMIDI.sendControlChange(86, 127, 1);
}

// MIDI Controllers should discard incoming MIDI messages.
while (usbMIDI.read()) {
}
}



Maybe someone could help me to write an add for this code to receive midi message as discribed. Thanks a lot!!!
 
I could also figure out a working code for Midi receive. It works great with the software as it should, but I can not figure out, how to combine the code above with this

Code:
int LED1 = 11;

void setup() {
 pinMode(LED1, OUTPUT);
 usbMIDI.setHandleControlChange(myControlChange);
}

void loop() {
 usbMIDI.read();
}
void myControlChange(byte channel, byte control, byte value) {
  if(channel == 1 && control == 86 && value == 127)
 digitalWrite(LED1, HIGH);

  if(channel == 1 && control == 86 && value == 0)
 digitalWrite(LED1, LOW);
}


Please help me to combine this code with this

Code:
//Buttons, LEDs
#include <ButtonSwitch.h>

int buttonPin1 = 0; // define Pin XXX as the input of the button signal
int LED1 = 11; // define Pin XXX as the input of the LED signal
int buttonPin2 = 1;
int LED2 = 12;
int buttonPin3 = 2;
int LED3 = 13;

// declare button as a data type of Button_Switch connected to the buttonPin (in this case pin 12 of the Arduino),
buttonSwitch button1(buttonPin1);
buttonSwitch button2(buttonPin2);
buttonSwitch button3(buttonPin3);

// the setup function runs once when you press reset or power the board


//Midi Send Bounce
#include <Bounce.h> // Bounce library makes button change detection easy
const int channel = 1;

Bounce switch1 = Bounce(buttonPin1, 5); // 5 = 5 ms debounce time
Bounce switch2 = Bounce(buttonPin2, 5); // which is appropriate for good

void setup() {

pinMode(LED1, OUTPUT); // initialize digital pin LED_BUILTIN as an output
pinMode(buttonPin1, INPUT_PULLUP); // initialize digital pin for the push button as an input

pinMode(LED2, OUTPUT);
pinMode(buttonPin2, INPUT_PULLUP);

pinMode(LED3, OUTPUT);
pinMode(buttonPin3, INPUT_PULLUP);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(LED1, button1.SwitchI()); // turns the builtin LED on or off if the button was pushed
digitalWrite(LED2, button2.SwitchI());
digitalWrite(LED3, button3.SwitchI());

delay(10); // little delay needed to debounce the push button


//Loop Midi Send
switch1.update();
switch2.update();

// Note On messages when each button is pressed
if (switch1.fallingEdge()) {
usbMIDI.sendControlChange(85, 127, 1);
}
if (switch2.fallingEdge()) {
usbMIDI.sendControlChange(86, 127, 1);
}

// MIDI Controllers should discard incoming MIDI messages.
while (usbMIDI.read()) {
}
}
 
A first iteration of your code with MIDI receive added would be:
Code:
//Buttons, LEDs
#include <ButtonSwitch.h>

int buttonPin1 = 0; // define Pin XXX as the input of the button signal
int LED1 = 11; // define Pin XXX as the input of the LED signal
int buttonPin2 = 1;
int LED2 = 12;
int buttonPin3 = 2;
int LED3 = 13;

// declare button as a data type of Button_Switch connected to the buttonPin (in this case pin 12 of the Arduino),
buttonSwitch button1(buttonPin1);
buttonSwitch button2(buttonPin2);
buttonSwitch button3(buttonPin3);

// the setup function runs once when you press reset or power the board


//Midi Send Bounce
#include <Bounce.h> // Bounce library makes button change detection easy
const int channel = 1;

Bounce switch1 = Bounce(buttonPin1, 5); // 5 = 5 ms debounce time
Bounce switch2 = Bounce(buttonPin2, 5); // which is appropriate for good

void setup()
{

  pinMode(LED1, OUTPUT); // initialize digital pin LED_BUILTIN as an output
  pinMode(buttonPin1, INPUT_PULLUP); // initialize digital pin for the push button as an input

  pinMode(LED2, OUTPUT);
  pinMode(buttonPin2, INPUT_PULLUP);

  pinMode(LED3, OUTPUT);
  pinMode(buttonPin3, INPUT_PULLUP);

  usbMIDI.setHandleControlChange(myControlChange);
}

// the loop function runs over and over again forever
void loop()
{
  digitalWrite(LED1, button1.SwitchI()); // turns the builtin LED on or off if the button was pushed
  digitalWrite(LED2, button2.SwitchI());
  digitalWrite(LED3, button3.SwitchI());

  delay(10); // little delay needed to debounce the push button


//Loop Midi Send
  switch1.update();
  switch2.update();

// Note On messages when each button is pressed
  if (switch1.fallingEdge()) {
    usbMIDI.sendControlChange(85, 127, 1);
  }
  if (switch2.fallingEdge()) {
    usbMIDI.sendControlChange(86, 127, 1);
  }

// MIDI Controllers should discard incoming MIDI messages.
  while (usbMIDI.read()) {
  }
}

void myControlChange(byte channel, byte control, byte value)
{
  if(channel == 1 && control == 0x86 && value == 127)
    digitalWrite(LED1, HIGH);

  if(channel == 1 && control == 0x86 && value == 0)
    digitalWrite(LED1, LOW);
}

I've left the delay(10) statement in but I don't think it is needed. The Bounce library debounces the pushbutton switches and I'm not sure that the buttonPins need debouncing.

Pete
 
My bad. I meddled with the control numbers and didn't change them back again. The myControlChange function should be this:
Code:
void myControlChange(byte channel, byte control, byte value)
{
  if(channel == 1 && control == 86 && value == 127)
    digitalWrite(LED1, HIGH);

  if(channel == 1 && control == 86 && value == 0)
    digitalWrite(LED1, LOW);
}

Where did you get the ButtonSwitch library? I can't find it.

Why do you have both the ButtonSwitch and Bounce libraries using the same pins?

Pete
 
FYI: Just tested part of this code on a Teensy2 and it receives Control Change message 86 on channel 1, turning the LED on when it receives a volume of 127 and turning it off when it receives a volume of zero.
I used Pocket MIDI on Windows 10 to send the CC message.

Pete
 
Hey thanks for this. I will try it. I took this libraries because I am a complete noob. These things are part of a few examples. So I have to learn a lot. This helps me a lot.
 
A guy from Arduino forum sent me a hint to a great library which reduces my code drastically. Maybe you are interested.

It is called control surface and especially for midi purposes i think.

Code:
 #include <Control_Surface.h> // Include the Control Surface library
 
USBMIDI_Interface midi; // Instantiate a MIDI over USB interface.
 
// Instantiate an array of CCButton objects that send MIDI control
// change messages whenever the button is pressed/released
CCButton buttons[] {
  {0, 85}, // button pin number, controller number
  {1, 86},
  {2, 87},
};
// Create an array of CCValueLED objects that turn on/off an LED
// depending on the MIDI control change messages they receive
CCValueLED leds[] {
  {11, 85}, // led pin number, controller number
  {12, 86},
  {13, 87},
};

void setup() {
  Control_Surface.begin(); // Initialize Control Surface
}
 
void loop() {
  Control_Surface.loop(); // Update the Control Surface
}
 
Status
Not open for further replies.
Back
Top