How do I detect a signal during a delay?

Hi.
My code consists of a few nested loops and delays. I won't bother you with the whole code, but it looks something like this:

while(1){
  for (...){
    instructions1
    delay(...)
    for(...){
      instructions2
      delay(...)
    }
  }

  delay

  for (...){
    instructions3
    delay(...)
    for(...){
      instructions4
      delay(...)
    }
  }
  delay(...)
  
}

I have designed a GUI in Processing in which when the user presses a button, a "\n" is sent over to the serial port and the Arduino should be able to detect that and break out of the "while" loop and then wait for other inputs to determine what the Arduino should do next. The problem is the user may press the button during one of those delays, or while the instructions are being executed. How do I avoid missing those clicks?
I had the idea that maybe I could send the "\n" over and over again, once the button has been pressed. But then it would mess with the next commands the user would want to send.

I won't bother you with all of my answer, but get rid of the for loops and the delays.

AWOL:
I won't bother you with all of my answer, but get rid of the for loops and the delays.

That would be a problem, because those are pretty important. Do you mean it is impossible to do such a thing?

Mylaiza:
Do you mean it is impossible to do such a thing?

No.
Why on Earth would you think that?

AWOL:
No.
Why on Earth would you think that?

Because you basically said I should get rid of all the 'code' I posted here?

No, I wrote that you should get rid of the code that is preventing you reach your goal.
You didn't actually post any code, remember?

AWOL:
You didn't actually post any code, remember?

Hence the quotation marks.

Here's some explanation:
I'm generating a certain waveform. In order to do that, I need to set a few I/O pins to 1 or 0 for a certain amount of time. The delays help with the timing. The for loops are needed because of the waveform that I'm going for.

The delays help with the timing

But the delays clearly don't help, or we wouldn't be having this dialogue.
So get rid of them.

What he is trying to say is that you should post all of your code and place them into code blocks.

There is no need to have a blocking while inside your loop. The first link I presented will teach you how to NOT use delay, the sooner you learn to not use it the better you are.

The 2nd link will expand on that knowledge.

https://forum.arduino.cc/index.php?topic=503368.0
https://forum.arduino.cc/index.php?topic=223286.0

There is no use for any delay() call, except for convenience during quick tests. A delay() stops you from doing other things. That's why you have to get rid of them. There is even less use for a while(1) loop.

So yes, basically all of the "code" you posted has to go.

If you need precise timing you can use the millis() or micros() functions, if that's not good enough timer interrupts.

you use delays in void setup(). in void loop, use millis()

Read "How To Use This Forum"

This is what happens when you do not

Using millis() for timing. A beginners guide

Mylaiza:
Here's some explanation:
I'm generating a certain waveform. In order to do that, I need to set a few I/O pins to 1 or 0 for a certain amount of time. The delays help with the timing. The for loops are needed because of the waveform that I'm going for.

Seventeen posts prior to this particular foray, we might have expected you to do some reading of other problems by way of research.

We know the classic XY problem when we see it.

Only now have you posted the correct question (along with two clearly wrong assertions). :grinning:

OK, move on and do the research into how to actually code for a microprocessor. :sunglasses: