Very bad example
The example was not meant to show the best possible programming practices, only to better explain my question without distracting with unnecessary details.
You shouldn’t use a while() function in setup() and best avoided anywhere.
Sorry, I can’t agree with such a general statement. I guess 99.99% of programmers are happily using while() in their programs. Like in any other iterative programming constructs, the programmer is responsible to ensure that there is a way out of the loop. That’s the main concern. Often, a millis()-based timeout is used for this.
setup() IMHO has nothing special when compared to any other function that is called in the sketch, except it is the function that’s executed at first. Really, I’m missing your point here. Maybe you should better contextualize your statement against usage of while() instruction.
Do you an example of a waiting loop in plain English, not in code format?
Well, an application interactively waiting for an input from the user (to act accordingly afterwards), may be a good example of a waiting loop. While the user decides what to do, the program has nothing better to do than waiting.
It used to be called “polling” (still is?). In other words, you wait (do nothing) until you get what you need. I agree that it is not considered the most efficient programming practice, it should be avoided in all situations where the CPU has a better way to spend its time than doing nothing, but in some situations it can’t be avoided. For example, if you have an application that works on a wifi connection, you can’t go on exchanging data on the network if you didn’t connect to WiFi before. So I can’t see what other you can do than waiting while the connection is being established (the classic
while(!WiFi.status() == WL_CONNECTED) { }
you see everywhere ) .
And YES, adding a timeout to this while()
would be appropriate too!
If you need to send a signal to the MCU can’t you simply set a timer i.e. if no button pressed in 10s then device goes into deepSleep?
I could reply by simply asking you what would the sketch be supposed to do while the 10s pass (other than a waiting loop), but I want to give you a more useful answer.
I’m doing things more complex than sending a signal to the MPU. For example, I have a calibration procedure in my sketch, in which the sketch prompts the user to input several numeric values in succession. So each step requires a waiting loop, I can’t go in DeepSleep in the middle of such interactive procedure! So, I DO need waiting loops here! What else I can do?
However, you are right where you suggest that if the user doesn’t input any data in a “long” time, a timeout should allow the execution to go on, and finally jumping into the DeepSleep that is at the end of the setup() routine. It is exactly what I’m doing. But this doesn’t save me from the while() loops for the “normal” delays when the user is “thinking”, don’t you agree?
Since I don’t see while() instructions as evil, I’m not worried about using them, only concern is if it is required to call Blynk.run() inside them, to keep the whole Blynk environment “alive”.
Are your buttons virtual or physical?
As I wrote in my first post, I’m only using virtual pins.
Not just buttons though, I’m also using several types of widgets, including terminal, history graphs, menu, lcd display, RTC…