marco_c:
Can you create a function inside of loop() & reference that function within loop()?
Yes you can. No matter where you are the value of millis() returned comes from the same place.
Let me give a pseudocode example of what I meant; I'm not sure we're talking about the same thing:
loop(){
myFunctionOne(){
do something whiz-bang;
}
myFunctionTwo(arg1, arg2){
do something else;
}
if(blah){
myfunctionOne();
}
else myFunction2(arg1, arg2);
} // end loop()
If the above would work, that would be great, but I don't think it will. If it does, then that may nicely solve my problem. I could put my functions within the loop. I suppose I'd have to maintain the variables outside of the function though so they don't get overwritten on every new trip through the loop().
Turns out it stops the program just like a delay()
Only if you are (locally) looping to wait for the time to expire in the loop, which is what you are doing. Then you may as well use delay().
To state the obvious, your code:
while(!exitThis){
if(millis() >= (timeStart + duration)){
digitalWrite(pin, HIGH);
exitThis = true;
} // end if
} // end while()
needs to change to this:if(millis() >= (timeStart + duration)){
digitalWrite(pin, HIGH);
} // end ifThe while statement blocks you so that you cannot do anything else. The whole while with millis() thing is just like delay().
At first I tried the code that you suggested (literally, no joke!) and it did not work the way I wanted. After thinking about it, I thought that since it was outside of loop(), it would not keep testing, it would only test once. Obviously the first test will not pass because enough time has not elapsed. Eventually I made it work with the while() loop, but it's not quite the way I want it to work. The while() loop is now a stopgap measure, and eventually I realized I had recreated delay() (Did I reinvent delay() w/ a while() loop while trying to make a pulse function? - Programming Questions - Arduino Forum) I think there might be a way to implement a poll for that function within loop(), but the function is 2 levels removed from loop(), so I'd have to pass variables through 2 functions, and that doesn't sound all that good in theory. I'll check out that article on state machines before I rewrite all of my code. Any idea why your suggestion might not have worked like we both thought it would?
The structure of your code looks like it could benefit from you getting familiar with Finite State Machines, as your application seems to be based on doing a sequence of things followed by waiting for an external input that changes the current state and what needs to be 'done' before moving to a new state. The easiest way to implement these are with case statements and you should be able to reuse a lot of your existing code. Google the term or search these forums as there is always a lot of discussion on FSM.
Edit:
Here is a link by majenko http://hacking.majenko.co.uk/finite-state-machine
Thanks for pointing me in that direction, I'll definitely check it out. I'm new to AVR/Arduinos; mostly programmed on PCs with a sprinkling of other systems.