count midi messages, even / odd - do something?

hi, have a project i’m working on that sends really fast (or really slow, depending on potentiometer position) midiProg change messages to simulate a tremolo effect by muting/unmuting the device’s audio output. the problem i’m having is that i have a 50/50 shot of leaving the audio muted when i release my button depending on what the current state of the device is.
i thought to solve this by counting the number of times my code has sent the message and if it’s ODD send one more midiProg and else it’s EVEN do nothing… that way on release of the spst momentary switch it always comes out to clean audio output. aka: 1=mute, 2=unmute, 3=mute, 4=unmute… so on, so forth. i tried doing this using “buttonPushCounter++;” but haven’t been able to get it to work… see the bottom-most section of code below… i just included all of it in case the error was elsewhere in the code.

my code is below (scroll to the bottom for the section in question), any help appreciated.

#include <Bounce.h>
//send a two byte MIDI message
void midiProg(char status, int data ) {
  Serial.print(status, BYTE);
  Serial.print(data, BYTE);
const int buttonPin11 = 11;
int buttonState11 = HIGH;
int lastButtonState11 = HIGH; 
Bounce buttonBounce11 = Bounce( buttonPin11, 10 );
#define holdTime11 100
const int potPin = 5;
    int val = 0;
long btnDnTime11;
    long btnUpTime11;
boolean ignoreUp11 = true;
int buttonPushCounter11 = 0;

void setup() {
//set MIDI baud rate

pinMode(buttonPin11, INPUT);
void loop() {
val = analogRead(potPin); 
  buttonBounce11.update ( );
  buttonState11 =;
  // compare the buttonState to its previous state
  if (buttonState11 != lastButtonState11) {
    // if the state has changed
    if (buttonState11 == LOW) {
      if (ignoreUp11 == true) {
      // if the current state is HIGH then the button
      // went from off to on:  
    else {
      ignoreUp11 = false;
      lastButtonState11 = buttonState11;
    if (buttonState11 == HIGH && (millis() - btnDnTime11) > long(holdTime11))
      ignoreUp11 = false;
      btnDnTime11 = millis();
      if ( (buttonPushCounter11 % 2) == 0) {
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState11 = buttonState11;

"var & 1" is a much more intuitive way of testing odd/even

how would i implement that? could you give me example code or point me to a tutorial?


also, i should note, i need to be counting the number of times the midiProg has been sent during the button hold down state... not the number of actual button pushes if that makes a difference


i tried doing this using "buttonPushCounter++;" but haven't been able to get it to work... see the bottom-most section of code below.

It doesn't matter if you are counting switch presses, lima beans, or messages sent. You need to know if there was an odd or even number of them.

So, pick something to count. When you are done counting, print the value, decide whether it is odd or even, and print that decision.

If you see 14 odd 27 even

you know that your odd/even decision process is wrong.

If you see numbers that don't make sense in terms of the number of switch presses, then the counting is wrong.

If the wrong action occurs when the even/odd decision is made, then there is a logic problem.

But, "I couldn't get it to work" doesn't tell us diddly.

Present some facts, and we can help.

right. what i'm asking is how to count the number of time the midiProg has been sent once the button has been held down. i don't know how to do that so i don't know how to give you any more information than that...

You've got the code right there in front of you. You know which statements send the midi messages. It's simple to add to a counter each time a mute message is sent, and to subtract from that counter when an un-mute message is sent. At any given time, you can know whether the receiver is muted or not.