Button controlled alternating servos in void loop overlap.

Hi I'm new and my project using push buttons and servos has got me confused. I want to use one button to start the alternating movement of a servo situated in the centre of my device that hits buttons either side that also start servos. I have got this to work, only the last servo movement and the first servo movement run at the same time in the void loop, unless I release the main button and press it again. Please can anyone help me stop the overlap?

[void loop(){

buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
buttonState3 = digitalRead(button3);

If(buttonState1 == LOW) {
myservo1.write(180);
for(pos = 0;pos<180; pos+=1)
{
myservo1.write(pos);
delay(3);
}
for(180; pos>1; pos-=1)
{
myservo1.write(pos);
delay(3);
}
If(buttonState3 == LOW) {
myservo3.write(180);
for(pos = 0;pos<180; pos+=1)
{
myservo3.write(pos);
delay(3);
}
If(buttonState2 == LOW) {
myservo2.write(180);
for(pos = 0;pos<180; pos+=1)
{
myservo2.write(pos);
delay(3);
}
for(180; pos>1; pos-=1)
{
myservo2.write(pos);
delay(3);
}
if(buttonState3 == LOW) {
myservo3.write(0);
for(180; pos>1; pos-=1)
{
myservo3.write(pos);
delay(3);
}
}
}
}
}]

for(180; pos>1; pos-=1)

Should be

for(pos = 180; pos>1; pos-=1)

Also read the how to use this forum sticky post it tells you how to post code correctly. We need to see all the code not just the loop function. Please do not call this a void loop function as void is not part of its name, it just means it does not return a value.

unless I release the main button and press it again.

Yes that is the way you have written it.
You want to do the initial movement only when that button1 becomes pressed and not is pressed. Keep track of the last value you read and only do the action when this time you read HIGH and last time you read LOW.

See the state change example in the examples section of the IDE.

Grumpy_Mike:

for(180; pos>1; pos-=1)

Should be

for(pos = 180; pos>1; pos-=1)

I propose:

for (byte pos = 180; pos > 1; pos--)

Where the key change is the type declaration. I don't see a declaration for pos anywhere. It's of course possible it's declared as global, which is bad practice, but OP failed to post the rest of their sketch so we don't know.

Hi,

Thank you both for your prompt responses. Grumpy_Mike your observation of the lack of the word pos and the = symbol have now been included and to my delight has solved my problem. In all honesty I don't understand much of how coding works it isn't a subject I wish to study but for my device was clearly needed. I understand now that my code was posted wrong although I read somewhere how to do this I obviously still got it wrong.

Again thank you!