 
     case LatchingOff:
     case LatchingOn:
-      // Fetch current latch state based on last value sent
-      if (LastValue[i] < 64) {
-        state = 0;
-      }
-      else {
-        state = 1023;
-      }
-
       // Toggle state on button press
       if (PinState[pinNum] < 512 && NewState[pinNum] >= 512) {
-        state = 1023 - state;
+        LatchState[i] ^= 1;
         changed = 1;
       }
       else {
         changed = 0;
       }
+      state = (LatchState[i] == 0) ? 0 : 1023;
       break;
 
     default:

#ifdef LOG_EVENTS
      Serial.println(String("Off ") + noteEvt->Channel + "/" + noteEvt->Note + ": " + noteEvt->OffVelocity);
#endif
      usbMIDI.sendNoteOn(noteEvt->Note, noteEvt->OffVelocity, noteEvt->Channel);
      MIDI.sendNoteOn(noteEvt->Note, noteEvt->OffVelocity, noteEvt->Channel);
    }
    value = state / 8; // Scale to 0-127 range
    break;

    }
  }

  // Initialize EventList, NumEvents, LastValue, LatchState
  NumEvents = count;
  EventList = (EventMap *)realloc (EventList, NumEvents * sizeof (*EventList));
  memcpy(EventList, &DefaultEventList[i], NumEvents * sizeof (*EventList));

  LastValue = (uint8_t *)realloc (LastValue, NumEvents * sizeof (*LastValue));
  memset(LastValue, 0, NumEvents * sizeof (*LastValue));

  LatchState = (uint8_t *)realloc (LatchState, NumEvents * sizeof (*LatchState));
  memset(LatchState, 0, NumEvents * sizeof (*LatchState));

  // Send any initialization events, and initialize LastValue[]

