Control two servo

Hello guys

To control the movement of my robot, I have written the follow code (I have 2 servo 360°).

#include <Servo.h>

Servo ruotadestra;
Servo ruotasinistra;

void setup() {
Serial.begin(9600);
ruotasinistra.attach(9);
ruotadestra.attach(10);

}

void loop() {
int i, j;
i=j=1500;
while (i>500 && j<2500) {
ruotadestra.writeMicroseconds(i);
ruotasinistra.writeMicroseconds(j);
i-=100;
j+=100;

}
}

The problem is that, for each iteration, the board loses the connection. I have understood that it is a error in codes, but I don’t understand what. Can you help me?
Thank you!

The problem is that, for each iteration, the board loses the connection.

Each iteration of what? How can the board loose connection with the servo?

The problem is that, for each iteration, the board loses the connection. I have understood that it is a error in codes, but I don't understand what. Can you help me? Thank you!

Are you powering your servos from the arduino? If so, this usually causes the arduino to crash and reset.

Yes, thank you for the help!!
I’ve read that Arduino should never have dual power. Then how can I feed the servo and at the same time to program them?

Just use a four AA pack for the servos, and use whatever you're using at the moment to power the Arduino (I'll assume it isn't a 9V radio battery) Connect the grounds together.

Basic arduino servo wiring attached.

servo-wire.jpg