Switch to send midi CC

Hi everybody

I try to send midi CC with a switch : when i touch it send value 127, when i touch again it send 0, when i touch again it send 127...

#include <MIDI.h>

int ctrlchg1;
int lastctrlchg1;


MIDI_CREATE_DEFAULT_INSTANCE();


void setup()
{
   Serial.begin(31250);
    MIDI.begin (MIDI_CHANNEL_OMNI);  // Launch MIDI and listen to channel omni
      pinMode(2, INPUT_PULLUP);

}

void loop()
{
    //read the pushbutton value into a variable
  int switch1 = digitalRead(2);
int lastctrlchg1 = ctrlchg1;


  if (switch1 == LOW && lastctrlchg1 == 0)
  {
    int ctrlchg1 = 127;
      MIDI.sendControlChange(12, ctrlchg1, 1);
      int lastctrlchg1 = 127;
      delay(1000);
      }

  if (switch1 == LOW && lastctrlchg1 == 127)
  {
    int ctrlchg1 = 0;
      MIDI.sendControlChange(12, ctrlchg1, 1);
       int lastctrlchg = 0;
      delay(1000);
      }
      
int ctrlchg1 = lastctrlchg1;

}

it stay at 127 and don't go to 0 when i press again
i have tried some stuff but...

You have more than one variable named ctrlchg1, each with their own scope

You declare it once at the start of the program which gives it global scope so remove the int from all other occurrences of it to stop a new version of it being declared

The same goes for lastctrlchg1

thanks for reply i have clean some stuff but same result i have this now

#include <MIDI.h>

int ctrlchg1;
int lastctrlchg1;


MIDI_CREATE_DEFAULT_INSTANCE();


void setup()
{
   Serial.begin(31250);
    MIDI.begin (MIDI_CHANNEL_OMNI);  // Launch MIDI and listen to channel omni
      pinMode(2, INPUT_PULLUP);

}

void loop()
{
    //read the pushbutton value into a variable
  int switch1 = digitalRead(2);



  if (switch1 == LOW && lastctrlchg1 == 0)
  {
    int ctrlchg1 = 127;
      MIDI.sendControlChange(12, ctrlchg1, 1);
      int lastctrlchg1 = 127;
      delay(1000);
      }

  if (switch1 == LOW && lastctrlchg1 == 127)
  {
    int ctrlchg1 = 0;
      MIDI.sendControlChange(12, ctrlchg1, 1);
       int lastctrlchg = 0;
      delay(1000);
      }
      


}

same result don' go back to value 0

#include <MIDI.h>

int ctrlchg1;


MIDI_CREATE_DEFAULT_INSTANCE();


void setup()
{
   Serial.begin(31250);
    MIDI.begin (MIDI_CHANNEL_OMNI);  // Launch MIDI and listen to channel omni
      pinMode(2, INPUT_PULLUP);

}

void loop()
{
    //read the pushbutton value into a variable
  int switch1 = digitalRead(2);



  if (switch1 == LOW && ctrlchg1 == 0)
  {

      MIDI.sendControlChange(12, ctrlchg1, 1);
      
      delay(1000);
      }

  if (switch1 == LOW && ctrlchg1 == 127)
  {

      MIDI.sendControlChange(12, ctrlchg1, 1);

      delay(1000);
      }
      


}

after this my DAW don't reconize CC but midiOX show me CC0

I have only looked at your code in reply #4 but I see that you never change the value of ctrlchg1 from zero so how will

  if (switch1 == LOW && ctrlchg1 == 127)

ever be true ?

Hello
I got tired of lecturing about debouncing buttons and finite state machines tonight, so I added the changes to your sketch. The sketch is untested and may need to be adapted to your MIDI environment.

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
void setup()
{
  Serial.begin(31250);
  MIDI.begin (MIDI_CHANNEL_OMNI);  // Launch MIDI and listen to channel omni
  pinMode(2, INPUT_PULLUP);
}
void loop() {
  static bool state = 0;
  static int stage = 0;
  static unsigned long buttonMillis;
  const  unsigned long buttondelay = 50;
  if  (millis() - buttonMillis >= buttondelay) {
    buttonMillis = millis();
    bool pinState = !digitalRead(2);
    if (state != pinState) {
      state = pinState;
      if (state) {
        switch (stage) {
          case 0:
            MIDI.sendControlChange(12, 0, 1);
            break;
          case 1:
            MIDI.sendControlChange(12, 127, 1);
            break;
        }
        stage++;
        stage = stage % 2;
      }
    }
  }
}
1 Like

wow you are a god!!!

it works exactly like i want now i will try to understand your code...

thanks a lot

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.