I am writing code for a hardware user interface device to simulate the control of my tank.
The code needs to be fast, so I want the loop() function to go back to the beginning each time it detects and processes and event (switch states changing).
The way I have my code set up is that I declare all my variables, then use setup() to initialize variables and set initial conditions for processing, then I use loop() to scan for any changes in switch states and if any changes are detected the appropriate code will be run.
How do I get loop() to restart? I have not found any clear and concise solutions on google. Here is an example of my code:
int myints;
void setup()
{
int myints = whatever;
}
void loop()
{
swPro(); //Reads switch states and programs corresponding values (to be tested by each function)
swOne(); //A function to test switch one
swTwo(); //A function to test switch two
swEtc(); //etc...
}
void swOne()
{
//does stuff
return; // <-- I thought this will restart the the loop() from the beginning instead of processing the rest of the code after it, but I am not sure. Please clarify this for me and if I'm wrong please explain how I could go back to the beginning of loop() from here.
}
void swTwo()
{
//does stuff. Similar to swOne().
}
void swEtc()
{
//etc...
}
I just to reiterate that my question is this: What code do I write in each function to restart the the loop() function?
(Note: I do NOT want the Arduino to rerun the declarations or even the setup() function.)
There are two cases for each routine, right? One where it wants loop to exit/go around, and one where it wants loop to continue down the list of check functions.
I would say it's easiest if you return 1 if you want loop() to go around, and return 0 if you don't.
What? No, I don't want to test each function - as in a conditional like if(swOne()). I want the command to restart loop() to be inside each function (swOne(), swTwo(), etc...).
So does having a return 0; or a return 1; inside those functions cause the loop() to restart?
I understand the structure of having an if() testing a function that is returning a 0 or 1, but to me that is not very elegant when you have 40+ switches to test.
Have you any concept of how fast the Arduino runs ? True, it is not the fastest thing on Earth but it is pretty darned quick compared with anything that your tank will be doing I suspect.
Let's look at your requirements from the other way. How fast does does the code need to be ?
Your suggested solution of returning to the start of loop() after processing a function is full of holes, not least that it may mean that some functions are never executed. Once a condition in loop() is satisfied and you return to the start, then conditions after the one satisfied will not be tested and this could continue forever.
Surely the way to do what you want is to take advantage of the fact that the loop() function does what it says and not to subvert its very purpose. Do not use delay() within functions as that blocks execution of other code, use millis() as a timer instead. I have a simple 2 wheeled vehicle that accelerates up to a fixed speed, maintains straight running by monitoring wheel rotation and monitors the space ahead for obstructions, all done sequentially in loop() with no delays. This uses millis() to determine when each function should be executed.
What is it about your tank that needs to be so fast ?