adding more buttons given all of the variables that one button needs

Hey All,

I am trying to get through my first esp-32 board project incorporating the Apple MIDI library. I am trying to make 4 buttons. Each is independent of the others and, for right now, can follow the state of an LED to send a MIDI message with a value of 0 (off) or value 0f 127 (on). How would I got about creating a second button? I don’t want either button to have priority over another. For example, if two are pressed almost at the same time, I don’t want only one to be sent. Is this possible? This code right now works over WiFi perfectly. So, ideally, I am hoping that adding 4 times the code will work, but I imagine that being inefficient… any ideas?!? Thanks!

Also, my board’s pin layout is included
Also, I am using the Apple MIDI library second to the most recent or second to the ‘alpha.’ This is the only one I can get to work. Or maybe it is as simple as, they dropped examples for my board in what’s included in the library. But, whatever the reason, I am currently using the second most recent library when selecting from the library to add after searching for it.

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>

#include "AppleMidi.h"
char ssid[] = "rob69"; //  your network SSID (name)
char pass[] = "testtest";    // your network password (use for WPA, or use as key for WEP)

// constants won't change
const int button1_Pin = 4;
const int LED1_PIN = 16;

const int DEBOUNCE_DELAY = 50;   // the debounce time; increase if the output flickers

// variables will change:
int lastSteady1State = LOW;       // the previous steady state from the input pin
int lastFlickerable1State = LOW;  // the previous flickerable state from the input pin
int led1State = LOW;     // the current state of LED
int lastButton1State;    // the previous state of button
int currentButton1State; // the current state of button

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long t0 = millis();
bool isConnected = false;

APPLEMIDI_CREATE_INSTANCE(WiFiUDP, AppleMIDI); // see definition in AppleMidi_Defs.h

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void setup() {
  pinMode(button1_Pin, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(LED1_PIN, OUTPUT);          // set arduino pin to output mode

  currentButton1State = digitalRead(button1_Pin);
  Serial.print(F("Getting IP address..."));
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
  Serial.println(F("WiFi connected"));

  Serial.print(F("IP address is "));

  Serial.println(F("OK, now make sure you an rtpMIDI session that is Enabled"));
  Serial.print(F("Add device named Arduino with Host/Port "));
  Serial.println(F("Then press the Connect button"));
  Serial.println(F("Then open a MIDI listener (eg MIDI-OX) and monitor incoming notes"));

  // Create a session and wait for a remote host to connect to us



void loop() {;
   if (currentButton1State != lastFlickerable1State) {
    // reset the debouncing timer
    lastDebounceTime = millis();
    // save the the last flickerable state
    lastFlickerable1State = currentButton1State;
    lastButton1State    = currentButton1State;      // save the last state
    currentButton1State = digitalRead(button1_Pin); // read new state
  if(lastButton1State == HIGH && currentButton1State == LOW) {
    Serial.println("The button is active");

    // toggle state of LED
    led1State = !led1State;

    // control LED arccoding to the toggled state
    digitalWrite(LED1_PIN, led1State);
    if (led1State == HIGH) {
      AppleMIDI.sendControlChange(85, 127, 13);
      } else {
        AppleMIDI.sendControlChange(85, 0, 13);

// ====================================================================================
// Event handlers for incoming MIDI messages
// ====================================================================================

// -----------------------------------------------------------------------------
// rtpMIDI session. Device connected
// -----------------------------------------------------------------------------
void OnAppleMidiConnected(uint32_t ssrc, char* name) {
  isConnected  = true;
  Serial.print(F("Connected to session "));

// -----------------------------------------------------------------------------
// rtpMIDI session. Device disconnected
// -----------------------------------------------------------------------------
void OnAppleMidiDisconnected(uint32_t ssrc) {
  isConnected  = false;

If you only need 4 buttons, create all the variables needed for the 3 additional buttons you will need and add the code for them.


ok. cool. Is it important which pins the 3 other LED and 3 other buttons go to? The tutorial I watched chose pin 4 (D4) and pin 16 (RX2). I see a bunch more D[number] pins (I assume digital), but only one more RX pin... called RX0. There is also a TX2 and TX0. I just don't want to fry anything. ha

It should not matter as long as you use a GPIO pin. GPIO16 is its primary function. RX2 is its secondary function. They probably chose pin 4 and pin 16 because they are adjacent.

ok great. thanks!