You will have to check were it crashes.

One thing that is unrelated to crashing but looks very wrong is the second loop.
If the signal stays at LOW it will never break.

Also be aware that this will block your UI as long as the signal period is.

Cheers,
_