Servos stop and not move after running once in for loop

hi , im using 4 servos to make a walking robot, the issue is only the first servo is running , currently i have written code only for 2 servos to check the motion of the legs movement, but the second second servo is not at all moving and the first servo is moving once and stopping, im using arduino uno, with 5v power adapter

here is the code

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;


void setup() {
  // put your setup code here, to run once:
servo1.attach(3);
servo2.attach(4);
servo3.attach(5);
servo4.attach(6);
servo1.write(90);
servo2.write(90);
servo3.write(90);
servo4.write(90);
}

void loop() {
  // put your main code here, to run repeatedly:
for (int i= 120;i>=70;i-=0.2){
  servo1.write(i);
  delay(100);
  if(i==70){
    break;
  }
}
delay(10);
for (int i= 70;i<=120;i+=0.2){
  servo2.write(i);
  delay(100);
  if(i==120){
    break;
  }
}
delay(10);
}

Welcome to the forum

A general comment :

    for (int i = 120; i >= 70; i -= 0.2)

What is the point of subtracting 0.2 from an integer variable ? What are you hoping to achieve, especially as write() takes an integer value ?

i can keep the value as 1, but the issue is that the servos move once and stops

That is exactly your problem. View the serial monitor of this program.

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;


void setup() {
  // put your setup code here, to run once:
servo1.attach(3);
servo2.attach(4);
servo3.attach(5);
servo4.attach(6);
servo1.write(90);
servo2.write(90);
servo3.write(90);
servo4.write(90);

Serial.begin ( 115200 );
}

void loop() {
  // put your main code here, to run repeatedly:
for (int i= 120;i>=70;i-=0.2){
  servo1.write(i);
  Serial.print ( "cycle 1 " );
  Serial.println ( i );
  delay(100);
  if(i==70){
    break;
  }
}
delay(10);
for (int i= 70;i<=120;i+=0.2){
  servo2.write(i);
  Serial.print ( "cycle 2 " );
  Serial.println ( i );
  delay(100);
  if(i==120){
    break;
  }
}
delay(10);
}
cycle 1 120
cycle 1 119
cycle 1 118
cycle 1 117
cycle 1 116
cycle 1 115
cycle 1 114
cycle 1 113
cycle 1 112
cycle 1 111
cycle 1 110
cycle 1 109
cycle 1 108
cycle 1 107
cycle 1 106
cycle 1 105
cycle 1 104
cycle 1 103
cycle 1 102
cycle 1 101
cycle 1 100
cycle 1 99
cycle 1 98
cycle 1 97
cycle 1 96
cycle 1 95
cycle 1 94
cycle 1 93
cycle 1 92
cycle 1 91
cycle 1 90
cycle 1 89
cycle 1 88
cycle 1 87
cycle 1 86
cycle 1 85
cycle 1 84
cycle 1 83
cycle 1 82
cycle 1 81
cycle 1 80
cycle 1 79
cycle 1 78
cycle 1 77
cycle 1 76
cycle 1 75
cycle 1 74
cycle 1 73
cycle 1 72
cycle 1 71
cycle 1 70
cycle 2 70
cycle 2 70
cycle 2 70
cycle 2 70

Can you see that the second loop never ends? Do not make such a salad with different type variables to avoid such problems.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.