Looking for some help in stopping this motorized potentiometer midway

sorry if my code did not gave you any hint of its working but i think maybe there is another part i forget to put in maybe it need a do while statement the code so that i could work properly, btw you cant use 0 as your first case since that will never be true.. urm let me test with my set up b4 i get back to you