button and leds problem with code :D

hi, i have a problem with my code…
now, when i push the first button i have to wait to loop the first part 15 times and after this i can push the button2 to do the second part
what i really want is when first part is running and it is for example at 5th loop, pressing the second button the first part stop and second part do its loop

my code:

#include <Button.h> 

Button button1(10); 
Button button2(11); 


void setup(){
 pinMode (10, INPUT);
 pinMode (11, INPUT);
 pinMode (5, OUTPUT);
 pinMode (6, OUTPUT);

 button1.begin();
 button2.begin();

 while (!Serial) { }; 
 
}

void loop(){
  if (button1.pressed()){ 
    for (int i = 0; i < 15; i++){
  digitalWrite(5, HIGH);
   delay(3000);
   digitalWrite(5, LOW);
   delay(1000);
   digitalWrite(6, HIGH);
   delay(3000);
   digitalWrite(6, LOW);
   delay(1000);}
    
  }
 else if (button2.pressed()) {
    digitalWrite(5, HIGH);
   delay(3000);
   digitalWrite(5, LOW); 
    }
    
 }

what i really want is when first part is running and it is for example at 5th loop, pressing the second button the first part stop and second part do its loop

The answer is exactly the same for you as it is for everyone else. Get rid of EVERY single delay() in your code. The loop() function loops, so get rid of EVERY for loop in loop().

When you consider that loop() loops, and that, on any given iteration of loop(), there may, or may not, be something to do, based on switch presses, counts, and time, you can easily see how to write the code without delay()s or for loops.

Look at the blink without delay example, and google "state machine" if you need more inspiration.

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

i cant make it work,
this is now the code:
any help? :smiley:

#include <Button.h> 

Button button1(10); 
Button button2(11); 


long storedTime=0;
int secIndex =0;
byte k=15;


void setup(){
 pinMode (10, INPUT);
 pinMode (11, INPUT);
 pinMode (5, OUTPUT);
 pinMode (6, OUTPUT);

 button1.begin();
 button2.begin();

 while (!Serial) { }; 
}


void loop() {

  if (button1.pressed()) {
    k=0;
    storedTime=millis();
  }
  if (button2.pressed()) {
    k=15;
    storedTime=millis();
  }

  if (k<15) {
   secIndex = (millis() -  storedTime) /1000;
   if (secIndex<3) digitalWrite(5, HIGH);
   else if (secIndex<4) digitalWrite(5, LOW);
   else if (secIndex<7) digitalWrite(6, HIGH);
   else if (secIndex<8) digitalWrite(6, LOW);
   else storedTime=millis();
  }
  else{
      secIndex = (millis() -  storedTime) /1000;
     if (secIndex<3) digitalWrite(5, HIGH);
     else if (secIndex<6) digitalWrite(5, LOW);
     else storedTime=millis();
  }
  k++;

}

i cant make it work,

"It doesn't work", or any variation on that theme, is completely useless here.

The code does something. You expect the code to do something. Only you have a clue what either of those things are.

Before you post any code again, EVERY { goes on a line BY ITSELF. EVERY } goes on a line BY ITSELF. EVERY if, else if, and else statement will get curly braces, even if there is only one thing to do.

AND, ALL variables will get meaningful names. secIndex doesn't mean squat.