Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: MIDI Message Toggle in Code

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Location
    Johnson City TN
    Posts
    10

    MIDI Message Toggle in Code

    Hi,

    I'm trying to create a momentary button that will alternate/toggle between two fixed MIDI messages in code.
    I'm using the Bounce Library and would like to use either Falling or Rising edge to trigger this alternating behavior.
    The code i've got is just sending a continuous stream of alternating note numbers. My programming knowledge is
    pretty limited but trying to learn along the way. Any suggestion or guidance is greatly appreciated.


    Code:
    // Button Toggle In Code
    
    #include <Bounce.h>
    
    const int channel = 1;
    int StateOne = HIGH;
    int StateTwo = HIGH;
    int toggleState = HIGH;
    
    
    Bounce button0 = Bounce(0, 10);
    
    
    void setup() {
    
      pinMode(0, INPUT_PULLUP);
    
    
    }
    
    void loop() {
    
      button0.update();
    
    
      if(button0.fallingEdge() != StateTwo && StateOne == 1 && toggleState == 0) {
        usbMIDI.sendNoteOn(60, 99, channel);
        toggleState = 1;
    
      }
    
       else if(button0.fallingEdge() != StateTwo && StateOne == 1 && toggleState == 1); {
        usbMIDI.sendNoteOn(61, 99, channel);
        toggleState = 0;
    
      }
    
      
    
      while (usbMIDI.read()) {
    
      }
    }

  2. #2
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Location
    Florida
    Posts
    425
    Does it only send a stream when pressing the button or constantly?

  3. #3
    Junior Member
    Join Date
    Nov 2019
    Location
    Johnson City TN
    Posts
    10
    Hi Vj,

    Thanks so much for chiming in here. It appears to start streaming as soon as the sketch starts. I'm watching the output with MIDI monitor.

  4. #4
    Senior Member
    Join Date
    Nov 2017
    Location
    Belgium
    Posts
    165
    What about this?
    Code:
    // Button Toggle In Code
    
    #include <Bounce.h>
    
    const int channel = 1;
    
    Bounce button0 = Bounce(0, 10);
    
    void setup() {
    
      pinMode(0, INPUT_PULLUP);
    
    }
    
    void loop() {
    
      if(button0.update()){
        if(button0.fallingEdge()){
          usbMIDI.sendNoteOn(60, 99, channel); //midi on fallingEdge
        }else{
          usbMIDI.sendNoteOn(61, 99, channel); //midi on risingEdge
        }
      }
    
      while (usbMIDI.read()) {}
    }

  5. #5
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Location
    Florida
    Posts
    425
    Delete this part from the if statements and it should work:
    != StateTwo
    .fallingEdge() returns true if it’s a falling edge so you shouldn’t test if it’s not high.

  6. #6
    Junior Member
    Join Date
    Nov 2019
    Location
    Johnson City TN
    Posts
    10
    Hi Neuro,


    thanks for the reply and code .... nice little snippet for sure !! .. but, I'm hoping to toggle/alternate using only fallingEdge or risingEdge .. if that makes sense?

  7. #7
    Junior Member
    Join Date
    Nov 2019
    Location
    Johnson City TN
    Posts
    10
    Hi VJ,

    trying this now ... Still continuous stream but a single note number ...

  8. #8
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,176
    Get rid of StateOne, StateTwo and toggleState.

    Code:
      if(button0.fallingEdge()) {
        usbMIDI.sendNoteOn(60, 99, channel);
      }
    
       if(button0.risingEdge()) {
        usbMIDI.sendNoteOn(61, 99, channel);
      }
    Note also that I got rid of a bug in your code:
    Code:
       else if(button0.fallingEdge() != StateTwo && StateOne == 1 && toggleState == 1); {
    The semicolon shouldn't be there.

    Pete

  9. #9
    Junior Member
    Join Date
    Nov 2019
    Location
    Johnson City TN
    Posts
    10
    Hey Pete !


    Thanks for the corrections .... I'm looking to get this behavior at each fallingEdge if that makes sense ? ... so fallingEdge, Note 60 Next fallingEdge Note 61.

  10. #10
    Senior Member
    Join Date
    Nov 2017
    Location
    Belgium
    Posts
    165
    Quote Originally Posted by digitalelements View Post
    Hi Neuro,


    thanks for the reply and code .... nice little snippet for sure !! .. but, I'm hoping to toggle/alternate using only fallingEdge or risingEdge .. if that makes sense?
    oops, misunderstood the question.

    correction:
    Code:
    // Button Toggle In Code
    
    #include <Bounce.h>
    
    const int channel = 1;
    bool toggleState = false;
    
    Bounce button0 = Bounce(0, 10);
    
    void setup() {
    
      pinMode(0, INPUT_PULLUP);
    
    }
    
    void loop() {
    
      if(button0.update() && button0.fallingEdge()){
        if(!toggleState){
          usbMIDI.sendNoteOn(60, 99, channel);
          toggleState = true;
        }else{
          usbMIDI.sendNoteOn(61, 99, channel);
          toggleState = false;
        }
      }
    
      while (usbMIDI.read()) {}
    }

  11. #11
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Location
    Florida
    Posts
    425
    This should do the trick:
    Code:
    // Button Toggle In Code
    
    #include <Bounce.h>
    
    const int channel = 1;
    boolean toggleState = HIGH;
    
    
    Bounce button0 = Bounce(0, 10);
    
    
    void setup() {
      pinMode(0, INPUT_PULLUP);
    }
    
    void loop() {
      button0.update();
    
      if(button0.fallingEdge()) {
        if(toggleState) usbMIDI.sendNoteOn(60, 99, channel);
        else usbMIDI.sendNoteOn(61, 99, channel);
        toggleState = !toggleState;
      }  
    
      while (usbMIDI.read()) {
    
      }
    }
    I haven't tested it, but it compiles and the logic looks right to me.

  12. #12
    Junior Member
    Join Date
    Nov 2019
    Location
    Johnson City TN
    Posts
    10
    Ahhhhhh ... thank you very much ... Both solutions ( Neuro & VJ ) are working as I had hoped.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •