I like to do it this way, using startTime != 0 as the timing flag.
unsigned long startTime = 0;
void setup()
{
Serial.begin(115200);
}
void loop()
{
unsigned long currentMillis = millis();
Serial.print(currentMillis);
Serial.print(" ");
Serial.println(startTime);
if (Serial.available() > 0)
{
char in = Serial.read();
if (in == 'e' && startTime == 0)
// or just "if (in == 'e')" if you want to
// be able to re-start the timer before it ends
{
startTime = currentMillis;
Serial.println("e");
}
} // serial done
if (startTime != 0 && currentMillis - startTime > 5000)
{
Serial.println("e-done");
startTime = 0;
}
} // loop done
Many thanks to both of you.
I somehow managed to forget what the if(Serial.available()>0){...} does to my code here. Just to put it in my own words:
The if(Serial.available()>0){...} is never entered again without any new input from serial. Only when (if Serial.available()>0) constantly remains true, then the "e-done" would be printed after the defined 5s.