How to use a push button to turn on and off on cortrol Surface library

I'm kinda new to programming arduino, and i'm trying to build a midi footswitch for amplitube or any other so, i wanted to make the button send a cc message with a value and then when i click again, send the same message but with other value. basically, if click it'll turn what i want on and if i click again it'll turn off.
Here is the code

#include <Control_Surface.h>


HairlessMIDI_Interface(midi);



CCButton button {
  2, 
  {MIDI_CC::Effects_1, Channel_1}
};

CCLED led {
  3,
  {MIDI_CC::Effects_1, Channel_1}
};


void setup() {
  Control_Surface.begin();
  Serial.begin(9600);
}

void loop() {
  Control_Surface.loop();
}

Observations: I"m using arduino UNO and, sorry if i messed up some words, i'm stil learning enlgih

which?

On the first click send a message with a value 0 to turn on, and when i click again the value will be 127 to turn it off

#define pin 2
void setup() {
  Serial.begin(115200);
  pinMode(pin, INPUT_PULLUP);
}

void loop() {
  static bool oldState = false;
  bool state = !digitalRead(pin);
  if (oldState != state) {
    if (state)CC();
    while (!digitalRead(pin));
    oldState = state;
    delay(100);
  }
}

void CC() {
  static bool V = true;
  Serial.write(0xB0);// CC 1 channel
  Serial.write(0x00);// first byte, pick one in range 0-127
  Serial.write(V ? 0x7f : 0x00); //value. if each time should be same value than buy me a coffee
  Serial.flush();
  V = !V;
  delay(100);
}
1 Like

thank you so much! It worked really well, but i was wondering how can i use a potentiometer?

more important is how you want it use, what should it do?

This is what the CCButtonLatched class does. In the code from your first post, simply replace CCButton by CCButtonLatched.

1 Like

I already managed to solve it, but thanks for everything anyways

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