How many servos on an Arduino Uno

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
robothand
 
Posts: 38
Joined: Mon Oct 15, 2012 2:48 pm

Re: How many servos on an Arduino Uno

Post by robothand »

I am going to use 2 xbee series 1 for the communication. When you say I am missing the communication code are you referring to

Code: Select all

Serial.begin(9600); // 
I'll adjust the delays. Thanks for that advice.

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: How many servos on an Arduino Uno

Post by adafruit_support_bill »

To use the XBees, you need to write code on both ends. The glove needs to send the data and you need code on the servo control side to receive that data.

User avatar
robothand
 
Posts: 38
Joined: Mon Oct 15, 2012 2:48 pm

Re: How many servos on an Arduino Uno

Post by robothand »

By Communication part do you mean

Code: Select all

Serial.begin(9600); // 
If not could you expand?

Thanks for the advice on the delay. Will do.

User avatar
robothand
 
Posts: 38
Joined: Mon Oct 15, 2012 2:48 pm

Re: How many servos on an Arduino Uno

Post by robothand »

Sorry don't see your reply above. I do have

Code: Select all

Serial.begin(9600); // 
on both ends.

Are you referring to something else?

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: How many servos on an Arduino Uno

Post by adafruit_support_bill »

Serial.begin() just initializes the serial port. http://arduino.cc/en/Serial/Begin

Too get the glove flex sensor readings to the servos via XBee, you have to write code to send data from the glove: http://arduino.cc/en/Serial/Write
and code to receive data on the other end: http://arduino.cc/en/Serial/Read

User avatar
robothand
 
Posts: 38
Joined: Mon Oct 15, 2012 2:48 pm

Re: How many servos on an Arduino Uno

Post by robothand »

Now do I have the communication piece for the xbee to work?

Glove

Code: Select all

int flexpin=A0;    
int flexpin1=A1;
int flexpin2=A2;
int flexpin3=A3;
int flexpin4=A4;


byte pos=0;   // changed int to byte
byte pos1=0;
byte pos2=0;
byte pos3=0;
byte pos4=0;

int flexsum=0;  
int flexsum1=0;
int flexsum2=0;
int flexsum3=0;
int flexsum4=0;

#define sampleSize 20

//
// Read "sampleSize" samples and report the average
//
int ReadAnalog(int Pin)
{
  long reading = 0;
  analogRead(Pin);    // read the analog in value:
  delay(1);          // waits 1ms for the between each analog pin reads
  for (int i = 0; i < sampleSize; i++)
  {
    reading += analogRead(Pin);
  }
  return reading/sampleSize;
}


void setup()
{

Serial.begin(9600); 
}


void loop()
{
flexsum = ReadAnalog(flexpin);
flexsum1 = ReadAnalog(flexpin1);
flexsum2 = ReadAnalog(flexpin2);
flexsum3 = ReadAnalog(flexpin3);
flexsum4 = ReadAnalog(flexpin4);


{

Serial.print(flexsum);
Serial.print("\t");
Serial.print(flexsum1);
Serial.print("\t");
Serial.print(flexsum2);
Serial.print("\t");
Serial.print(flexsum3);
Serial.print("\t");
Serial.print(flexsum4);
Serial.print("\t");


}
byte pos=map(flexsum,870,800,0,180);  //Added Byte in front of pos
byte pos1=map(flexsum1,870,800,0,180);
byte pos2=map(flexsum2,870,800,0,180);
byte pos3=map(flexsum3,870,800,0,180);
byte pos4=map(flexsum4,870,800,0,180);

delay(50);  //Would I change the delay of the glove here?
}
Did I use the Byte coding correctly above?


This is the hand code

Code: Select all

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

int pos=0;   // variable to store the servo position
int pos1=0;
int pos2=0;
int pos3=0;
int pos4=0;



void setup()
{

Serial.begin(9600); // initialize serial communications at 9600 bps:


myservo.attach(2);    // attaches the servo on pin 2 to the servo object
myservo1.attach(3);
myservo2.attach(4);
myservo3.attach(5);
myservo4.attach(7);

}


void loop()
{
    
if(Serial.available() >=5)  
{

byte servodig = Serial.read();
byte servodig1 = Serial.read();
byte servodig2 = Serial.read();
byte servodig3 = Serial.read();
byte servodig4 = Serial.read();



  
myservo.write(servodig); // tells servo to go to position based on finger position
myservo1.write(servodig1);
myservo2.write(servodig2);
myservo3.write(servodig3);
myservo4.write(servodig4);


Serial.println(pos);
Serial.println(pos1);
Serial.println(pos2);
Serial.println(pos3);
Serial.println(pos4);
}
}
How does the hand code look?

Thanks for your help

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: How many servos on an Arduino Uno

Post by adafruit_support_bill »

Now do I have the communication piece for the xbee to work?
You will have to test yourself to verify if it works, but I can see a few problems in the code still:
1.On the glove side, you are doing Serial.print() of flexsum values plus some tab characters. This may be good for debug output, but it is not what the hand-side is expecting. You need to do a Serial.write() of the mapped "pos" values in order to send the data in binary format.
2. If you want to have debug output as well as communication (a good idea), you need to use different serial ports for the different output. SoftwareSerial will allow you to do that. http://arduino.cc/en/Reference/SoftwareSerial.
3. You have no way for the receiving side to tell which received byte is servodig. You need to send some synchronization bytes to tell the receiver that the next 5 bytes will be servodig - servodig4.

User avatar
robothand
 
Posts: 38
Joined: Mon Oct 15, 2012 2:48 pm

Re: How many servos on an Arduino Uno

Post by robothand »

Recap my first code made use of one arduino to control the glove and hand

Code: Select all

#include <Servo.h>

int flexpin=A0;    // Analog input pin that the flex sensor is attached to
int flexpin1=A1;
int flexpin2=A2;
int flexpin3=A3;
int flexpin4=A4;


int pos=0;   // variable to store the servo position 
int pos1=0;
int pos2=0;
int pos3=0;
int pos4=0;



Servo myservo;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;



int flexsum=0;
int flexsum1=0;
int flexsum2=0;
int flexsum3=0;
int flexsum4=0;

#define sampleSize 20

//
// Read "sampleSize" samples and report the average
//
int ReadAnalog(int Pin)
{
  long reading = 0;
  analogRead(Pin);    // read the analog in value:
  delay(1);          // waits 1ms for the between each analog pin reads 
  for (int i = 0; i < sampleSize; i++)
  {
    reading += analogRead(Pin);
  }
  return reading/sampleSize;
}

void setup()
{
myservo.attach(2);    // attaches the servo on pin 2 to the servo object
myservo1.attach(3);
myservo2.attach(4);
myservo3.attach(5);
myservo4.attach(7);

Serial.begin(9600); // initialize serial communications at 9600 bps:
}

void loop()
{
flexsum = ReadAnalog(flexpin);
flexsum1 = ReadAnalog(flexpin1);
flexsum2 = ReadAnalog(flexpin2);
flexsum3 = ReadAnalog(flexpin3);
flexsum4 = ReadAnalog(flexpin4);

if(Serial.available())
{
Serial.println(flexsum);
Serial.println(flexsum1);
Serial.println(flexsum2);
Serial.println(flexsum3);
Serial.println(flexsum4);

delay(100);
}
pos=map(flexsum,870,800,0,180);
pos1=map(flexsum1,870,800,0,180);
pos2=map(flexsum2,870,800,0,180);
pos3=map(flexsum3,870,800,0,180);
pos4=map(flexsum4,870,800,0,180);

myservo.write(pos);      // tell servo to go to position in variable 'pos
myservo1.write(pos1);
myservo2.write(pos2);
myservo3.write(pos3);
myservo4.write(pos4);

delay(200);   // stop the program for some time
}

my next attempt was to write code for hand and glove using xbee

Glove

Code: Select all

int flexpin=A0;    // Analog input pin that the flex sensor is attached to
int flexpin1=A1;
int flexpin2=A2;
int flexpin3=A3;
int flexpin4=A4;

int pos;   // variable to store the servo position
int pos1;
int pos2;
int pos3;
int pos4;


int flexsum=0;
int flexsum1=0;
int flexsum2=0;
int flexsum3=0;
int flexsum4=0;


#define sampleSize 20

//
// Read "sampleSize" samples and report the average
//
int ReadAnalog(int Pin)
{
  long reading = 0;
  analogRead(Pin);    // read the analog in value:
  delay(1);          // waits 1ms for the between each analog pin reads
  for (int i = 0; i < sampleSize; i++)
  {
    reading += analogRead(Pin);
  }
  return reading/sampleSize;
}

void setup()
{

Serial.begin(9600); // initialize serial communications at 9600 bps:
}

void loop()
{
flexsum = ReadAnalog(flexpin);
flexsum1 = ReadAnalog(flexpin1);
flexsum2 = ReadAnalog(flexpin2);
flexsum3 = ReadAnalog(flexpin3);
flexsum4 = ReadAnalog(flexpin4);


{
pos=map(flexsum,870,800,0,180);
pos1=map(flexsum1,870,800,0,180);
pos2=map(flexsum2,870,800,0,180);
pos3=map(flexsum3,870,800,0,180);
pos4=map(flexsum4,870,800,0,180);

Serial.write(pos);
Serial.write(pos1);
Serial.write(pos2);
Serial.write(pos3);
Serial.write(pos4);

delay(50);
}
}
Hand

Code: Select all

#include <Servo.h>

