Why did I say "Programming Arduino like an adult! Kill the loop()!"? First, we need to know where this construct came from. A sketch is a variant of the C++ programming language. Hell, wait doesn't C++ have a main() function? Yep, it surely does. Then how and why did we (it) end up with the loop() function? Well, they had this dumb idea, that the language should be easy to master for artists and children. Well C++ has never been one of the go-to languages as its scoping rules are worse than Java's. Syntactically it looks like C or C++ without classes. So you end up with a hot mess, that uses two (2) new functions. The first is void setup() which is where you stick your libraries and what they call "run once" (this opens the door for a lot of interpretations, and I will not get into that here). The second is a void loop() which should have never seen the light of day as it leads one into very sloppy programming as it redefines main() with a MACRO.
Here is my case for dumping void loop(). Do you see the keyword void which means your program can not return anything usable, like hey we failed, and maybe why.
#define FAIL -1
#define PASS 1
void setup(){
Serial.begin(9600); // set up Serial library at 9600 bps
}
int main(){
}
int init_ser1()
byte n = Serial.available();
if(n !=0 ) {
char x = Serial.read();
if(x != '\n') { // this fails as it did not get what it expected
rtn = FAIL; }
else { rtn = PASS; }
program }
}
retirm( rtn );
}
This is called diagnostic testing. In the example I have two (2) Arduino talking to each other, one is the salve, and the other is the master when the master powers up he listens for the slave he does not hear it in let's say two (2) seconds he now fails and sends an error message via the return() function. In the master, I have a table of error messages and their sender. So now it is a simple matter to do the following things. tell me (human) that the master can't find the slave, print the error to the screen, and or print the error to a log file. or just retry.
In NexGen sim I have about 8 radios, it would be nice to know I one was turned off.. LOL