Programing multiple servos

Hi,
I’m compiting in the robocup junior dance competition, as part of this I have a robot with 14 mg946r servos, they are all have powersupply and their signal pin connected to the digital ports on the arduino mega.

Each servo is currenty program by sweep:

int pos = 0;

void setup() {
myservo.attach(9);
}

void loop() {
for (pos = 30; pos <= 180; pos += 3) {
myservo.write(pos);
delay(15);
}

}

for example, and that for 1 servo.
But we faces a problem, how can we run multiple servos simultaniosly?
we need them to preform completly different actions, somthing like this:
servo 1 130->150 in steps of 2 degress
servo 2 140->110 in steps of 5 degress
and so on and forth.

furter more, if I call each time that multple servos run simoluentsly a “scene” we have somthing like 50 scenes, so my question for you is, can you help me write an example for multiple servos (like 8 servos together) to run at the same time, doing deffrent angels?
I read a lot about this over this fourrum and the internet, but I haven’t found a soultion to feet our needs.
p.s I’m pretty noob in arduino

Thanks! :slight_smile:

Read Overview | Multi-tasking the Arduino - Part 1 | Adafruit Learning System

Rather than use

void loop() {
  for (pos = 30; pos <= 180; pos += 3) {
     myservo.write(pos);             
    delay(15);               
  }

which moves the servo through its full range in one go

have a look at how the servo moves a little piece in each iteration of loop() in the demo Several Things at a Time. You can easily add more servos that way and have them all appear to move at the same time.

Also, don’t use delay() anywhere in your program.

…R

... also check this thread.

ieee488:
Read Overview | Multi-tasking the Arduino - Part 1 | Adafruit Learning System

Hi, I read all of it but I didn’t quite sure it’s fit for my needs, or I just didn’t understand how to use different servos with completly differnt movements in this example of code.

Robin2:
Rather than use

void loop() {

for (pos = 30; pos <= 180; pos += 3) {
    myservo.write(pos);           
    delay(15);             
  }



which moves the servo through its full range in one go

have a look at how the servo moves a little piece in each iteration of `loop()` in the demo [Several Things at a Time](http://forum.arduino.cc/index.php?topic=223286.0). You can easily add more servos that way and have them all appear to move at the same time.

Also, don't use delay() anywhere in your program.

...R

Thank you very much for you answer, I’ve read your post before but I didn’t know how can I use it in my program.

zakim:
… also check this thread.

http://forum.arduino.cc/index.php?topic=384432.0

in summerize, I read all of your suggestion, thanks for your help but I didn’t know how to use those example for my needs. I would be very happy if you could assist me to write a code to control 3 servo (with option to add more in easy way) in the same time preforming different actions.
I really want to learn how to do it on my own, but I just can’t understand how to do it, because I need my code to be very easy to change, and all of my code is a set of different movements.

You haven't answered the main question. Is there any reason not to just bang a given servo to a new position?

iii2:
Thank you very much for you answer, I've read your post before but I didn't know how can I use it in my program.

That does not give me any information that I can use to help you.

Why not take the code in Several Things at a Time as a starting point and try to modify it to do what you want. Then you can explain what parts are causing you a problem.

...R

PaulS:
You haven't answered the main question. Is there any reason not to just bang a given servo to a new position?

I can just give the servo a new position without any "sweep" , but than I can't control the speed of the servo, and the movement iteself is much less "clean", and I need my robot to move as close to human.

Robin2:
That does not give me any information that I can use to help you.

Why not take the code in Several Things at a Time as a starting point and try to modify it to do what you want. Then you can explain what parts are causing you a problem.

...R

I will try it and update, thanks

Discussion concerning slower servo movement. You may just want to get a servo controller that the arduino will control. The servo controllers usually have provisions for controlling servo movement speed.