How do I get the sketch to jump to loop() when it wakes from sleep mode.
As it is now it returns to where it was when it was put to sleep.
As it is now it returns to where it was when it was put to sleep.
That's what it's supposed to do. Why do you want to change this behavior?
How about
void loop () {
if (Iwanttosleep) {
Sleep();
}
else {
otherStuff();
}
}
or
void loop() {
dostuff();
if (Iwanttosleep) {
sleep();
return; //Returns from loop function and goes back to beginning
}
doOtherStuff();
}
PaulS:
As it is now it returns to where it was when it was put to sleep.
That's what it's supposed to do. Why do you want to change this behavior?
Because when I put it to sleep it will be asleep for weeks or months. In that time parameters would have changed. There is no point of the sketch running based on old data. When I want it to wake up I want it to run from the start of the sketch as if power has just been applied to it and reread its inputs.
Wiz; Thanks.
But what about a command at the end of the sleep function that sends it to the start of loop?
void sleep_now() {
dostuff();
goto loop():
}
What about restructuring your code so it does that automagically?
void loop() {
dostuff();
if (Iwanttosleep)
sleep();
}
That goes back to the start of loop after sleeping. You know it is going to sleep, so what's the big problem with sleeping in a place that suits you?
Packhorse:
goto loop():
Using goto is generally considered poor programming practice because it leads to ambiguous program flow.
Packhorse:
Because when I put it to sleep it will be asleep for weeks or months. In that time parameters would have changed. There is no point of the sketch running based on old data. When I want it to wake up I want it to run from the start of the sketch as if power has just been applied to it and reread its inputs.
There should be several ways to accomplish what you're trying to do. Nick's tip on structuring when sleep is called is one valid method. Or, how about writing a function that refreshes the variables you need and calling it immediately after sleep?
If you really needed to start things over, you could do something like this:
void loop() {
while (1) {
doStuff();
if (sleepyTime) {
sleepNow();
break;
}
}
}
Packhorse:
Because when I put it to sleep it will be asleep for weeks or months. In that time parameters would have changed. There is no point of the sketch running based on old data. When I want it to wake up I want it to run from the start of the sketch as if power has just been applied to it and reread its inputs.
If you wanted to, you could turn it off completely with something like this
There, you just hook your interrupt up to where it suggests putting a switch and trigger the OFF pin instead of sleeping.
Packhorse:
How do I get the sketch to jump to loop() when it wakes from sleep mode.
...
Packhorse:
Because when I put it to sleep it will be asleep for weeks or months. In that time parameters would have changed. There is no point of the sketch running based on old data. When I want it to wake up I want it to run from the start of the sketch as if power has just been applied to it and reread its inputs.
I just want to point out that when the power is applied it does some initial setup, then it executes "setup" and then "loop". So jumping to the start of loop isn't exactly "as if power has just been applied to it".
Just out of curiosity, what will wake it from sleep mode after "weeks or months"?
If there is a button, why not just connect it to reset? Then it will go back and do everything from scratch.
For example, the TV-B-Gone does exactly that:
http://www.ladyada.net/make/tvbgone/index.html
It does its stuff and goes to sleep. The only way you wake it is to hit the button which is wired to reset. Then it does its stuff again.
A interrupt via a button. But the button does other things too. So I cant hook it up to RESET. I also dont want to add other buttons.
Using goto is generally considered poor programming practice because it leads to ambiguous program flow.
While that may be true it can simplify things for us poor programmers out there.
Packhorse:
While that may be true it can simplify things for us poor programmers out there.
Without wishing to start yet another "goto flame war", the use of goto, whilst needed/useful in some highly specialized situations, is simply not required in the majority of "domestic" applications.
As suggested further up this thread, simply doing a "return" from loop would mean that immediately afterwards, loop is restarted, which appears to be your requirement. Thus, goto is not required.
Thus, goto is not required.
Not to mention that you can goto a function, anyway.