int pos;   // variable to store the servo position
int pos1;
int pos2;
int pos3;
int pos4;

int data1=0;  
int data2=0;
int data3=0;
int data4=0;
int data5=0;

Servo myservo;  // create servo object to control a servo 
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

void setup()
{
myservo.attach(2);    // attaches the servo on pin 2 to the servo object
myservo1.attach(3);
myservo2.attach(4);
myservo3.attach(5);
myservo4.attach(7);

Serial.begin(9600); // initialize serial communications at 9600 bps:
}

void loop()

{

if(Serial.available() >=5)

data1 = Serial.read();
data2 = Serial.read();
data3 = Serial.read();
data4= Serial.read();
data5 = Serial.read();


pos=map(data1,870,800,0,180);
pos1=map(data2,870,800,0,180);
pos2=map(data3,870,800,0,180);
pos3=map(data4,870,800,0,180);
pos4=map(data5,870,800,0,180);

myservo.write(pos);      // tell servo to go to position in variable 'pos
myservo1.write(pos1);
myservo2.write(pos2);
myservo3.write(pos3);
myservo4.write(pos4);
}
How does the communication coding look?

Thanks

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: How many servos on an Arduino Uno

Post by adafruit_support_bill »

How does the communication coding look?
Have you tested it? What does it do?

From looking at it, I would guess that it probably works sometimes, but the sensors might not always match up to the correct fingers. Why? Because you have no way to tell the glove which sensor reading is from finger #0.

What I would do is add some synchronization bytes to mark the start of each group of 5 readings. Since your sensors are unlikely to return a 0 value (much less 3 zeros in a row), this should work:

Code: Select all

Serial.write(0); // synchronization bytes
Serial.write(0);
Serial.write(0);

Serial.write(pos);
Serial.write(pos1);
Serial.write(pos2);
Serial.write(pos3);
Serial.write(pos4);
Then on the receiving side, instead of waiting for Serial.available() >=5, you look for 3 zeros in a row, then start reading the finger positions.

User avatar
robothand
 
Posts: 38
Joined: Mon Oct 15, 2012 2:48 pm

Re: How many servos on an Arduino Uno

Post by robothand »

Thanks for your advice. My question is how do you notate 3 zeros in a row?

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: How many servos on an Arduino Uno

Post by adafruit_support_bill »

A while loop would be easiest:

Code: Select all

int sync = 0;
while ( sync < 3)
{
   if (Serial.read() == 0)
   {
      sync++;
   }
   else
   {
      sync = 0;
   }
}

User avatar
robothand
 
Posts: 38
Joined: Mon Oct 15, 2012 2:48 pm

Re: How many servos on an Arduino Uno

Post by robothand »

The wireless portion of the project has not worked as yet. When the code was written for it to work wired on one arduino everything worked.

The

Code: Select all

Serial.write(0); // synchronization bytes
Serial.write(0);
Serial.write(0);
resulted in a " call of overloaded 'write(int)' is ambiguous" Here is the full program for the glove

Code: Select all

int flexpin=A0;    // Analog input pin that the flex sensor is attached to
int flexpin1=A1;
int flexpin2=A2;
int flexpin3=A3;
int flexpin4=A4;

int pos;   // variable to store the servo position
int pos1;
int pos2;
int pos3;
int pos4;


int flexsum=0;
int flexsum1=0;
int flexsum2=0;
int flexsum3=0;
int flexsum4=0;


#define sampleSize 20

//
// Read "sampleSize" samples and report the average
//
int ReadAnalog(int Pin)
{
  long reading = 0;
  analogRead(Pin);    // read the analog in value:
  delay(1);          // waits 1ms for the between each analog pin reads
  for (int i = 0; i < sampleSize; i++)
  {
    reading += analogRead(Pin);
  }
  return reading/sampleSize;
}

void setup()
{

Serial.begin(9600); // initialize serial communications at 9600 bps:
}

void loop()
{
flexsum = ReadAnalog(flexpin);
flexsum1 = ReadAnalog(flexpin1);
flexsum2 = ReadAnalog(flexpin2);
flexsum3 = ReadAnalog(flexpin3);
flexsum4 = ReadAnalog(flexpin4);


{
pos=map(flexsum,870,800,0,180);
pos1=map(flexsum1,870,800,0,180);
pos2=map(flexsum2,870,800,0,180);
pos3=map(flexsum3,870,800,0,180);
pos4=map(flexsum4,870,800,0,180);


Serial.write((byte)0);
Serial.write((byte)0);
Serial.write((byte)0);


Serial.write(pos);
Serial.write(pos1);
Serial.write(pos2);
Serial.write(pos3);
Serial.write(pos4);

delay(50);
}
}
and the following for the hand

Code: Select all

#include <Servo.h>

int pos;   // variable to store the servo position
int pos1;
int pos2;
int pos3;
int pos4;

