I was wrong being too confident about my whole code. When people suggested posting my whole code, I should have just done it!!!
Anyway, here is the mistake I made. I initialized the variable flash_time as an 8-bit integer. Of course, 8 bit can only hold up to 255. I am trying to store 9000!!! The following code has this bug fixed.
const long sleep_interval = 3000;
const long awake_interval = 250;
const int ledPin = 3;
const uint16_t flash_time = 9000; //bug fixed by using uint16_t instead of uint8_t
const uint16_t t1 = 5;
const uint16_t t2 = 495;
void flash() {
unsigned long x = millis();
while(millis() - x < flash_time){
digitalWrite(ledPin, HIGH);
delay(t1);
digitalWrite(ledPin, LOW);
delay(t2);
}
}
void setup() {
Serial.begin(57600);
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long x = millis();
while(millis() - x < awake_interval){
Serial.println("flash time!");
flash();
}
unsigned long y = millis();;
while (millis() - y < sleep_interval) { //sleep_interval = 3 secs
Serial.println("Sleep time!");
}
}
So, nested loops that use millis() to determine the terminating conditions work just fine, even if loop 1 runs for 250 ms and loop 2 (that's inside loop 1) runs for much longer, like 9 seconds.
Apologies if I have wasted too much of people's time.