/*
  Arduino | coding-help
  Arduino Leonardo Bass Pedal Sustain note with a latching footswitch
  Ea-Nasir — 6/19/24 at 6:05 PM
  I am working on a bass pedal keyboard project,
  and I'm looking for some coding help with adding
  a Note Hold or Latching feature to a sketch.
*/

const int BTN_PIN = 3;
const int LED_PIN = 2;

bool oldLatchState = false;
int oldBtnState = HIGH;

bool checkButton()  {
  static bool mode = false;

  int btnState = digitalRead(BTN_PIN);
  if (btnState != oldBtnState)  {
    oldBtnState = btnState;
    if (btnState == LOW) {
      mode = !mode;
    }
    delay(20);  // debounce
  }
  return mode;
}

void setup() {
  Serial.begin(9600);
  pinMode(BTN_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.println("Latch is OFF");
}

void loop() {
  bool latchState = checkButton();
  if (latchState != oldLatchState)  {
    oldLatchState = latchState;
    digitalWrite(LED_PIN, latchState);
    Serial.print("Latch is ");
    Serial.println(latchState ? "ON" : "OFF");
  }
}
$abcdeabcde151015202530fghijfghij