Loop that doesn't interrupt the execution of the code

Hi!

I'm looking for a way to repeat a part of my code without interrupting the execution of the rest of the code. For example I want to blink a LED continuously in a loop but the rest (reading inputs and stuff) shouldn't be stopped. As far as I know a while loop doesn't work because it would repeat itself until the variable is not true anymore and then execute the rest of the code.

Thanks for your help!

For example I want to blink a LED continuously in a loop but the rest (reading inputs and stuff)

No, you don't. You want to periodically turn an LED on or off. Look at state machines and the blink without delay example.

I took a look at the blink without delay example but wouldn't that just run one time each time the loop() is repeated. I'm looking if there is a way to repeat the "led blinking" continuously in the background while he code is executed normally.

Maybe it helps if i explain my Problem. I have LEDs connected in a LED matrix and tried to use less pins. So I need to switch the states very fast that all LEDs seem to be turned on. If I add this now to my code I can see that they're not continiously turned on. I'm looking for a way to speed up the changing of the states so that all LEDs seem to be turned on. Sorry if my first explanation wasn't clear.

I'm looking if there is a way to repeat the "led blinking" continuously in the background

That implies a foreground that is distinct from the background. That is not possible on the Arduino, since it has just one processor.

The loop() function is (or should be) called hundreds of thousands of times per second. Most of the time, it should accomplish nothing. Now and then, it should determine that it is time to change state, and should react accordingly. The blink without delay example shows how this works.

Think about how you would cook bacon and eggs for breakfast. Is it possible for you to be cooking both at once? Of course it is, even though there is only one of you. Periodically, you check to see if it is time to turn the bacon. Periodically, you check to see if it is time to turn the eggs. Periodically, you check to see if the bacon is done. Periodically, you check to see if the eggs are done.

Now, suppose that you want toast and coffee with your bacon and eggs. You can imagine, easily, I'm guessing, how you would handle checking on the toast and coffee while the eggs and bacon are cooking.

Thank you that makes sense! And now I understand the Blink without delay example. Moreover I found a short delay in another part of my code… My bad. Thank you for your help!

Think about how you would cook bacon and eggs for breakfast. Is it possible for you to be cooking both at once? Of course it is, even though there is only one of you. Periodically, you check to see if it is time to turn the bacon. Periodically, you check to see if it is time to turn the eggs. Periodically, you check to see if the bacon is done. Periodically, you check to see if the eggs are done.

Now, suppose that you want toast and coffee with your bacon and eggs. You can imagine, easily, I'm guessing, how you would handle checking on the toast and coffee while the eggs and bacon are cooking.

Pauls: I like this example, is a great analogy that I have never heard before... I look forward to using it when teaching others.

Pauls: I like this example, is a great analogy that I have never heard before… I look forward to using it when teaching others.

To be honest, I stole it from Nick Gammon, so he should get the credit.

PaulS:

Pauls: I like this example, is a great analogy that I have never heard before... I look forward to using it when teaching others.

To be honest, I stole it from Nick Gammon, so he should get the credit.

I bought an Amiga in about 1987, and was a regular speaker at our local Amiga User Group. The Amiga was a fuly multitasking computer (not the first home computer to multitask, but the first one that became popular), and at that time, Microsoft was not even considering multitasking for their OS, because, as Mr. Gates claimed, you could not multitask with less than 4 MB of memory.

One of my favourite teaching tools was that particular analogy (though I usually used a roast, potatoes, vegetables and a salad as players in the story. I too, read it somewhere, probably on a Usenet posting, so it's been around for ages. Doesn't make it any less informative. I'm very happy to see it still being used.

Hw can I do it wthout trnng my eggs. Plz hlp Im stuk.