Adding double click and long press to Control surface lib midi controller with OneButton. how? midi

Hello

I've built a midi controller using the Control surface library and i want to add a different press for double click and long press. I'm trying to use OneButton library for this but i just dont understand how im going to do it. its been so long since i programmed anything that i have completely forgotten everything.

Control surface instantiates everything before void setup and also the pin. It then does a single thing in the loop. I've tried just putting the midi object inside the buttonclick under the loop but it dosent work. Im an idiot so detailed examples would be amazing!

/*
 This is a sample sketch to show how to use the OneButtonLibrary
 to detect click events on 2 buttons in parallel. 
 The library internals are explained at
 http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
 
 Setup a test circuit:
 * Connect a pushbutton to pin A1 (ButtonPin) and ground.
 * Connect a pushbutton to pin A2 (ButtonPin) and ground.
 * The Serial interface is used for output the detected button events.
 
 The Sketch shows how to setup the library and bind 2 buttons to their functions.
 In the loop function the button1.tick and button2.tick functions have to be called as often as you like.
 */

// 01.03.2014 created by Matthias Hertel
// ... and working.

/* Sample output:

Starting TwoButtons...
Button 1 click.
Button 2 click.
Button 1 doubleclick.
Button 2 doubleclick.
Button 1 longPress start
Button 1 longPress...
Button 1 longPress...
Button 1 longPress...
Button 1 longPress stop
Button 2 longPress start
Button 2 longPress...
Button 2 longPress...
Button 2 longPress stop

*/
#include <Control_Surface.h> // Include the Control Surface library
#include "OneButton.h"

// Instantiate a MIDI over USB interface.
USBMIDI_Interface midi;

OneButton button1(2, true);  
OneButton button2(3, true);
OneButton button3(4, true);
OneButton button4(5, true);
OneButton button5(6, true); 
OneButton button6(7, true);
OneButton button7(8, true); 
OneButton button8(9, true);
OneButton button9(10, true);  
OneButton button10(16, true);

NoteButton button_1 {
  2,                       // Push button on pin 5
  {MIDI_Notes::C(1), CHANNEL_1}, // Note C4 on MIDI channel 1
};

NoteButton button_2 {
  3,                       // Push button on pin 5
  {MIDI_Notes::C(2), CHANNEL_1}, // Note C4 on MIDI channel 1
};

// setup code here, to run once:
void setup() {
  // Setup the Serial port. see http://arduino.cc/en/Serial/IfSerial


  // link the button 1 functions.
  button1.attachClick(click1);
  button1.attachDoubleClick(doubleclick1);
  button1.attachLongPressStart(longPressStart1);
  button1.attachLongPressStop(longPressStop1);
  button1.attachDuringLongPress(longPress1);

  // link the button 2 functions.
  button2.attachClick(click2);
  button2.attachDoubleClick(doubleclick2);
  button2.attachLongPressStart(longPressStart2);
  button2.attachLongPressStop(longPressStop2);
  button2.attachDuringLongPress(longPress2);

   Control_Surface.begin(); // Initialize Control Surface

} // setup


// main code here, to run repeatedly: 
void loop() {
  // keep watching the push buttons:
  button1.tick();
  button2.tick();
  
  Control_Surface.loop(); // Update the Control Surface
  
  // You can implement other code in here or just wait a while 
  delay(10);
} // loop


// ----- button 1 callback functions

// This function will be called when the button1 was pressed 1 time (and no 2. button press followed).
void click1() {
  Serial.println("Button 1 click.");
  NoteButton button_1 {
  2,                       // Push button on pin 5
  {MIDI_Notes::C(1), CHANNEL_1}, // Note C4 on MIDI channel 1
};

  } // click1


// This function will be called when the button1 was pressed 2 times in a short timeframe.
void doubleclick1() {
  Serial.println("Button 1 doubleclick.");
  NoteButton button_2 {
  3,                       // Push button on pin 5
  {MIDI_Notes::C(2), CHANNEL_1}, // Note C4 on MIDI channel 1
};
} // doubleclick1


// This function will be called once, when the button1 is pressed for a long time.
void longPressStart1() {
  Serial.println("Button 1 longPress start");
} // longPressStart1


// This function will be called often, while the button1 is pressed for a long time.
void longPress1() {
  Serial.println("Button 1 longPress...");
} // longPress1


// This function will be called once, when the button1 is released after beeing pressed for a long time.
void longPressStop1() {
  Serial.println("Button 1 longPress stop");
} // longPressStop1


// ... and the same for button 2:

void click2() {
  Serial.println("Button 2 click.");
} // click2


void doubleclick2() {
  Serial.println("Button 2 doubleclick.");
} // doubleclick2


void longPressStart2() {
  Serial.println("Button 2 longPress start");
} // longPressStart2


void longPress2() {
  Serial.println("Button 2 longPress...");
} // longPress2

void longPressStop2() {
  Serial.println("Button 2 longPress stop");
} // longPressStop2


// End

I've never used OneButton, and I'm not a fan of its callback-based design. However, you can just send MIDI messages in the callbacks if you want. See Control Surface: MIDI Tutorial and the examples referenced therein for details on how to do that.

Why burn CPU cycles like this?

This has no effect: it just creates a local NoteButton object that immediately gets destroyed again when you leave the function.
In the callback function, you don't want to create a NoteButton, you want to send a MIDI message, so use something like Control_Surface.sendNoteOn({MIDI_Notes::C(2), CHANNEL_1}, 127); instead, as explained in the MIDI tutorial.

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