int data1=0;  
int data2=0;
int data3=0;
int data4=0;
int data5=0;

int sync = 0;


Servo myservo;  // create servo object to control a servo 
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

void setup()
{
myservo.attach(2);    // attaches the servo on pin 2 to the servo object
myservo1.attach(3);
myservo2.attach(4);
myservo3.attach(5);
myservo4.attach(7);

Serial.begin(9600); // initialize serial communications at 9600 bps:
}

void loop()

{
while ( sync < 3)
{
   if (Serial.read() == 0)
   {
      sync++;
   }
   else
   {
      sync = 0;
   }
}


data1 = Serial.read();
data2 = Serial.read();
data3 = Serial.read();
data4= Serial.read();
data5 = Serial.read();


pos=map(data1,870,800,0,180);
pos1=map(data2,870,800,0,180);
pos2=map(data3,870,800,0,180);
pos3=map(data4,870,800,0,180);
pos4=map(data5,870,800,0,180);

myservo.write(pos);      // tell servo to go to position in variable 'pos
myservo1.write(pos1);
myservo2.write(pos2);
myservo3.write(pos3);
myservo4.write(pos4);
}
Did I make the right corrections?

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: How many servos on an Arduino Uno

Post by adafruit_support_bill »

Did I make the right corrections?
That looks right. How does it work? Looking at code is no substitute for testing. :wink:

User avatar
robothand
 
Posts: 38
Joined: Mon Oct 15, 2012 2:48 pm

Re: How many servos on an Arduino Uno

Post by robothand »

We could not get the wireless hand and glove project to work so we now using one arduino.

We sewed the flex sensor on the glove and connected it to a custom made pcb. We also made a separate pcb for the servos and connected the servos to it.

The glove is powered by the arduino's 5v and the servos is powered by a separate power 6v power supply. Both pcb's are connected with a common ground.

Problem:
1) The servos for the hand will work for about a minute some times less before all five servos begin to make small jitters and will not respond to the glove control

2) The fingers connected to pin A1 and A3 will not respond but no issues with the other fingers. We have swapped out servos and flex sensors with same results. Any thoughts.

Here is me code:

Code: Select all

#include <Servo.h>

int flexpin=A0;    // Analog input pin that the flex sensor is attached to
int flexpin1=A1;
int flexpin2=A2;
int flexpin3=A3;
int flexpin4=A4;


int pos=0;   // variable to store the servo position 
int pos1=0;
int pos2=0;
int pos3=0;
int pos4=0;



Servo myservo;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;



int flexsum=0;
int flexsum1=0;
int flexsum2=0;
int flexsum3=0;
int flexsum4=0;

#define sampleSize 20

//
// Read "sampleSize" samples and report the average
//
int ReadAnalog(int Pin)
{
  long reading = 0;
  analogRead(Pin);    // read the analog in value:
  delay(1);          // waits 1ms for the between each analog pin reads 
  for (int i = 0; i < sampleSize; i++)
  {
    reading += analogRead(Pin);
  }
  return reading/sampleSize;
}

void setup()
{
myservo.attach(2);    // attaches the servo on pin 2 to the servo object
myservo1.attach(3);
myservo2.attach(4);
myservo3.attach(5);
myservo4.attach(7);

Serial.begin(9600); // initialize serial communications at 9600 bps:
}

void loop()
{
flexsum = ReadAnalog(flexpin);
flexsum1 = ReadAnalog(flexpin1);
flexsum2 = ReadAnalog(flexpin2);
flexsum3 = ReadAnalog(flexpin3);
flexsum4 = ReadAnalog(flexpin4);

if(Serial.available())
{
Serial.println(flexsum);
Serial.println(flexsum1);
Serial.println(flexsum2);
Serial.println(flexsum3);
Serial.println(flexsum4);

delay(100);
}
pos=map(flexsum,870,800,0,180);
pos1=map(flexsum1,870,800,0,180);
pos2=map(flexsum2,870,800,0,180);
pos3=map(flexsum3,870,800,0,180);
pos4=map(flexsum4,870,800,0,180);

myservo.write(pos);      // tell servo to go to position in variable 'pos
myservo1.write(pos1);
myservo2.write(pos2);
myservo3.write(pos3);
myservo4.write(pos4);

delay(200);   // stop the program for some time
}

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: How many servos on an Arduino Uno

Post by adafruit_support_bill »

The servos for the hand will work for about a minute some times less before all five servos begin to make small jitters and will not respond to the glove control
What is the current rating of your servo power supply? Do you measure a drop in voltage associated with jittering behavior?
The fingers connected to pin A1 and A3 will not respond but no issues with the other fingers. We have swapped out servos and flex sensors with same results
What does the serial output say? Are you getting reasonable readings from the flex sensors?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”