MIDI Message Toggle in Code

Status
Not open for further replies.

digitalelements

Active member
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()) {

  }
}
 
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.
 
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()) {}
}
 
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.
 
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?
 
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
 
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.
 
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()) {}
}
 
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.
 
I'm here because I had the same issue and I tested this code and it is running well, thank you so much vjmuzik



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.
 
Status
Not open for further replies.
Back
Top