2 servos and multiple pos problem

#include <Servo.h>


Servo servo1;
Servo servo2;
const int Val1Min = 0;
const int Val1Max = 70;
int val1 = Val1Min;  // 

const int Val2Min = 15;
const int Val2Max = 150;
int val2 = Val2Max;

const int Val3Min = 40;
const int Val3Max = 70;
int val3 = Val3Max;

void setup()
{
  pinMode(12, INPUT);
  servo1.attach(2);
  servo1.write(val1);
  servo2.attach(3);
  servo2.write(val2);
 
}

void loop()
{
  if (digitalRead(12) == HIGH)
  {
    for (; val1 <= Val1Max; val1 += 1)
    {
      servo1.write(val1);
      delay(30);
    }

    delay(60);

    for (; val2 >= Val2Min; val2 -= 1)
    {
      servo2.write(val2);
      delay(30);
    }

    delay(60);

     for (; val3 >= Val3Min; val3 -= 1)
    {
      servo1.write(val3);
      delay(30);
    }

  }
  else // input LOW

   {

   for (; val3 <= Val3Max; val3 += 1)
    {
      servo1.write(val3);
      delay(30);
    }

    delay(60);
    
    for (; val2 <= Val2Max; val2 += 1)
    {
      servo2.write(val2);
      delay(30);
    }

    delay(60);

    for (; val1 >= Val1Min; val1 -= 1)
    {
      servo1.write(val1);
      delay(30);
    }

    
   
  }
}

i have two servos attached to ardiuno

when D12 is high

servo 1 reach 70° and waits till servo 2 reach 150° then servo 1 returns to 40°

when D12 is low

servo 1 reach 70° and waits till servo 2 reach 15° then servo 1 returns to 0°

everything is working fine with this coding but whenever ardiuno starts (powered up for the first time)

servo 1 makes a small harsh movement (small movement of 10° approx) and then remains stable and work fine

but when ardiuno is powered up again it make same movement for one sec

please help pointing the issue in the coding

When you attach() a servo it moves immediately to its default angle of 90 degrees unless you have previously written a different angle to it

Could this be your problem ?
If so, then write() the required position to the servo before attaching it

Shouldn't this be something like:

    for (val1 = Val1Min; val1 <= Val1Max; val1 += 1)

?

It may not matter in this program, but just fyi: unless you have a 4-wire servo which provides feedback to the program, you don't actually know if a servo ever gets to its commanded destination. All you can say is that the command was given, and you hope the delay() is enough time for it to arrive. But if it jams for any reason, you don't know that....

but its already mentioned in coding, are you asking something different that what i already did?

but i am not talking about servo function or problem while moving, its working fine but only problem is while servo is attached for the first time or ardiuno is powered..

That's why I said....

You need to write() the required initial position to the servo before attaching the servo in order that it starts at that position

Btw it stops doing this work if i remove whole val3 code

But i need servo 1 to return 40°

Doesn’t change anything

Please post your sketch as it is now

Do you mean code without val3?

I mean the code that you referred to when you said

Servo.write() before attaching servo.



#include <Servo.h>


Servo servo1;
Servo servo2;
const int Val1Min = 0;
const int Val1Max = 70;
int val1 = Val1Min;  // 

const int Val2Min = 15;
const int Val2Max = 150;
int val2 = Val2Max;

const int Val3Min = 40;
const int Val3Max = 70;
int val3 = Val3Max;

void setup()
{
  pinMode(12, INPUT);
 servo1.write(val1);
 servo2.write(val2);
 servo1.attach(2);
 servo2.attach(3);

}

void loop()
{
  if (digitalRead(12) == HIGH)
  {
    for (; val1 <= Val1Max; val1 += 1)
    {
      servo1.write(val1);
      delay(30);
    }

    delay(60);

    for (; val2 >= Val2Min; val2 -= 1)
    {
      servo2.write(val2);
      delay(30);
    }

    delay(60);

     for (; val3 >= Val3Min; val3 -= 1)
    {
      servo1.write(val3);
      delay(30);
    }

  }
  else // input LOW

   {

   for (; val3 <= Val3Max; val3 += 1)
    {
      servo1.write(val3);
      delay(30);
    }

    delay(60);
    
    for (; val2 <= Val2Max; val2 += 1)
    {
      servo2.write(val2);
      delay(30);
    }

    delay(60);

    for (; val1 >= Val1Min; val1 -= 1)
    {
      servo1.write(val1);
      delay(30);
    }

    
   
  }
}

Thank you

With the code that you posted, when first powered the sketch should move servo1 to 0 degrees and servo2 to 150 degrees. Do they do that ?

Have you got a pulldown resistor keeping pin 12 LOW when the button is not pressed ?

Why have you not explicitly set initial values for the for loop variables ?

Try printing the values of the angles written to the servos in the for loops. Are they what you expect ?

How are the servos powered ?

With the code that you posted, when first powered the sketch should move servo1 to 0 degrees and servo2 to 150 degrees. Do they do that ? YES

Have you got a pulldown resistor keeping pin 12 LOW when the button is not pressed ? YES

Why have you not explicitly set initial values for the for loop variables ? CAN YOU EXPLAIN HOW TO AS I DIDNT GOT YOU WELL

How are the servos powered ? ESTERNAL POWER SOURCE DIFFERENT FROM ARDIUNO

Your for loops look like this

 for (; val2 >= Val2Min; val2 -= 1)

so they start at the previous value of the loop variable, val2 in this case. I wondered if you had done this deliberately rather than setting the initial value explicitly in the for loop

Have the external power supply and the Arduino got a common GND connection ?

Can you give an example with code i should try?

for (int val2 = val2Max; val2 >= val2Min; val2-=1)
{
      servo2.write(val2);
      delay(30);
}

you want me to try for all or just val2?