Author Topic: External Midi Controller  (Read 3298 times)

Peetem

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: External Midi Controller
« Reply #15 on: March 05, 2023, 07:51:08 AM »
I put in some debugging code and while, for whatever reason, not all the buttons work, the state is changing:

Button :
4
Toggle State :
1
Button :
4
Toggle State :
0

Randall (Admin)

  • Administrator
  • Full Member
  • *****
  • Posts: 221
    • View Profile
Re: External Midi Controller
« Reply #16 on: March 05, 2023, 11:32:23 AM »
That's good.  Is it your first or last button not working?  If so, you might have an "off by one" indexing problem.

Is the MIDI message being sent/received?

If not, make sure your MIDI_CC_NUMS is indexed too:

Code: [Select]
usbMIDI.sendControlChange (MIDI_CC_NUMS[i], 0, MIDI_CHAN);

Peetem

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: External Midi Controller
« Reply #17 on: March 05, 2023, 11:46:25 AM »
That's good.  Is it your first or last button not working?  If so, you might have an "off by one" indexing problem.

Is the MIDI message being sent/received?

If not, make sure your MIDI_CC_NUMS is indexed too:

Code: [Select]
usbMIDI.sendControlChange (MIDI_CC_NUMS[i], 0, MIDI_CHAN);

Of the 8 buttons, 4 of them aren't registering.  I'm putting some more debugging code to see what the output looks like.  Will post soon.....

Peetem

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: External Midi Controller
« Reply #18 on: March 05, 2023, 12:48:24 PM »
Here's the latest code - it prints the toggle change, the Button/CC#, and either 0 or 127 (if a button is pushed/not pushed).

Code: [Select]
/* Teensy 4.1 DIY USB-MIDI controller.
*/

#include <Bounce.h>

//The number of push buttons
const int NUM_OF_BUTTONS = 8;

// Create Bounce objects for each button

//button debounce time
const int DEBOUNCE_TIME = 25;

Bounce buttons[NUM_OF_BUTTONS] =
{
  Bounce (1, DEBOUNCE_TIME),
  Bounce (2, DEBOUNCE_TIME),
  Bounce (3, DEBOUNCE_TIME),
  Bounce (4, DEBOUNCE_TIME),
  Bounce (5, DEBOUNCE_TIME),
  Bounce (6, DEBOUNCE_TIME),
  Bounce (7, DEBOUNCE_TIME),
  Bounce (8, DEBOUNCE_TIME)
};

//Array to the store the CC message value of each button

const int MIDI_CC_NUMS[NUM_OF_BUTTONS] = {24, 25, 26, 27, 20, 21, 22, 23};

bool running = false;

bool toggle_state[NUM_OF_BUTTONS] = {false, false, false, false, false, false, false, false};

//The setup function. Called once when the Teensy is turned on or restarted

void setup()

{
  // Configure the pins for input mode with pullup resistors.
  // The buttons/switch connect from each pin to ground. Button shorts to ground.
 
  for (int i = 0; i < NUM_OF_BUTTONS; i++)
  {
    pinMode (i, INPUT_PULLUP);
  }

}

//The loop function. Called over-and-over once the setup function has been called.

void loop()

{
  // Update all the buttons/switch.
  for (int i = 0; i < NUM_OF_BUTTONS+1; i++)
  {
    buttons[i].update();
  }
  // Check the status of each push button
  {
    for (int i = 0; i < NUM_OF_BUTTONS; i++)
  {
    if (buttons[i].fallingEdge())
     { 
       toggle_state[i] =  !toggle_state[i];
     }
     // Debug code to see button states
      { Serial.println (MIDI_CC_NUMS[i]);
        Serial.println ("Toggle State :");
        Serial.println (toggle_state[i]);
      }
    {
      if (toggle_state[i])
        { usbMIDI.sendControlChange (MIDI_CC_NUMS[i], 0, 1);
       
        // Debug code to see button CC# passed
       
          Serial.println ("CC # sent change state:");
          Serial.println (MIDI_CC_NUMS[i]);
          Serial.println (0);
         }
      else
         { usbMIDI.sendControlChange (MIDI_CC_NUMS[i], 127, 1);
       
        // Debug code to see button CC# passed
           
           Serial.println ("CC # sent change state:");
           Serial.println (MIDI_CC_NUMS[i]);
           Serial.println (127);
          }
     }   
    }
   }

  // MIDI Controllers should discard incoming MIDI messages.
  while (usbMIDI.read())
  {
    // ignoring incoming messages, so don't do anything here.
  }
}
//Modify message

At this point, the buttons are sending the #'s, so i wonder if the MIDI syntax or the CC# is incorrect?

EDITED - ok, so I hooked the Arduino up to my mac and then launched the MIDI setup app.  It was recieving MIDI messages, but its not now and hasn't been since I went from "latching infinite on". 

I don't quite understand MIDI well enough to know if I shouldn't be using some other send message?
« Last Edit: March 05, 2023, 01:33:50 PM by Peetem »

