Toggle MIDI CC on/off with momentary switch

kiran1365

New member
Hi!

I am very new to Teensy

I am trying to make a MIDI controller for my HX Stomp that can send MIDI CC messages to bypass blocks on the HX Stomp.

I have been successful in being able to turn a block on with a CC message however thats as far as my knowledge can get me in Teensy coding. I need the button to be pressed again to turn off the block.
so is there a way to toggle controlValues between 0 and 127 with the same button? or some other way to toggle the same controlNumber on and off?
I am using Teensy 3.6

Code:
#include <MIDI.h>
#include <Bounce.h>  
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);

Bounce button1 = Bounce(2, 5);  // 5 = 5 ms debounce time

void setup() {
 
  pinMode(2, INPUT_PULLUP);
 
  MIDI.begin(MIDI_CHANNEL_OMNI);
  Serial1.setRX(1);
  Serial.begin(31250);
}

void loop() {
  button1.update();

 
  // Note On messages when each button is pressed
  if (button1.fallingEdge()) {
    MIDI.sendControlChange(90, 127, 1); 

  }
}
 
hey!

You should save a variable, that saves the current status.

Very simple example:

Code:
uint8_t cc90_value = 0;

void loop() {
  button1.update();
  if( button1.fallingEdge()) {
    cc90_value = 127 - cc90_value;
    MIDI.sendControlChange(90, cc90_value, 1);
  }
}
that toggles the value between 0 and 127.

(value = 0 -> 127 - 0 = 127)
(value = 127 -> 127 - 127 = 0)

you could do it with an if statement too:

Code:
if( cc90_value == 127) cc90_value = 0;
else cc90_value = 127;

That way you can toggle by pressing the button (like you are used to from stomp boxes). And for more buttons, you just add more variables.

Hope that helps.
 
Back
Top