How to execute a function for "t" time without delay + using external interrupt

PeterH:
No, that's NOT how I'd do it.

You want to avoid designing your code so that it stops and waits for something to happen (the command to end monitoring); that's a blocking design which would need you to replicate logic all over the place. The benefit of following the non-blocking approach demonstrated in the 'blink without delay' example is that your sketch can handle multiple actions and events in a consistent way without all your separate bits of code needing to explicitly cooperate or depend on each other; it gives you a nice simple architecture that scales very well.

In loop() add your code to receive commands from the serial port and set a 'monitoring enabled' flag to indicate whether the regular monitoring should be happening based on the commands received. Also in loop() test whether the 'monitoring enabled' flag is set and then whether it's time to collect a sample, and if it is then you collect and transmit the sample. Other pieces of code in loop() could deal with other things you might need to do concurrently such as blinking LEDs or whatever.

Thank you for your reply!This solution seems good, but I haven't understand it very well. Can you explain it to me with an example?