Reputation: 13
In the ASCII table example sketch there is an intentional infinite loop:
if (thisByte == 126) { // you could also use if (thisByte == '~') {
// This loop loops forever and does nothing
while (true) {
continue;
}
What is the point of doing this in void main()
? Why not just put everything in void setup()
?
Is this useful for running a function once? I think if it were to be used in a particular function, it would be stuck in the infinite loop.
Upvotes: 0
Views: 1462
Reputation: 2183
The infinite loop makes the Arduino stop doing anything in the loop()
when a certain condition has been met. It is just a simple way to make it stop looping when it is done doing what it did. Putting the MCU in a deep sleep or power down mode indefinitely would also work for this.
And yes, you could do everything in setup()
, that is always the case. Any Arduino program can be rewritten to do everything in setup()
.
The main reason for not doing this is that setup()
is intended for setting things up, and the loop()
is intended for looping and running the main functionality, by convention, and because otherwise, the two functions would have silly names.
Everybody is free to do it their own way, though.
Upvotes: 1