Pages: [1]   Go Down
Author Topic: Multiple Servo Control  (Read 1629 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm doing a project where I have to control multiple servos with one arduino. I have this code I used for one servo, but I don't know if it will work for more than one. What do I have to do? Can I just do the code over again and define more buttons? Here is the code. Thanks.
Code:
#include <Servo.h>
Servo myservo; 
int pos = 0;   

const int buttonPinleft = 2;
const int buttonPinright = 5;

int buttonStateleft = 0;
int buttonStateright = 0;

void setup()
{

  pinMode(buttonPinleft, INPUT);
  pinMode(buttonPinright, INPUT);     
  myservo.attach(9);
}
 
 
void loop()
{

  buttonStateleft = digitalRead(buttonPinleft);
  buttonStateright = digitalRead(buttonPinright);

  if (pos<180) pos=180;
  if (pos>0) pos=0;

  if (buttonStateleft == HIGH) {
    pos += 5;
  }
  if (buttonStateright == HIGH) {
    pos -= 5;
  }
 
    myservo.write(pos);
    delay(0);
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm doing a project where I have to control multiple servos with one arduino. I have this code I used for one servo, but I don't know if it will work for more than one. What do I have to do? Can I just do the code over again and define more buttons?

Create as many Servo objects as you need (up to the Arduino UNO limit of 12 or Arduino Mega limit of 48). Use the .attach() method to associate each Servo object with an output pin.

You can have as many buttons as you have spare digital pins.  If you run out of pins there are hardware ways to connect more buttons than pins.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you so much!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do I connect more buttons than pins? I just realized that I will need a lot more pins than available. What do I have to do?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are several choices.  The cheapest is probably the R/2R resistor ladder that changes your button pushes to voltages on an analog input.  Using that method, each of the 6 analog inputs can read at least four buttons (24 buttons total).

http://blog.littlebirdelectronics.com/tutorial-analog-input-for-multiple-buttons-pa
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I didn't understand how to make the R-2R circuit.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I didn't understand how to make the R-2R circuit.

You follow the schematic diagram near the top of that page.  Note the use of 10 kOhm and 20 kOhm resistors: R and 2R.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh. Didn't see that. Sorry. Thank you!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One last thing. Is there any way to connect more servos than pins? For this, I need to connect 14 servos and 15 buttons. The buttons are taken care of, so I was wondering about the servos.
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1848
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

One last thing. Is there any way to connect more servos than pins? For this, I need to connect 14 servos and 15 buttons. The buttons are taken care of, so I was wondering about the servos.
There are various add-on boards that can do this.  For example Pololu has Maestro controllers that can handle 18 and 24 servos.  I'm sure if you go to other robot shops (dfrobot, robotshop, etc.), you will see other brands.

« Last Edit: August 08, 2012, 08:32:38 pm by MichaelMeissner » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One last thing. Is there any way to connect more servos than pins? For this, I need to connect 14 servos and 15 buttons. The buttons are taken care of, so I was wondering about the servos.

Since 14 is more than the 12 the Arduino UNO can handle on its own you have two choices:

Arduino Mega which can handle 48 servos.  It has has 54 digital I/O pins so after the 14 servos you have about 40 pins left for buttons.

External servo drivers, as mentioned above.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, but I'm confused. Let's say Servos 1 and 2 are connected to pins 1 and 2. And Buttons 1 and 2 are connected to Analog pin 1. I want to control Servo 1 with Button 1 and Servo 2 with Button 2. Will the Analog pin be able to tell the difference between the buttons, or will it just read the amount of input it is receiving?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, but I'm confused. Let's say Buttons 1 and 2 are connected to Analog pin 1. Will the Analog pin be able to tell the difference between the buttons, or will it just read the amount of input it is receiving?

The analog input pin will do what analog input pins do:  It will measure the voltage from 0V to 5V and return a number from 0 to 1023 to represent that voltage.  The voltage is controlled by which buttons are pressed.  If no buttons are pressed you should get the number 0.

You can press each combination of buttons and note the value you get.  Then you can use a look-up table to determine the combination of buttons from the value.  It won't be exact so just search the table for the closest match.  Once you have determined the combination of buttons that are pressed you can take that value apart and work on each button individually.

Of course you could switch to a Mega and skip all this analog stuff.  smiley
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I might just get a Mega lol. Thank you.
Logged

Pages: [1]   Go Up
Jump to: