Please follow the advice given in reply #2 when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
There is likely nothing wrong with your software. Please post a wiring diagram of your hardware connections.