Hi,
I am trying to control 5 servo motors to specific angles given by a PC via Xbee . A code (encoded servo number and angle ) will be sent from the PC via an Xbee transmitter and will be received using an Xbee receiver. The arduino should convert the received code to angles & servoes and perform the operation. I want the assistance with the code. Any help is welcomed...
![]()
Hey,
Um not so sure about the code but this shield would be perfect for your project. It supports up to 8 servo ports, which is more than enough for you, and has a space to directly connect an xbee receiver to the shield.
hope this helps a little.
For only five servos, why bother with a shield?
I have got the hardware, I am using the PWM pins of the ARDUINO itself and no other external boards. But i need help in coding the program as I had explained above. I want to serially control multiple servoes, thats all...
Don't use PWM to drive R/C servos.
Use the servo library.
But i need help in coding the program as I had explained above.
Well, show us what you've got so far, and then we should be able to help you.
Yes , I am using servo library and the PWM pins. Please someone help me on how should I encode the servonumber and angle to send from PC and the code to decode it in the arduino , and rotate the corresponding servos to received angles ???
AWOL:
Well, show us what you've got so far, and then we should be able to help you.
// Import the Arduino Servo library
#include <Servo.h>
// Create a Servo object array for all the 5 servos
Servo servo[6];
// Common servo setup values
int minPulse = 600; // minimum servo position, us (microseconds)
int maxPulse = 2400; // maximum servo position, us
int userInput[4]; // User input for servo and position , raw input from serial buffer, 3 bytes
int startbyte=0,start=0,hault=0,endbyte=0; // start byte (begin reading input),end byte (end reading input),start( started getting data ), hault ( data reception ended )
int num=0,pos=0,i; // num = servo number, pos = angle to take , i for itration
void setup()
{
// Attach each of the 6 Servo object to a digital PWM pin
servo[0].attach(3, minPulse, maxPulse);
servo[1].attach(5, minPulse, maxPulse);
servo[2].attach(6, minPulse, maxPulse);
servo[3].attach(9, minPulse, maxPulse);
servo[4].attach(10, minPulse, maxPulse);
servo[5].attach(11, minPulse, maxPulse);
// Open the serial connection, 9600 baud
Serial.begin(9600);
}
void loop()
{
// Wait for serial input (min 6 bytes in buffer)
if (Serial.available() >= 6)
{
startbyte = Serial.read(); // receive the first byte
if (startbyte == '<') // check if it is '<' or not
{
start=1; // if the start bit is correct the code reception starts
for (i=0;i<4;i++)
{
userInput[i] = Serial.read()-'0'; // reads and stores the 4 values received as numbers in an array
}
}
endbyte= Serial.read(); // receive the 6th bit
if(endbyte=='>') // if it is the ending byte then
{
hault=1; // data reception ended
}
if ( start==1 && hault==1) // if data was successfully received
{
num = userInput[0]; // identifying the servo from the code
pos =(userInput[1]*100)+(userInput[2]*10)+userInput[3]; // calculating angle from the next 3 bits of the received code ( 3 bytes to one value conversion )
Serial.print("Servo No: "); // acknowledging the transmitter with the decoded data , angle and servo
Serial.print(num+1);
Serial.print("Angle : ");
Serial.print(pos);
servo[num].write(pos); // rotate the corresponding dervo to the received angle
delay(100); // wait till servo reaches that position
hault=0,start=0;
}
}
}
the data sent from the pc will be of the format
<(servonumber)(servo angle in 3 digits)>
The sketch looks to be OK, what's the problem?
Below is some code I use to test servos I have that might be of interest.
// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1;Â // create servo object to control a servo
Servo myservo2;
void setup() {
 Serial.begin(9600);
 myservo1.attach(6); //the pin for the servo control
 myservo2.attach(7);
 Serial.println("servo-test-21"); // so I can keep track of what is loaded
}
void loop() {
 while (Serial.available()) {
  delay(1);Â
  if (Serial.available() >0) {
   char c = Serial.read(); //gets one byte from serial buffer
   readString += c; //makes the string readString
  }
 }
 if (readString.length() >0) {
   Serial.println(readString); //see what was received
  Â
   // expect a string like 07002100 containing the two servo positions  Â
   servo1 = readString.substring(0, 4); //get the first four characters
   servo2 = readString.substring(4, 8); //get the next four characters
  Â
   Serial.println(servo1); //print to serial monitor to see results
   Serial.println(servo2);
  Â
   int n1; //declare as numberÂ
   int n2;
  Â
   char carray1[6]; //magic needed to convert string to a number
   servo1.toCharArray(carray1, sizeof(carray1));
   n1 = atoi(carray1);
  Â
   char carray2[6];
   servo2.toCharArray(carray2, sizeof(carray2));
   n2 = atoi(carray2);
  Â
   myservo1.writeMicroseconds(n1); //set servo position
   myservo2.writeMicroseconds(n2);
  readString="";
 }
}