How to change the delay time value on a button push?

Hi, so I am trying to change the delay time value of an LED from the push of a button but I got a problem.
The time does not change.
So, I would appreciate any help because I’m still a beginner at arduino.

const int ledPin = 4;
const int buttonPin = 7;
const int buttonPin1 = 8;

int ledState = LOW;
int buttonState = 0;
int buttonState1 = 0;

unsigned long previousMillis = 0;

long interval = 500;
long intervalEditor = 50;

void setup() {

  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);

}

void loop() {
  unsigned long currentMillis = millis();

  
  if(currentMillis - previousMillis >= interval){
    previousMillis = currentMillis;

  buttonState = digitalRead(buttonPin);
  buttonState1 = digitalRead(buttonPin1);

  

    if(ledState == LOW){
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
  }
  digitalWrite(ledPin, ledState);

 if(buttonState == HIGH){
    delay(300);
    interval + intervalEditor;
    delay(300);
    Serial.println(interval);
    Serial.println("increased");
  }else if(buttonState1 == HIGH){
    delay(300);
    interval - intervalEditor;
    delay(300);
    Serial.println(interval);
    Serial.println("decreased");
  }

}
  interval + intervalEditor;

you probably meant

  interval += intervalEditor;

Hello,
Can you be more precise about your objective.
Maybe we can help you even more?
Is LED blinking with a variable invterval time you want to change with the 2 buttons?
Best regards,
Johi.

Hi,

You should read the buttons outside the LED ON/OFF condition block:

  buttonState = digitalRead(buttonPin);
  buttonState1 = digitalRead(buttonPin1);

  if(currentMillis - previousMillis >= interval){
    previousMillis = currentMillis;

    if(ledState == LOW){
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);  // <--
  }

Reading the buttons inside the LED ON/OFF “if” block, you would be able to read the buttons state only in the exact moment the LED is being turned ON or OFF.
Also, I would write the LED state to the pin inside that “if” block. No need continuously update the LED after the change.

And, like UKHeliBob already said, use the equal sign to update the interval variable.

1 Like

Hello
Well you should add pinMode()´s for the buttons in setup() .

At least you need a timer function for debouncing, LED-Blink and button input polling. You can use a common struct to have access to variables.

struct TIMER {
  unsigned long stamp;
  unsigned long interval;  
};
TIMER debouncing {0,20};
TIMER ledBlink {0,1000};
TIMER buttonPolling {0,500};

This common structure is used for blinking led as follows:

if(currentMillis - ledBlink.stamp >= ledBlink.interval){
  ledBlink.stamp = currentMillis;
  digitalWrite(ledPin, !digitalRead(ledPin, ledState)); 
  };

This code snipe can be used to build the timer for debouncing and button polling function.
Or you may merge the debouncing and button polling function.

Whilst I agree that would make the code more specific and would allow the use of INPUT_PULLUP, the pins default to being inputs

yes

There is an post where I present a timer object
that encapsulates millis() completely. This makes the approach more simple.

What a mixed-up piece of code that was - using the proper millis() timing in one part, and then introducing several "delay()" calls! :astonished:

Anyway the project i was planning to do was finished and the code only used millis() for a button system.(which i later cleared off after findng a button library.).

Anyway the project i was planning to do was finished and the code only used millis() for a button system.(which i later cleared off after findng a button library.).Thanks for your help.