Consecutive servo with if statement not working

hello everyone

trying to make this code work but something is not going on track

what i want is:

when digital 12 is high
1st servo1 start and reach the desired angle and then servo2 start and reach its angle

when digital 12 is low
1st servo2 start and reach the desired angle and then servo1 start and reach its angle

but i am having issue on high, sometime servo 2 starts first and sometime servo1,

where i am doing wrong?

code:

#include <Servo.h>


Servo servo1;
Servo servo2;
int val2 = 0;
int val = 155;


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

void loop(){
if(digitalRead(12)==HIGH)
for(val2 <= 50; val2 <= 50; val2 += 1)
{servo1.write(val2);
delay(20);
}

delay(40)

if(digitalRead(12)==HIGH)
for(val >= 20; val >= 20; val -= 1)
{servo2.write(val);
delay(20);
}


if(digitalRead(12)==LOW)
for(val <= 155; val <= 155; val += 1)
{servo2.write(val);
delay(20);
}


delay(60);

if(digitalRead(12)==LOW)
for(val2 >= 0; val2 >= 0; val2 -= 1)
{servo1.write(val2);
delay(20);
}


}

Oops.

Take a look at the reference page on how to write a for loop

Try this:

#include <Servo.h>


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

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

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

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

    delay(40);

    for (; val2 >= Val2Min; val2 -= 1)
    {
      servo2.write(val2);
      delay(20);
    }
  }
  else // input LOW
  {
    for (; val2 <= Val2Max; val2 += 1)
    {
      servo2.write(val2);
      delay(20);
    }

    delay(60);

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

Thanks a lot, it worked..

Can you please help me to add one more code to this in a correct way…

I want:

When 12 high

Servo 1 start and reach 50 then servo 2 reach its position

Then servo 1 comes to 30

When 12 low

Servo 1 again reach 50 then servo 2 reach its position

Then servo 1 comes to 0

It can be possible?

Thanks a lot btw

well help adding code would be:

you

make a first attempt how you think it might work and then post the modified code with a description what the code does (or that the code does not compile)
combined with a question "what do I have to modify to make it work?

Posting ready to use code is not help adding code.
It is writing the whole code for you.

best regards Stefan

thanks

Here is a version where all of the complex parts have been moved into functions. You just need to call a function to move a specific servo to a specific position. It should be easy to move them in the order you want to the positions you want.

#include <Servo.h>

Servo Servo1;
Servo Servo2;

int Position1 = 90;
int Position2 = 90;

void setup()
{
  pinMode(12, INPUT);
  
  Servo1.write(Position1);
  Servo1.attach(2);
  
  Servo2.write(Position2);
  Servo2.attach(3);
}

void MoveTo(Servo &servo, int &currentPosition, const int desiredPosition)
{
  if (currentPosition <= desiredPosition)
  {
    // Moving in positive direction
    while (currentPosition <= desiredPosition)
    {
      servo.write(currentPosition);
      delay(20);
      currentPosition += 1;
    }
  }
  else
  {
    // Moving in negative direction
    while (currentPosition >= desiredPosition)
    {
      servo.write(currentPosition);
      delay(20);
      currentPosition -= 1;
    }
  }
}

void MoveServo1To(const int desiredPosition)
{
  MoveTo(Servo1, Position1, desiredPosition);
}

void MoveServo2To(const int desiredPosition)
{
  MoveTo(Servo2, Position2, desiredPosition);
}

void loop()
{
  if (digitalRead(12) == HIGH)
  {
    MoveServo1To(50);
    delay(40);
    MoveServo2To(20);
  }
  else // input LOW
  {
    MoveServo2To(150);
    delay(60);
    MoveServo1To(0);
  }
}