Button toggling two values

Hello, I have a piece of code that uses a button to toggle an LED on or off (press release on, press release off). This works perfectly fine but I wish to add a value to be toggled with this code, 127 when toggled on and 0 when toggled off. I want to be able to read the toggled value through serial.
Here is my attempt, but what is happening is the toggled value “sustain” is appearing only when the button is pressed, not released. Can anyone help me understand how to make this work correctly? Thanks in advance…

int sustain = 0;

const int buttonPin = 2;
const int ledPin = 3;

int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);
}

void loop() {

  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      sustain = 0;
      if (buttonState == HIGH) {
        ledState = !ledState;
        sustain = 127;
      }
    }
  }

  digitalWrite(ledPin, ledState);
  lastButtonState = reading;
  Serial.println(sustain);
}

Get rid of the "sustain = 0;" and change "sustain = 127;" to "sustain = 127 * ledState;".

Pieter

Thank you sir :slight_smile: