Help me to change a running function using button

So the thing is i am trying to change the function while it is running... so i jave 2 relays,an arduino uno connected with a button so i am trying to turn the relay on/off for several times and i am cheking if the button is pressed and recording its count using Timer one interrupt function..

  Timer1.initialize(200000);
  Timer1.attachInterrupt(chkbtn);
   [code]

[code]void chkbtn()
{
  bs=digitalRead(but);
  if(bs!=lbs){
    if(bs==HIGH)
    {
      bc=bc+1;
      }

So take it as the board is on and the value of button counter is 0 on serial monitor.. i just pressed the button,value changes to 1, so a function will run to turn relay1 on for 5 sec and then will stop for 2 secs and then will turn off the relay 1 and turn on the relay 2...

void forward()

{Serial.print("clk");
  digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
}
void stp()
{Serial.print("stp");
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
}
void reverse()
{Serial.print("anticlk");
  digitalWrite(in1,LOW);digitalWrite(in2,HIGH);
}

and the below 1 is in void loop()

while (bc==1)
  {Serial.print("light");
    forward();
    delay(5000);
    stp();
    delay(3000);
    reverse();
    delay(5000);
    
  }

same for 2 and 3

while(bc==2)
  {Serial.print("mid");
    forward();
    delay(10000);
    stp();
    delay(3000);
    reverse();
    delay(10000);
  }
   while(bc==3)
  {Serial.print("heavy");
    forward();
    delay(30000);
    stp();
    delay(3000);
    reverse();
    delay(30000);
  }

so the thing is if i press the button the button gets counted bcoz of interrupt and now the value is 1 so the first condition will run "while(bc==1)" so it will run the relay for 5 sec than off for 3 secs and will turn 2nd relay on for 5 secs as you can check and if i am pressing the button 2nd time the value of bc gets changed to 2 but the catch is the condition doesn't gets changed it will execute for atleast once then it will change and i want to go on 2nd condition as soon as i press the button..more like i want to change the condition as soon i press button.... i have tried switch case,if else,goto statement,do while.. and in every one of these loop gets executed for once and i want to get out as soon as the button is pressed :slightly_frowning_face: :slightly_frowning_face: please help!!!!!!!!!!

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Post your full code, all as one piece.

Do a better job of explaining the problem you are having. Your description is extremely hard to understand. Use sentences!

#include <TimerOne.h>
int in1 = 10;
int in2 = 11;
int led = 13;
int val;
int extra;
int but = 12;
int bc = 0;
int lbs = 0;
int bs = 0;
void forward()

{ Serial.print("clk");
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
}
void stp()
{ Serial.print("stp");
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}
void reverse()
{ Serial.print("anticlk");
  digitalWrite(in1, LOW); digitalWrite(in2, HIGH);
}
void setup() {
  Serial.begin(9600);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(but, INPUT);
  Timer1.initialize(200000);
  Timer1.attachInterrupt(chkbtn);
  pinMode(led, OUTPUT);


}
void chkbtn()
{
  bs = digitalRead(but);
  if (bs != lbs) {
    if (bs == HIGH)
    {
      bc = bc + 1;

    }
  }
}

void loop() {
  digitalWrite(led, HIGH);
  delay(300);
  digitalWrite(led, LOW);
  delay(200);

  while (bc == 1)
  { Serial.print("light");
    forward();
    delay(5000);
    stp();
    delay(3000);
    reverse();
    delay(5000);

  }
  while (bc == 2)
  { Serial.print("mid");
    forward();
    delay(10000);
    stp();
    delay(3000);
    reverse();
    delay(10000);
  }
  while (bc == 3)
  { Serial.print("heavy");
    forward();
    delay(30000);
    stp();
    delay(3000);
    reverse();
    delay(30000);
  }
  if (bc > 3)
  {
    bc = 0;
  }
  Serial.print(bc);
}

so now the thing is if i press the button value of "bc" gets changed to 1 and the while loop will run for "bc=1" right??.. and will run for 13 sec ( 5sec for in1 high,3 secs for in1 low and 5 secs for in2 high)

 while (bc == 1)
  { Serial.print("light");
    forward();
    delay(5000);
    stp();
    delay(3000);
    reverse();
    delay(5000);

  }

so i just want to exit this loop if i pressed the button 2nd time.the value "bc" will change to 2 but the first loop"while(bc==1)" will run for 13 sec as programmed but i want to change the loop to "while(bc==2)" as soon as i presses the button

You'll need to rewrite your sketch to use non-blocking code. The idea is that your loop function will be constantly running many times a second and polling the button pin. That means no use of delay(). Instead you will use millis(). You can see a simple demonstration of non-blocking code in this tutorial:

Study that until you full understand it, then you can proceed to apply that knowledge to writing your new sketch.

If you want a responsive program you should not use delay() as the Arduino is blocked until the delay() completes. And for the same reason don't use WHILE or FOR unless they complete very quickly - in a small number of microseconds. Generally it is better to use IF and allow loop() to do the repetition.

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

Also look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R

thank you but i did it.. just needed to add a small check on stp() function