How to properly Controlling two servo's, and Repeat specif part of code

Hello guys,

I'm new on this forum, signed up because I'm bumping into all sort of problems.
If you're taking the time to read this, in advance thank very much! And sorry for bad english

My current project is controlling a laser, not only turning it on and off, but also controlling the angle of the beam with servo's. One servo controls the movement in the x-axis, moving at an angle of 90 degree, the other servo controls the movement in the y-axis, with an angle of 10 degrees.

I'm able te control both servo's by writting for both an if-statement, problem with this is, the servo's move one after the other. to solve this problem I wrote every step(movement) of both servo's separated. This methode allowed me to control the servo's one after an other.
for example: x-axis turns 15 degrees, next step y-axis turns 10 degrees(this one goes back and forth)

vertical.write(0);
delay(duurvertical);
horizontal.write(0);

vertical.write(10);
delay(duurvertical);
horizontal.write(10);

vertical.write(20);
delay(duurvertical);
horizontal.write(0);

problem with this is, it isn't fluent and the code is very long.
Is there an solution to make the movement seem more fleunt and the movement of both servo's move at the same time/through eachother?

once that part of the code is written, I would like te repeat that specific part it a few times(or for a given time), then delay it for a while. after those steps the whole thing starts over (loping)?
current methode:

if(myCounter<2) {
code with the laser and movement
myCounter = myCounter + 1;
}else{
delay(3000);

But it doesn't seem to loop, it just stops after the given number of repeatings

Is there an solution to make the movement seem more fleunt

Suppose that both servos are at zero and you want to move x to 45 degrees and y to 5 degrees, How fast do you want the servos to move and is it important that they arrive at their targets at the same time ?

If speed is not a problem then write the target values to both servos with no delays

If you want the servos to move slower and arrive at their targets at about the same time you need to break down their movements into steps, the same number for each servo. Suppose you decide on 10 steps, then each x step will be 4.5 and each y step, 0.5. Ignore the fact that the steps are not integers for now.

You could then do

xPos = 0;
yPos = 0;
for (int step = 0; step < 10; step++)
{
  xPos += 4.5
  yPos += 0.5;
  servoX.write(xPos);
  servoY.write(yPos);
  delay(100);
}

BUT there is a problem because the write() function expects to receive an integer. The code will work but the steps will be larger than expected and there will be half as many as expected.

So what to do ?
Well, if you use writeMicroseconds() instead of write you can move in smaller steps which will help considerably.

As to your problem with code not repeating, please post the whole of it and use code tags when you do to make it easier to copy/paste to an editor.

Thank you for your reply!

current code is:

#include <Servo.h>
Servo servo1;
Servo servo2;

float x = 15; //this value prevents the servo from buzzing, by a value of zero it hits the stopping gear 
float y = 0;
int val = 25; 

int relayPin = 12;

void setup() {
   pinMode(relaisPin, OUTPUT);
   servo1.attach(4);
   servo2.attach(7);
}

void loop() {
digitalWrite(relayPin,LOW);
for (int step = 0; step < 20; step++)
{
  x += 5.5;
  y += 0.5;
  
  servo1.write(x);
  delay(val);
  servo2.write(y);
  delay(val);
}
for (int step = 0; step < 20; step++)
{
  x -= 5.5;
  y -= 0.5;

  servo1.write(x);
   delay(val);
  servo2.write(y);
   delay(val);
}
digitalWrite(relayPin,HIGH);
delay(2000);
}

this code let the servo's move back forth, now i would like to te repeat the part a few times.
Then delay for a couple of seconds, that schould loop till infinity.