ESP32 BLE-MIDI switch controller

Hello everyone
I'm trying to build a simple Bluetooth controller
parts I have are
8 momentary ON/OFF switches
LOLIN32 Lite Board V1.0 con ESP-32 Rev1
the idea is to send ControlChange using Bluetooth
I've done this with an RPI Pico using USB and that was already complex to find information online
for this, I found many examples for noteOn noteOff but is quite confusing for me coming from a different platform
so hopefully someone can help out with the code in:

when a pushbutton is pressed once should send value 127
when pressed again should send value 0

I can add the code I have for the Pico but not sure how this will work with the Arduino interface as it seems that syntax is very different

please let me know if someone has tried this or has some example code, or any help will be highly appreciated

thank you in advance for any reply

Thank you PieterP

I checked those documents but I'm too nooby to know how to include that library in the project and how to code a button to trigger a control change

I've searched in the list of libraries but the Control Surface is not there to be added

I will keep looking

the BLE Midi example that comes with the board works just fine
I can connect to my iPad and the note is playing just fine

now I just need to tell which pin I want to use, and when pushed once to send a certain value and when pushed again should send a different value

every example I find online is not clear or is for a specific board only :expressionless:

thanx anyway

See Control Surface: Getting Started and Control Surface: Installation.

#include <Control_Surface.h>

BluetoothMIDI_Interface midi;

CCButtonLatched button {0, 7}; // {pin №, CC №}

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

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

Uau!! that was a quick reply :slight_smile:
Thank you
I will try this - will install your library and check some of the examples

here the code I am trying to emulate
this was done for the Pico using CircuitPython

import time
import board
import digitalio
import usb_midi
import adafruit_midi  # MIDI protocol encoder/decoder library
from adafruit_midi.control_change import ControlChange
midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0)

button1 = digitalio.DigitalInOut(board.GP13)
button1.switch_to_input(pull=digitalio.Pull.DOWN)
button2 = digitalio.DigitalInOut(board.GP12)
button2.switch_to_input(pull=digitalio.Pull.DOWN)
button3 = digitalio.DigitalInOut(board.GP11)
button3.switch_to_input(pull=digitalio.Pull.DOWN)
button4 = digitalio.DigitalInOut(board.GP10)
button4.switch_to_input(pull=digitalio.Pull.DOWN)
button5 = digitalio.DigitalInOut(board.GP18)
button5.switch_to_input(pull=digitalio.Pull.DOWN)
button6 = digitalio.DigitalInOut(board.GP19)
button6.switch_to_input(pull=digitalio.Pull.DOWN)
button7 = digitalio.DigitalInOut(board.GP20)
button7.switch_to_input(pull=digitalio.Pull.DOWN)
button8 = digitalio.DigitalInOut(board.GP21)
button8.switch_to_input(pull=digitalio.Pull.DOWN)

flag = 1

while True:
    if button1.value and flag != 1:
        midi.send(ControlChange(12, 127))
        print("You sent ON")
        flag = 1
        time.sleep(0.75)
    elif button1.value and flag != 0:
        midi.send(ControlChange(12, 0))
        print("you sent off")
        flag = 0
        time.sleep(0.75)
    if button2.value and flag != 1:
        midi.send(ControlChange(13, 127))
        print("You sent ON")
        flag = 1
        time.sleep(0.75)
    elif button2.value and flag != 0:
        midi.send(ControlChange(13, 0))
        print("you sent off")
        flag = 0
        time.sleep(0.75)
    if button3.value and flag != 1:
        midi.send(ControlChange(14, 127))
        print("You sent ON")
        flag = 1
        time.sleep(0.75)
    elif button3.value and flag != 0:
        midi.send(ControlChange(14, 0))
        print("you sent off")
        flag = 0
        time.sleep(0.75)
    if button4.value and flag != 1:
        midi.send(ControlChange(15, 127))
        print("You sent ON")
        flag = 1
        time.sleep(0.75)
    elif button4.value and flag != 0:
        midi.send(ControlChange(15, 0))
        print("you sent off")
        flag = 0
        time.sleep(0.75)
    if button5.value and flag != 1:
        midi.send(ControlChange(18, 127))
        print("You sent ON")
        flag = 1
        time.sleep(0.75)
    elif button5.value and flag != 0:
        midi.send(ControlChange(18, 0))
        print("you sent off")
        flag = 0
        time.sleep(0.75)
    if button6.value and flag != 1:
        midi.send(ControlChange(19, 127))
        print("You sent ON")
        flag = 1
        time.sleep(0.75)
    elif button6.value and flag != 0:
        midi.send(ControlChange(19, 0))
        print("you sent off")
        flag = 0
        time.sleep(0.75)
    if button7.value and flag != 1:
        midi.send(ControlChange(20, 127))
        print("You sent ON")
        flag = 1
        time.sleep(0.75)
    elif button7.value and flag != 0:
        midi.send(ControlChange(20, 0))
        print("you sent off")
        flag = 0
        time.sleep(0.75)
    if button8.value and flag != 1:
        midi.send(ControlChange(21, 127))
        print("You sent ON")
        flag = 1
        time.sleep(0.75)
    elif button8.value and flag != 0:
        midi.send(ControlChange(21, 0))
        print("you sent off")
        flag = 0
        time.sleep(0.75)

I know, I'm sure there would be a much cleaner way to do it :slight_smile:
but as I mentioned before I'm very new to this and nevertheless it works on the pico via usb
trying to do the same using BLE midi with the esp32

thanx again PieterP

Thank you PieterP
That works perfectly
I didn't think it could be so simple :neutral_face:
Thank you again

Hey @PieterP
thank you again for the code and the library
one more question, only if its easy :smiley:
I am using the code you sent, so the CCButtonLatched and it works beautifully
is there an easy way to add a LED to be powered when the ON signal is sent
as there is no visible way for me to say when the ON or OFF signal is sent
let me know if there is an integration with an LED per button
if there isn't not to worry
thank you again for this great implementation :wink:

You can use the getState() method and turn on/off the LED based on that.

do you have any examples? or will there be examples in the documents provided?
I will check anyway :slight_smile:
thanx again and I really like your approach on the library
it seems well thought :wink:

See this post: CCButtonLatched LED without ShiftRegister · Issue #241 · tttapa/Control-Surface · GitHub

:+1: :+1:
perfect - tank you Pieter :wink:

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