I seem to remember somewhere somebody making an "Always" loop between Setup() and Loop().

But I can't find any documentation on it. Does such a thing exist, or am I imagining it?


There's nothing officially called an "always" loops as such. However, you can make a loop which goes on forever like this:

while (true)
    // this bit repeats forever

A while loop checks the condition before every iteration of the main body. If it detects that the condition equals boolean false then it will stop the loop and move on to the next bit of code.

Normally, the condition would be some kind of expression, like (x < 10). However, in the example above, the condition is hard-coded as true, meaning it will never be false so it will never stop.

  • Another variant - with exactly the same result - is for(;;){ ... }, which appeals to me for the simple reason that it reads almost like the word "forever". (Or maybe I just have an overactive imagination.... :-) – JRobert Jun 7 '15 at 12:24

You might be thinking of the Serial Event system.

This is a specially defined function which runs whenever loop() finishes if there is anything to read from the serial port.

Basically it is implemented as a function call after loop() in the main() function, which looks like this:


for (;;) {
    if (serialEventRun) serialEventRun();

As you can see there is only setup(), loop() and the serial event system.

  • serialEventRun is a weak reference. If some code file (typically an included library) defines a function by that name, it is called. It really does not check for serial data, but that is what it could be used for. The name only hints at a purpose, but nothing really ties it to serial data ready (despite misleading documentation). – user6569 Jun 7 '15 at 12:07
  • 2
    @Spiked3 I beg to differ. Yes, it is defined as weak, but there is a default one provided in HardwareSerial.cpp:190 which checks for serial data availability. Yes, you can override it with your own, but if you don't (and few would) then it acts exactly as I described. – Majenko Jun 7 '15 at 17:14
  • Interesting. We must have different code, as I am looking and the line you mention is at 63, not 190, but it is preceded with a note '// this next line disables the entire HardwareSerial.cpp, // this is so I can support Attiny series and any other chip without a uart". In any case, in context of this question, not important, your answer stands as correct. – user6569 Jun 8 '15 at 0:57

Your Answer

