dual function button ON/OFF/DIMMER

Hi,

I'm currently trying to get my head around how to code a single momentary button to turn an led on and off when pressed and released but when pressed and held dim the led. To make it more clear it would work like so.

LED off
Press button - LED on
Press button - LED off
Hold button - LED gets brighter until desired brightness
Press button - LED off
Press button - LED on
Hold button - LED gets dimmer until desired brightness

i am not great at coding and find i learn best by picking though similar sketches and adapting them to my own needs. So far i've been able to use one button to turn on an led when pressed and increase the brightness of a second led when held.

int LED1 = 10;
int LED2 = 9;
int button = 8;

int brightness = 0;    // how bright the LED is
int fadeAmount = 25;    // how many points to fade the LED by

boolean LED1State = false;
boolean LED2State = false;

long buttonTimer = 0;
long longPressTime = 500;

boolean buttonActive = false;
boolean longPressActive = false;

void setup() {

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(button, INPUT);
}

void loop() {
analogWrite(LED1, brightness);
  if(digitalRead(button) == HIGH) {

    if (buttonActive == false) {

      buttonActive = true;
      buttonTimer = millis();

    }

    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {


      longPressActive = true;
      // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
    delay (10);
  } 
  

    
delay (50);
  }} else {

    if (buttonActive == true) {

      if (longPressActive == true) {

        longPressActive = false;

      } else {

        LED2State = !LED2State;
        
        digitalWrite(LED2, LED2State);
delay (50);
      }

      buttonActive = false;

    }

  }

}

The issues I'm having are that i cant get it to work with just one led, the dim function only does a single increment (in my code its 25 ) per press instead of a smooth increase or decrease.

Once worked out i'll be add this to my larger project that im working on which involve using blynk to also control the led.

Thanks in advance
Ben

long buttonTimer = 0;
long longPressTime = 500;

Are these signed so that you can support negative times?

  pinMode(button, INPUT);

How IS the switch wired? Why are you not using the far simpler wiring that INPUT_PULLUP enables?

      buttonTimer = millis();

The millis() function does NOT return a signed value, so you should NOT be storing the returned value in a signed variable.

  }} else {

Sloppy coding. EVERY { belongs on a line BY ITSELF. Every } belongs on a line BY ITSELF.

Use Tools + Auto Format before you post code again.

What do your serial print statements tell you is happening? Why not?

Is that correct?

That part is.

I had the wiring already set up on my bread board with a resistor so I didn't bother to change it.

Or explain it.

Is there anything else you would suggest?

I'm going to refrain.