Either declare variable ldOn as volatile or Remove optimizations from compilation and check
It should work. Even after using volatile keyword/removing optimization from compilation if code is not working then you have to triple review your code and check whether somewhere else variable is being modified.
Check this link