Mystery of escaping a while loop?

What would you like/expect to happen?
Maybe a cause of your problem: variables declared within a function will be lost at the end of the function.
If you want them to persist, you should declare them static or make them global.

Also there is a statement pulse_complete= true; in your function body...