10 years, 9 months ago.

While loop interupt

Hi,

Quick question: How do I return from a function after a determined amount of time, like a time out.

I have looked at this: https://mbed.org/handbook/Timeout, but from my understanding it runs another function not return from one.

Is there a way to do this?

Regards, Nick

1 Answer

10 years, 9 months ago.

So you have a while(1) loop, that you want to really stop running, not do something else and continue running it again?

Options:

Timer timey;
timey.start();

while(timey.read() < YOUR_TIME) {
  //Do stuff
}

This runs the while loop until certain amount of time has passed.

You can also do it using interrupts (and then you could also use different things than Timeout):

volatile bool stop = false;    //Dont forget the volatile

void timefunc(void) {
  stop = true;
}

Timeout timeout;

int main() {
  stop = false;
  timeout.attach(timefunc, YOUR_TIME);
  while(!stop) {
    //Your stuff
  }
}

Accepted Answer

This is brilliant and works in the main class but I am trying to use it in writing a library. But I get this error: Error: No instance of overloaded function "mbed::Timeout::attach" matches the argument list in "Xbee/Xbee.cpp", Line: 80, Col: 13 (error number 304), any ideas?

posted by Nick Oliver 01 Mar 2014

Not sure about your code but when you use the timer or ticker attach inside a class you need this method:

Your_timer.attach(this, &Your_class::Your_method_to_attach, YourTime);
posted by Wim Huiskamp 01 Mar 2014

Thats awesome, thanks alot!

posted by Nick Oliver 01 Mar 2014

Just another quick question: Can you see why this doesnt work:

string Xbee::getData(){
    
    timeout.attach(this,&Xbee::timefunc, 2.0);
    char c;
    string msg = "";
    while(_xbee.getc() != '$'){
        if(_stop==true){
            break;
        }
    }
    return msg;
}

It does not exit the for loop but the time out works. _xbee is a serial port. Without the timeout code, it works when it receives the character

posted by Nick Oliver 01 Mar 2014

It looks good to me, so main question would be, is _stop defined as volatile?

posted by Erik - 01 Mar 2014

yes, this is it as defined in the private section in xbee.h:

volatile bool _stop;

the time out function works and i have proved it, but it doesnt exit the loop.

posted by Nick Oliver 01 Mar 2014

Ah I think I know your issue: getc() blocks until there is data. So it is blocked before it gets to the if statement. What should work is:

while (1) {
  if (xbee.readable()) {
    if (xbee.getc() == '$')
       break;
    if (_stop == true) {
       break;
  }
}

Although you have to take into account this code and your own code currently always returns an empty string, nothing fills it.

Your current code should work if you get new characters.

posted by Erik - 01 Mar 2014

Brilliant, I had to move the if statement for the stop outside xbee.readable() if statement and it broke out of the while loop. The reason it returned nothing is i removed part of the code to only show the relevant data, the string returned "Test Working" to show it got there.

Thanks again

posted by Nick Oliver 01 Mar 2014

Ah yes you are completely right, that is supposed to be outside the readable part.

But good to hear :)

posted by Erik - 01 Mar 2014