I am very new to Arduino, and I am working on a project where I would like to use a button to change the position of a servo. The trouble I am having is I don't know how to make the servo hold the position I stopped pressing the button at. Any help would be greatly appreciated. Here is my code.
Feistlyone,
It looks to me like your current code moves the servo to 180 but when you release it it moves back to zero. Can you confirm that this is what it does now?
But it sounds like you want different behavior. You want the servo to stay where it is, either at zero or 180, unless you push the button. When the button is pressed, the servo should move to the other position and stay there.
Is this what you are looking for?
Reading it again, I think he wants it to start moving on a button press, and stop where it is on the button being released.
To do that you just need when the button is pressed for a variable say "pos" to increment by 1 (or 2 or 3 or whatever step you want) and do a servo.write(pos). If the button is held or pressed repeatedly, pos will take it to the end of the travel. So you look for pos being over say 175, change its sign to -1, -2 or whatever so it starts counting down. Change the sign back at about 5 degrees.
Do that all inside the button press code, so when you let go, the servo stops where it is.
Thank you for the help, but I have been trying to implement your solution and I think there is some part of coding I have not yet learned that I might need to know for this. Could you possibly write me a short example of what you mean, or point me in the direction of an example?