Want to use millis() instead of delay()

Hello all.

I would like to ask for help for my sketch.
There are two push buttons and two LEDs,
the intention is that if button 1 is pressed, LED 1 will flash ten times for three seconds.
The same for button 2 and LED 2.
When the buttons are not touched, the two LEDs fade alternately.

The sketch does work, problem is that when button 1 is pressed and LED 1 flashes,
button 2 and LED 2 do not react in the meantime due to the delays.

I would like to replace all delays in millis () then that problem should be fixed.

Below the code, thank you very much!

int value1, value2 ;

int S1 = 2;  // switch P1
int S2 = 3;  // switch P2
int L1 = 10; // led P1
int L2 = 11; // led P2

long time=0;

int periode = 8000;
int displace = 4000;

void setup() {                
  // initialize the digital pins.
  // assume switches will wire from ground to input pins
  pinMode(S1, INPUT_PULLUP);
  pinMode(S2, INPUT_PULLUP);
  pinMode(L1, OUTPUT);
  pinMode(L2, OUTPUT);
}

void loop() {

  if (!digitalRead(S1))
  { 
    digitalWrite(L1,LOW);
    digitalWrite(L2,LOW);

    //-------------------------------

    digitalWrite(L1,HIGH);
    delay(100);
    digitalWrite(L1,LOW);    
    delay(100);
    digitalWrite(L1,HIGH);    
    delay(100);
    digitalWrite(L1,LOW);    
    delay(100);
    digitalWrite(L1,HIGH);
    delay(100);
    digitalWrite(L1,LOW);
    delay(100);
    digitalWrite(L1,HIGH);
    delay(100);
    digitalWrite(L1,LOW);
    delay(100);
    digitalWrite(L1,HIGH);
    delay(100);
    digitalWrite(L1,LOW);
    delay(100);
    digitalWrite(L1,HIGH);
    delay(100);
    digitalWrite(L1,LOW);
    delay(100);
    digitalWrite(L1,HIGH);
    delay(100);
    digitalWrite(L1,LOW);
    delay(100);
    digitalWrite(L1,HIGH);
    delay(100);
    digitalWrite(L1,LOW);
    delay(100);
    digitalWrite(L1,HIGH);
    delay(1500);
    digitalWrite(L1,LOW);
    delay(200);
  }

  //-------------------------------------------------------

  if (!digitalRead(S2))
  { 
    digitalWrite(L1,LOW);
    digitalWrite(L2,LOW);

    //-------------------------------

    digitalWrite(L2,HIGH);
    delay(100);
    digitalWrite(L2,LOW);
    delay(100);
    digitalWrite(L2,HIGH);
    delay(100);
    digitalWrite(L2,LOW);
    delay(100);
    digitalWrite(L2,HIGH);
    delay(100);
    digitalWrite(L2,LOW);
    delay(100);
    digitalWrite(L2,HIGH);
    delay(100);
    digitalWrite(L2,LOW);
    delay(100);
    digitalWrite(L2,HIGH);
    delay(100);
    digitalWrite(L2,LOW);
    delay(100);
    digitalWrite(L2,HIGH);
    delay(100);
    digitalWrite(L2,LOW);
    delay(100);
    digitalWrite(L2,HIGH);
    delay(100);
    digitalWrite(L2,LOW);
    delay(100);
    digitalWrite(L2,HIGH);
    delay(100);
    digitalWrite(L2,LOW);
    delay(100);
    digitalWrite(L2,HIGH);
    delay(1500);
    digitalWrite(L2,LOW);
    delay(200);
  }

  //-------------------------------------------------------

  else   { 

    time = millis();
    value1 = 128+127*cos(2*PI/periode*time);
    value2 = 128+127*cos(2*PI/periode*(displace+time));

    analogWrite(L1, value2);           // sets the value (range from 0 to 255)

    analogWrite(L2, value1);           // sets the value (range from 0 to 255)

  }
}

Pexy:
I would like to replace all delays in millis () then that problem should be fixed.

Good idea. When you’ve done that let us know how it works and if there’s anything we can help with.

Steve

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

...R

Robin2:
The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

...R

Thank you! i will look into it.

Also asked here: Het Nederlandstalig Arduino forum - Bekijk onderwerp - Delays vervangen in millis().

Before you hit up millis(), why the multiple digitalwrite(), followed by delay() ?
Almost all of them are redundant.

It’s solved.