Randall (Admin)

  • Administrator
  • Full Member
  • *****
  • Posts: 221
    • View Profile
Re: External Midi Controller
« Reply #19 on: March 05, 2023, 12:51:18 PM »
Please use the "insert code" mode (# button).  I can't just assume there are array indexes in there.

Peetem

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: External Midi Controller
« Reply #20 on: March 05, 2023, 01:34:26 PM »
Please use the "insert code" mode (# button).  I can't just assume there are array indexes in there.

Done - didn't know how to do that.  Thanks - learned something else!

Randall (Admin)

  • Administrator
  • Full Member
  • *****
  • Posts: 221
    • View Profile
Re: External Midi Controller
« Reply #21 on: March 05, 2023, 06:54:24 PM »
You have way too many braces in there.  You close your falling edge 'if' statement right after you invert the toggle, so the rest of the code within the loop gets executed every iteration regardless if an edge was caught.

Fishing lesson over.  Try this:

Code: [Select]
void loop()
{
  // Update all the buttons/switch.
  for (int i = 0; i < NUM_OF_BUTTONS+1; i++)
  {
    buttons[i].update();
  }
  // Check the status of each push button
  for (int i = 0; i < NUM_OF_BUTTONS; i++)
  {
    if (buttons[i].fallingEdge())
    {
      toggle_state[i] = !toggle_state[i];
      // Debug code to see button states
      Serial.println (MIDI_CC_NUMS[i]);
      Serial.println ("Toggle State :");
      Serial.println (toggle_state[i]);

      if (toggle_state[i])
      {
        usbMIDI.sendControlChange (MIDI_CC_NUMS[i], 0, 1);
        // Debug code to see button CC# passed
        Serial.println ("CC # sent change state:");
        Serial.println (MIDI_CC_NUMS[i]);
        Serial.println (0);
      }
      else
      {
        usbMIDI.sendControlChange (MIDI_CC_NUMS[i], 127, 1);
        // Debug code to see button CC# passed
        Serial.println ("CC # sent change state:");
        Serial.println (MIDI_CC_NUMS[i]);
        Serial.println (127);
      }
    }
  }
}

Randall (Admin)

  • Administrator
  • Full Member
  • *****
  • Posts: 221
    • View Profile
Re: External Midi Controller
« Reply #22 on: March 05, 2023, 07:01:26 PM »
This:

Code: [Select]
  for (int i = 0; i < NUM_OF_BUTTONS+1; i++)
Should probably be this:

Code: [Select]
  for (int i = 0; i < NUM_OF_BUTTONS; i++)

Peetem

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: External Midi Controller
« Reply #23 on: March 06, 2023, 06:32:18 AM »
You have way too many braces in there.  You close your falling edge 'if' statement right after you invert the toggle, so the rest of the code within the loop gets executed every iteration regardless if an edge was caught.

Thanks!  This ^^^^^^ is why I am terrible with coding.  ;D

I get I was having a continuous loop of off/on, but dang, too many brackets!  That's stupid of me!

In college I had a class where I had to develop a database program using FORTRAN.  We used 8088 machines with no hard drives; dual 5 /14" floppies.  You would make one tiny change, and then wait 45 minutes for it to compile.  Then, you'd execute the code and the bug would still be there.  So I took the 40+ pages of green-impact-printer-large-sheet-printed code to my professor.

He looked at it and within 30 seconds said, with a sly grin, "Mr. Pat, if helps if a Do Loop starts with a DO rather than D-zero (D0)." 

If you don't know, in FORTRAN, depending on the column where you started a line of code, the complier read it as either a line # (e.g., a comment) or something executable.). 

Hours wasted on one little coding error.....and it worked perfectly after fixing that one tiny mistake.
« Last Edit: March 06, 2023, 06:34:34 AM by Peetem »

Peetem

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: External Midi Controller
« Reply #24 on: March 06, 2023, 02:48:40 PM »
And of course your code works perfectly!

One small tweak - It sends a MIDI signal to pedals just fine.  However, I was also using my controller to start a sequencer (simply on/off command).  It "sees" the controller and assigns it to the on/off switch, but alas it doesn't turn that particular device on/off.  Likely some specific value it wants to see from the MIDI command it receives, but that's a minor fix to figure that out.

Thanks again!  Learned a great deal.

Peetem

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: External Midi Controller
« Reply #25 on: March 06, 2023, 07:08:21 PM »
I posted about some apparent bugs in my other thread, but also thought I should post them here they may be related....

For some reason, I can run the hardware check and Pistomp "sees" the three buttons on the Pistomp, Tweak knobs, and Expression pedal.

However, the apps don't respond the pushing anything - including the sequencer I got working earlier today.  All the knobs, switches, expression pedal and etc. are assigned to a pedal, but pushing them causes no action.

When I try to assign one of the knobs, etc. on a new board with nothing assigned, they aren't detected.

Any suggestions?

Thanks!

Peetem

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: External Midi Controller
« Reply #26 on: March 06, 2023, 07:23:12 PM »
Output:

───Foot switches────────────────────────────────────────────│
│ON  OFF OFF                                                                   │
│───AnN DEFFG:root:Switch 22 short press CC event [176, 78, 115]123]───────────│
│---- ---- ----                                                                   │
│0908DEB43D1010DEBUG:root:Switch 27 short press CC event [176, 78, 125]────────│
│0 --                                                                              │
│                                                                                    │
└───────────────────────────────────────────────────┘
┌───────────────────────────────────────────────────┐
│  01:17:47|root        |INFO  |Colors:True                                          │
│  01:19:14|root        |DEBUG |AnalogControl Sending CC event [176, 78, 125]  │
│  01:19:22|root        |DEBUG |Switch 27 short press                          │
│  01:19:22|root        |DEBUG |Switch 23 short press                          │
│  01:19:22|root        |DEBUG |Sending CC event: 62 23                     │
│  01:19:23|root        |DEBUG |Switch 22 short press                          │
│  01:19:23|root        |DEBUG |Sending CC event: 63 22                     │
DEBUG:root:Sending CC event: 63 22C event [176, 78, 127]

───Foot switches───────────────────────────────────────────│
│ON  OFF OFF                                                                   │
│───AnN DEFFDEBUG:root:AnalogControl Sending CC event [176, 70, 111]─│
│---- ---- ----                                                                │
│0850DEB60:root:AnalogControl Sending CC event [176, 71, 94]]103], 125]──│
│0 --                                                                                                          │
│                                                                                                                  │
└───────────────────────────────── ─────────── ────────────┘
┌─────────────────────────────────────────────────────────┐
│  01:17:47|root        |INFO  |Colors:True                                                       │
│  01:19:45|root        |DEBUG |AnalogControl Sending CC event [176, 71, 87]   │
│  01:19:45|root        |DEBUG |AnalogControl Sending CC event [176, 71, 90]   │
│  01:19:45|root        |DEBUG |AnalogControl Sending CC event [176, 71, 92]   │
│  01:19:46|root        |DEBUG |AnalogControl Sending CC event [176, 70, 103   │
│  01:19:47|root        |DEBUG |AnalogControl Sending CC event [176, 70, 106]  │
│  01:20:01|root        |DEBUG |AnalogControl Sending CC event [176, 71, 94


I added this to show the expression pedal works (tried MIDI 78 rather than 77 just to check:

──────────────────────────────────────────────────────────────────────────────┐
│  01:17:47|root        |INFO  |Colors:True                                    │
│  01:24:04|root        |DEBUG |AnalogControl Sending CC event [176, 78, 106]  │
│  01:24:04|root        |DEBUG |AnalogControl Sending CC event [176, 78, 103]  │
│  01:24:04|root        |DEBUG |AnalogControl Sending CC event [176, 78, 105]  │
│  01:24:04|root        |DEBUG |AnalogControl Sending CC event [176, 78, 110]  │
│  01:24:04|root        |DEBUG |AnalogControl Sending CC event [176, 78, 118]  │
│  01:24:04|root        |DEBUG |AnalogControl Sending CC event [176, 78, 127]  │
DEBUG:root:Sending CC event: 63 22C event [176, 78, 127]
« Last Edit: March 06, 2023, 07:25:05 PM by Peetem »

Randall (Admin)

  • Administrator
  • Full Member
  • *****
  • Posts: 221
    • View Profile
Re: External Midi Controller
« Reply #27 on: March 06, 2023, 07:24:51 PM »
Ha, that FORTRAN bug sounds like a D00zy.

It appears that the most recent version of MOD made the default MIDI mode Aggregated instead of Segregated.  Aggregated will cause MIDI events to not be seen.  I haven't figured out why.

Change to Segregated and your problem might be resolved.  It's a per pedalboard setting, so remember to do it for each and save the pedalboard.  How to do that is here:
https://treefallsound.com/wiki/doku.php?id=tutorials_and_videos#controlling_pi-stomp

Peetem

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: External Midi Controller
« Reply #28 on: March 06, 2023, 07:29:41 PM »
Ha, that FORTRAN bug sounds like a D00zy.

It appears that the most recent version of MOD made the default MIDI mode Aggregated instead of Segregated.  Aggregated will cause MIDI events to not be seen.  I haven't figured out why.

Change to Segregated and your problem might be resolved.  It's a per pedalboard setting, so remember to do it for each and save the pedalboard.  How to do that is here:
https://treefallsound.com/wiki/doku.php?id=tutorials_and_videos#controlling_pi-stomp

Unfortunately they were Separated already. 

EDITED - some were and some were not.  I'll go through them all and see what happens.
« Last Edit: March 06, 2023, 07:31:54 PM by Peetem »

Peetem

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: External Midi Controller
« Reply #29 on: March 07, 2023, 04:12:20 PM »
Well, maybe it just needed a rest.   :)

Everything seems to be working now.

Only one weird thing - I had to use TRS input and output (in channel only has one signal) to get both channel outputs to work as stereo.  Small thing and not a big deal.  Probably something I'm doing wrong anyway.