I want whenever i press a button my delay goes lower and my led blink alot more faster. Yet i have failed

Heres my code below

const int BUTTON1 = 3;
const int BUTTON2 = 4;
const int LED1 = 8;
const int LED2 = 9;
const int LED3=10;

int d = 1000;
void setup()
{
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  Serial.begin(9600);
  digitalWrite(BUTTON1,HIGH);
  digitalWrite(BUTTON2,HIGH);
}

void loop()
{

  if (digitalRead (BUTTON1)== 1)
  {
    digitalWrite(LED1, HIGH);
    delay(d);
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(d);
    digitalWrite(LED2,LOW);
    digitalWrite(LED3,HIGH);
    delay(d);
    digitalWrite(LED3,LOW);
    
  } 


  if (digitalRead(BUTTON2) == 1)
  {
    d= d-100;
  } 

}

So far i have not been able to succeed doing this. The delay didn't go lower and the led blink delay is still the same? Sorry for my bad english.

Since the internal pullups are enabled, the switches should be wired to ground and the input.

How are the button switches wired? Do the switch inputs have pull down resistors installed? Schematic please.

The more accepted way to wire a switch is to wire the switch to ground and an input set to pinMode INPUT_PULLUP. The switch will read HIGH when not pressed and LOW when pressed. Adjust the code accordingly.

1 Like

delay does what the name says: DELAY

This means as long as the program is delay-ing it does nothing else than delay.
It simply does not execute your if-condition.

your program is UN-responsive because of that. This is called blocking timing.

It may sound like a simple exercise but it requires a totally different approach with non-blocking timing based on function millis() which is something completely different. And works completely different than a delay.

best regards Stefan

i tried to make the value of the delay go lower whenever I press button 2.

best regards Stefan