Magnetic switch loop

Read through the code and you will see there is nothing you do to stop it.

Put each { and each } on a new line. Then, use Tools + Auto Format to fix your atrocious indenting, and I think the situation will become a lot more obvious. If not, post the code again, and we'll help.

You might also consider whether all your curly braces are needed.