Change delay for millis - problem

Hi.
I wrote a code which set PWMspeed1 when I press button 1, next when I press button2, code have to wait a few second(here is "x").
Problem is when speed2 is active and I release button2 LED is still ON because of delay function. I heard about millis() but can't put it in my code.

Pls help me.

int s1;  //pr?dko?c pierwsza
int s2;  //predkosc druga
int x=1000;







void setup(){
  
  
  pinMode(4,INPUT); // wejscie 1 (speed1)
  pinMode(5,INPUT); //wejscie 2  (speed2)
  digitalWrite(4,HIGH); //podci?ganie do plusa
  digitalWrite(5,HIGH); //podciaganie do plusa
  
  
  
  
}



 
void loop(){
  
  
  s1=analogRead(A0); //czytanie nastawionej wartosci na pot1
  s1=map(s1,0,1023,0,255); 
  
  s2=analogRead(A1);  //czytanie nastawionej wawrto?ci na pot2
  s2=map(s2,0,1023,0,255);

  


if(digitalRead(4)==0 & digitalRead(5)==1) { //sygna? nr.1 w??cza PWM nr.1

 

  
  
  analogWrite(9,s1); // PWM
  
  
  
}

if(digitalRead(5)==0) { //sygna? nr. 2 w??cza PWM nr. 2
    
   
      
      delay(x);          //czekam okre?lony czas
      
  
    if(digitalRead(5) == 0) 
        
                analogWrite(9,s2);
                                       
      
       
               else 
                     analogWrite(9,0);
                    
    
  }
}
 
      
              
}

Is this what you are trying to do?

Press Button4: speed = Speed1
Press Button5 for time < x: speed = 0
Press Button5 for time > x: speed = Speed2

Press button4 - speed1
Press button5 - wait x then speed2
When I release button2 - STOP
Next when I press button4 - speed1...

It must be in loop.

Help others help you.

Please don't make reading your code unnecessary difficult. And at least compile it before copy-n-pasting it here.

int s1;  //pr?dko?c pierwsza
int s2;  //predkosc druga
int x=1000;


void setup() {
    pinMode(4,INPUT); // wejscie 1 (speed1)
    pinMode(5,INPUT); //wejscie 2  (speed2)
    digitalWrite(4,HIGH); //podci?ganie do plusa
    digitalWrite(5,HIGH); //podciaganie do plusa
}


void loop(){
    s1=analogRead(A0); //czytanie nastawionej wartosci na pot1
    s1=map(s1,0,1023,0,255); 

    s2=analogRead(A1);  //czytanie nastawionej wawrto?ci na pot2
    s2=map(s2,0,1023,0,255);

    if(digitalRead(4)==LOW && digitalRead(5)==HIGH) { //sygna? nr.1 w??cza PWM nr.1
        analogWrite(9,s1); // PWM
    }

    if(digitalRead(5)==LOW) { //sygna? nr. 2 w??cza PWM nr. 2
        delay(x);          //czekam okre?lony czas
        if(digitalRead(5) == LOW) {
            analogWrite(9,s2);
        }
        else {
            analogWrite(9,0);
        }
    }
}

Use LOW and HIGH, not 0 and 1. Always prefer named constants to literals.

Also, the first if() had a boolean test performed with a bitwise and. Bitwise and: &, boolean and: &&. Don't substitute one for another, or you'll insert subtle bugs sooner or later.

my 2 cents.

I heard about millis() but can't put it in my code.

delay() stops the program until the specified amount of time has passed. millis() just returns the number of milliseconds elapsed since the program started.
You can use it to execute code at specified time intervals, while allowing the program to run in the meanwhile, i.e. you can obtain a sort of multitasking. This requires a change of mindset though, from sequential programming to state-based programming.

It's not that hard, actually. You must introduce state variables, which keep track of program or hardware peripheral state.

currBtnState = digitalRead(btnPin);
if (currBtnState != prevBtnState) {
    if (currBtnState == LOW) {
        // button was pressed: execute action
        digitalWrite(ledPin, ledState);
        ledState = (ledState == HIGH ? LOW : HIGH);
    }
}
prevBtnState = currBtnState;

Please compare blink and blink without delay examples to have an idea of the difference between the two types of program.

HTH