1. I have done the following experiment which has established that the solutions provided by @kolaha in post #2 and @jremington in post #3 are the valid replacement of goto L1 instruction.
2. In my experiment, the MCU will go into sleep after glowing L (built-in LED of UNO) for 10-sec; however, if Battery voltage (which I have simulated using a switch at DPin-4) goes LOW before the elapse of this 10-sec time, the MCU will also go into sleep.
3. This is the Flow Chart (Fig-1) of the process, which I have not optimized for the shake of clarity.
Figure-1:
4. This is the test sketch which I have also not optimized for the shake of clarity.
#include<avr/sleep.h>
void setup()
{
Serial.begin(9600);
SMCR |= (1 << SM1); //Power-down Mode
pinMode(13, OUTPUT); //LED at DPin-13
pinMode(2, INPUT_PULLUP); //MCU wake up trigger
pinMode(4, INPUT_PULLUP); //Battery status at DPin-4
digitalWrite(13, HIGH); //LED is ON
attachInterrupt(digitalPinToInterrupt(2), wakeUpIsr, LOW);
}
void loop()
{
L1: unsigned long currentMillis = millis();
while (millis() - currentMillis < 10000) //test interval
{
if (digitalRead(4) == LOW)
{
digitalWrite(13, LOW); //LED is Off
sleepMCU(); //MCU sleeps
Serial.println("WakeupBatt."); //MCU is wake up
bitClear(SMCR, SE);
while (digitalRead(4) != HIGH) //checking if Battery volt ok
{
;
}
digitalWrite(13, HIGH); //LED is On
currentMillis = millis();//return;//goto L1; all three codes work
}
}
digitalWrite(13, LOW); //LED is Off
sleepMCU(); //MCU sleeps
Serial.println("Wakeup10sec."); //MCU is wake up
bitClear(SMCR, SE);
while (digitalRead(4) != HIGH) //checking if Battery volt ok
{
;
}
digitalWrite(13, HIGH); //LED is ON
}
void sleepMCU()
{
bitSet(SMCR, SE);
sleep_cpu();
}
void wakeUpIsr()
{
}
5. Serial Monitor Output:
Wakeup10sec.
Wakeup10sec.
WakeupBatt.
Wakeup10sec.
WakeupBatt.
6. Thanks to you all for helping me to convert my ignorance into knowledge.
7. Question:
The return statement is used at the end of a sub-routine for reverting to the main line program (the calling program). In my sketch, there is no sub-routine that has been called upon -- then how does return bring the control at the beginning of the loop() function?
8. Observation:
I have some reservations with the solution of @kolaha in post#2; where, the same instruction (currentMillis = millis()) is being executed twice to arrive at the beginning of the loop() function. If I delete this instruction: currentMillis = millis(), then the timing is not 10-sec (the test interval); rather it is erratic.