Noob questions, running multiple servos

Starting off with nothing and looking at getting an Leonardo and a servo shield to accomplish the following.

I would like to run 10-20 servos independently activated by a momentary switch.

Would that combination of hardware be able to do what I wish to do or is there a better way to do this?

The reason I would like to control the servos with momentary switches is that I would like to activate a servo from multiple locations.

What I need the servo to do is sweep from -90 degree's to + 90 degree's upon a momentary button push. After the servo has moved I need it to stay there until the next time the button is pushed.

I have a very very basic understanding of programming, but have not been able to find any sketches that use a momentary switch, they are either controlled by a pot or a continuous switch.

Any pointers would be greatly appreciated.

Thanks in Advance

John

You will need a hefty 5-6 volt regulated power supply for the servos. If several servos will be moving loads simultaneously, budget 1 ampere of current per straining servo. Don't even think of running them off of the Arduino power supply.

I don't know if I actually tried the below with a servo, but you can try it and see if it works.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo myservo;
int pos = 0;    // variable to store the servo position 

void setup()
{
  pinMode(button1, INPUT);
  myservo.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
    {                                  // in steps of 1 degree 
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    } 
    for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
    {                                
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    } 
  }
}

J, Totally understand about having to power the servos with an outside source. Have a couple different power supplies available for that purpose,

Zoom, I most likely totally wrong on this, but the way I am reading the code, it is going from 0-180 on button push, but then going back to 0 after a delay not another button push. Just learning so I may have missed something, but will give it a try when I have the hardware going.

Thanks for the thoughts

John

not another button push

Is that a push on a second button, or a toggle function from a single button?

Servos have three pins: Power supply +, Signal and Ground.
Basically, you supply pulses to the signal pin and the Width of the pulse determines the servo position. However, you need to continually supply these pulses....

Think they are a couple of options:

  1. Connect all the servos to the one PWM output (via a buffer) and switch the power to the servo you want to move. Move the servo, move the servo then power it down. Trouble is, if there is a force on the servo, it won't maintain the position.

  2. Take a look at this board: https://www.sparkfun.com/products/10615
    This has 16 PWM outputs - one going to the signal of each servo.... which should be enough in conjunction with a hefty power supply (Maybe use a Desktop Computer power supply). Just make sure you link the ground of the power supply to the ground of the Arduino boards.

Have fun.
Andrew.

zoom, Ideally I would want one momentary push button per servo, I would like it to work like this

  1. Power up - servo goes to 0 degrees
  2. upon button push - servo goes to 180 degrees and stays there
  3. upon 2nd push of same button - servo goes to 0 degrees.

Andrew, that looks a lot like the board I was planning to use.

Agree about the big power supply, I could see needing to power 20 servos at some point so that would be two shield boards and a bunch of power.

Think the stumbling block at the moment is the momentary switch, I know it would work with a DPDT switch. So I have to be able to tell when the switch in closed and keep looking for the next button push, and do this for 20 servos at the same time.

Thanks for the thoughts
John

Think what you need is an array of flags that remembers each servo position, then a loop that scans through the buttons looking for button presses....

In Pseudocode terms...

For each button
is button pressed ??
flag = not flag
update servo position
wait for button to be released
end if
next

Hope this helps.
One way you could have all these button is by multiplexing... maybe using like a 4051 IC which sort of work like an 8 pin switch controlled by 3 digital llines

Thanks Andrew,

Will look into it.

John

Basic servo button toggle code.

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

zoom

Thank You,

Getting my Arduino tomorrow and looking forward to giving this a try.

Will report back.

John