I might be made to sound like a fool but I think there is an error using the "return" statement in the "void loop()". Although I'm only a learner in the C language, I believe the loop should stop running when the program comes to the "return" line as suggested in one of the "return" examples in the following link http://arduino.cc/en/Reference/Return
Can some of you guys please run the following code and see if the text keeps being printed to the serial monitor? It will only take two seconds of your time - I promise
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Return should stop loop after this line!");
return;
}
If somebody can give me any feedback one this issue, that would be much appreciated. If I'm doing/saying something stupid, don't be afraid to say so...
Where do you expect it to return to and why do you think that after returning to where it was called that it will not be called again? There is a hidden function that calls loop() repeatedly, hence its name.
If you really want code to be run once then put it in the setup() function.
If you want code to execute a number of times or in response to user or sensor input then stop, put it in the loop() function and set a flag to stop it being executed again, or create a while loop that will always be true.
I was only intending to use the return statement as a "debugging" tool. Using while loops for debugging can become confusing after a while - well for me anyway :0. Bob, can you please elaborate a little bit more in regards to setting flags. Perhaps you can paste a small example code of what you mean by this?
So do I assume the example (second one) given on the Arduino reference web page is incorrect? The link is on my previous post and I have quoted it below. This was the source of my thinking!
void loop(){
// brilliant code idea to test here
return;
// the rest of a dysfunctional sketch here
// this code will never be executed
}
That seems to work but I can't see "exit" under Arduino's list of Control Structures given in the following link Arduino - Home
Where did you get that one from and why do you need to put (1) after exit? I did notice that this statement effected the printing of the line to serial monitor. Only prints "Re" now. Would you agree what Arduino has on their web page is incorrect?
Hi AWOL,
Did you actually compile the code and give it run on a board? It looked good for me as well but you can see what the other guys have said about it. Regarding the NB; your right, it is a Control Statement. I do need to watch my programming speak being a Mech Eng.
I can't see "exit" under Arduino's list of Control Structures given in the following link Arduino - Home
Because the home page only lists functions/feature that are unique to the Arduino. exit() is a standard C function, so it, along with many other C functions can be used on the Arduino.
why do you need to put (1) after exit?
The parens are because you are calling a function. The function expects a value.
I did notice that this statement effected the printing of the line to serial monitor. Only prints "Re" now.
The rest of the letters will be shifted out when exit() ends. Never, that is. If you need to see all the text, that is just about the only place where Serial.flush() has a place in your code.
Bob, can you please elaborate a little bit more in regards to setting flags.
Things have moved on in this thread whilst I have been away from the PC. What I had in mind was to initialise a boolean variable to false then change it to true when the code to be run once had executed the first time, and to check before running the 'once only' code that the variable was not true, thus preventing it from running again.
holmes4:
loop() is declared as void in which case it cannot return anything. break is a quick way out.
You seem to be confusing not being able to return a value, with not being able to return. You also seem to be confusing the requirement to return from the loop() function with being able to break out of a control loop.
It's understandable, though; the people who named the loop() function made this sort of confusion more or less inevitable when they chose such a daft name for it.