If / while loop interrupt with a console key

The objective of the code I am trying to write is that while the process is either in an If...loop or While... loop, a key entry in the console (or I should call it the serial monitor) will interrupt the process and based on the key entered initiate another loop (If or while).
Here I what I wrote so far, but I cannot find the way to interrupt the process when the variable "number" is between 20 and 10, to go to the line: if (order=="S"). Any suggestions, please?
int number=20;
'''
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
while (Serial.available()==0){}
String order=Serial.readString();
order.trim();

if (order=="F")
{
while(number>10) //HIGHER FROM RANGE WHILE
{
number=number-1;
Serial.print("number is avobe range ");
Serial.println(number);
delay (5000);
} // END OF HIGHER FROM RANGE WHILE
while (number<3) //LOWER FROM RANGE WHILE
{
Serial.print("number is below range ");
Serial.println(number);
delay (5000);
} //END LOWER FROM RANGE WHILE
} //END IF ORDER F

if(order=="S")
{
Serial.print("last number: ");
Serial.println(number);
} // end if order S

} //END LOOP
'''

I'm making it readable with auto indent and code tags:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  while (Serial.available() == 0) {}
  String order = Serial.readString();
  order.trim();

  if (order == "F")
  {
    while (number > 10) //HIGHER FROM RANGE WHILE
    {
      number = number - 1;
      Serial.print("number is avobe range ");
      Serial.println(number);
      delay (5000);
    } // END OF HIGHER FROM RANGE WHILE
    while (number < 3) //LOWER FROM RANGE WHILE
    {
      Serial.print("number is below range ");
      Serial.println(number);
      delay (5000);
    } //END LOWER FROM RANGE WHILE
  } //END IF ORDER F

  if (order == "S")
  {
    Serial.print("last number: ");
    Serial.println(number);
  } // end if order S

} //END LOOP

You can't "interrupt the process" when it's based on the delay() function. Nothing can happen when delay() is running, that is what it is designed to do.

humm

int variablething =0;

voiding loopy()
{

if( (variablething <= 20) && (variablething >= 10 )


}

or some thingy like that.

Thank you aarg.
I was not able to fully read your guide on how to make the code readable. Can you please send me the link for that?

Which may be a reason the thing is unresponsive to a press. delay(5000) means don't detect keyboard press for 5 seconds.

It's not mine. It's in the forum introductory threads. Look at the first few forum posts. Actually, with the new forum software, you were asked to read that stuff, and maybe you didn't comply since you have no badge for it.

The IDE has a "copy for forum" menu item. When you use that, you can just paste into a message here. It's in the Edit tab.

Auto format is ctrl-T or another menu item.

Please, consider I am far from being a coding expert. I don't see in your suggestion where a key entry in the serial monitor will interrupt a process.

Exactly. It can't. There are multiple suggestions for solving the "more than one thing at a time" problem in the intro threads ...

Thanks a lot

I hope you are sincere. Honestly, I am trying to speed you to a solution. It will get too long winded here, just going and reading will get you there faster. The material there has been compiled and revised many times, to be useful in this common situation that you are in.

If I said, "you need to implement cooperative multitasking using millis() timing" or something like that, we would just be repeating 10000 threads here that beat it to death.

Well, I took the delays off, increased the initial value of the variable from 20 to 2000 and still I cannot interrupt the loop.
Also, I apologize, but when I copied the code, it was missing the variable declaration before the void setup()

the OP can look at Files|Examples|02.BlinkWithoutDelay for an intro in using millis() for timing. With delay() the operations are stopped but with millis() a delaying action can be done but during the delaying action other things can be done; like check on other thingies.

If the code you tried to do thing thing was posted then we could see what's missing and provide clues or even just write the thing for you.

Then you have multiple problems. In such a case, you can never be sure which part of your code is failing. You need to do some "divide and conquer" testing with simpler test sketches that only exercise one part of your application.

If your input method is failing, and you fix it, the program will still not work and can't be made to work, using the present approach using delay().

As you can see, I didn't even look into the IDE interface. Got it.
Will go to the forum guidance before I post anything else.

Well, I think this is an extremely simple code. Just a counter that needs to be interrupted with a key entry. I did not enter the actual job because it would require the use of sensors, and steppers to verify operation. All that part I have done and it's working. However, I have to interrupt one operation with a key entry, and that is why I came to this simplified code to find out how I can do that interruption. Here is the code again from the IDE this time.




int number=2000;

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600); 
}

void loop() {
  // put your main code here, to run repeatedly:
  while (Serial.available()==0){}
  String order=Serial.readString();
  order.trim();

   if (order=="F")
{
   while(number>10)   //HIGHER FROM RANGE WHILE
   {
 number=number-1;
    Serial.print("number is avobe range ");
    Serial.println(number);
 //   delay (5000);
   }  // END OF HIGHER FROM RANGE WHILE
   while (number<3)   //LOWER FROM RANGE WHILE
   {
     Serial.print("number is below range ");
    Serial.println(number);
 //       delay (5000);
   }  //END LOWER FROM RANGE WHILE
}  //END IF ORDER F

  if(order=="S")
  {
  Serial.print("last number: ");
  Serial.println(number);
} // end if order S

   
} //END LOOP



aarg. the reason I used a delay, was to give me time to enter a key before the counter reach the end. to walk around that, I increased the counting range and that gave me extra time to enter any key.
Let's just forget about the use of delays in the process, please.

Idahow, I re-posted it in post #17

A counter with no timing? The computer can count to a million in a fraction of a second. How would interrupting that be useful?

If you want to interrupt a long series of prints, the pseudocode would look like

check the input
while there is no key input
{
check the input
print one line
}

About your comment regarding the counter, I fully understand, however, given that between counts there is a print process I wonder (I really do not know) if that delay between counts does not give me time to enter a key.
Regardless, I will look into your "check the input" suggestion. Isn't this "while there is no key input" the same as "while (Serial.available()==0)"?