I am not seeing how your code gives the behavior you describe. This is likely a hardware issue. Knowing the hardware used and how it is wired will provide valuable insight to anyone looking to help you.
Please see this link for more information on how to best use this forum.