Arduino Mega 8 Servo

Hi, I have an issue with the code and that is, after completing the movements, there is a delay when continuously 'looping'. How can I keep the sketch running and starting over again without a delay. I am using a an Arduino mega, separate power supply and everything grounded together. I am working on a 17 DOF Humanoid and this is just for the legs. Thank you. I am writing on iPad, so I hope the code comes out posted correctly. So I'll say sorry before I inevitably get slated by someone!

#include <Servo.h>
Servo servo1; // Left Foot
Servo servo2; // Left Ankle
Servo servo3; // Left Knee
Servo servo4; // Left Hip
Servo servo5; // Right Foot
Servo servo6; // Right Ankle
Servo servo7; // Right Knee
Servo servo8; // Right Hip
int pos = 0;

void setup() {
servo1.attach(52);
servo2.attach(50);
servo3.attach(48);
servo4.attach(46);
servo5.attach(53);
servo6.attach(51);
servo7.attach(49);
servo8.attach(47);
}

void loop() {
for(pos = 100; pos < 120; pos++)
{
servo1.write(pos); // Left Foot
servo5.write(pos); // Right foot
delay(20);
}
for(pos = 120; pos > 100; pos--)
{
servo1.write(pos); // Left Foot
servo5.write(pos); // Right Foot
delay(20);
}
for(pos = 70; pos < 110; pos++)
{
servo4.write(pos); // Left Hip
servo8.write(pos); // Right Hip
delay(20);
}
for(pos = 110; pos >70; pos--)
{
servo4.write(pos); // Left Hip
servo8.write(pos); // Right Hip
delay(20);
}
}

Does the same thing happen when you have two (or three) lots of the same code in the loop.
Leo..

Very good question Leo, I have tried adding the same code for servos 4 and 8 and they work twice then the timer goes back to slow again. That's what makes me think it's something I need in the sketch to keep it running at the same speed all the time.
Thank you Leo
Also, I have tried different sketches, but I can never get the timing right. I have tried varspeedservo, but the servos go manic when adjusting the code and have broke a hitec hs-5645 digital servo gear in the process.
I've been trying to get my head round servos for a while, and thought that the sketch I'm using is the safest for my servos.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

To help reading your code you can name the servos.

Servo servo1; // Left Foot
Servo servo2; // Left Ankle
Servo servo3; // Left Knee
Servo servo4; // Left Hip
Servo servo5; // Right Foot
Servo servo6; // Right Ankle
Servo servo7; // Right Knee
Servo servo8; // Right Hip
Servo LeftFoot; // Left Foot
Servo LeftAnkle; // Left Ankle
Servo LeftKnee; // Left Knee
Servo LeftHip; // Left Hip
Servo RightFoot; // Right Foot
Servo RightAnkle; // Right Ankle
Servo RightKnee; // Right Knee
Servo RightHip; // Right Hip

That way you can remove the comments and your code will be more informative.

Tom.. :slight_smile:

Hi,
Can I suggest you do not declare pos a global variable

Do this in each for loop;

  for(int pos = 100; pos < 120; pos++)

Just a thought.

Tom... :slight_smile:

@Brett72, it seems to me you have two conceptual errors in your code.

First, don't use delay() to regulate the speed because it blocks the Arduino. Look at how millis() is used to control the speed of a servo in Several Things at a Time

Second, the movements of your servos happen one after the other and I think it would be better if they were interleaved - it would give a more natural movement.

And, while it is more of a style issue, if you use arrays to hold all the servo details you can hugely shorten your code. For example all the servos could be attached with

for (n = 0; n < numServos; n++) {
    servoArray[n].attach(servoPins[n];
}

You could use a similar FOR loop to move all the servos to their next position.

You could also define variables to hold the index numbers for the servos so you could refer to them individually when necessary like, for example, servoArray[knee].write(xxx);

...R

#include <Servo.h>
Servo servo1; // Left Foot
Servo servo2; // Left Ankle
Servo servo3; // Left Knee
Servo servo4; // Left Hip
Servo servo5; // Right Foot
Servo servo6; // Right Ankle
Servo servo7; // Right Knee
Servo servo8; // Right Hip
int pos = 0;

void setup() {
  servo1.attach(52);
  servo2.attach(50);
  servo3.attach(48);
  servo4.attach(46);
  servo5.attach(53);
  servo6.attach(51);
  servo7.attach(49);
  servo8.attach(47);
}

void loop() {
  for(pos = 100; pos < 120; pos++)
  {
  servo1.write(pos); // Left Foot
  servo5.write(pos); // Right foot
  delay(20);
  }
  for(pos = 120; pos > 100; pos--)
  {
  servo1.write(pos); // Left Foot 
  servo5.write(pos); // Right Foot
  delay(20);
  }
  for(pos = 70; pos < 110; pos++)
  { 
  servo4.write(pos); // Left Hip
  servo8.write(pos); // Right Hip
  delay(20);
  }
  for(pos = 110; pos >70; pos--)
  {
  servo4.write(pos); // Left Hip
  servo8.write(pos); // Right Hip
  delay(20);
}
}
This is the same code, I just wanted to see if I could post it correctly - Thanks Tom
Thank you for all your help, it has given me a better insight into servo code.
I will use advice given and give it a go! I will post new code when I have got a bit further on.
Thank you all once again, Brett