Wireless Servo motor control via XBee

:slight_smile: hey i modified the code in it now works just fine ..thnks . :slight_smile: now we have expand the code to control 4 servo motors independently help me out.. thanks for responding.cheers

hey i modified the code

That's supposed to be followed by "and now it looks like:" (where you post your code...).

//here is the code for transmitter :

int potPin = 0;

void setup()
{
  //Create Serial Object (9600 Baud)
  Serial.begin(9600);
}

void loop()
{
  int val = map(analogRead(potPin), 0, 1023, 0, 180);
  Serial.write(val);
  delay(50);
  
}

//**************************************
//code for the receiver:

//Include Servo Library
#include <Servo.h>

//Define Pins
int servoPin = 9;

//Create Servo Object
Servo servo;

void setup()
{
 //Start Serial
 Serial.begin(9600);
  
  //Attaches the Servo to our object
  servo.attach(servoPin);
  
}

void loop()

{
while(Serial.available() == 0);

int data = Serial.read();
int pos = data;
pos = constrain(pos, 0, 180);

servo.write(pos);


}

int val = map(analogRead(potPin), 0, 1023, 0, 9);
Serial.write(val);

Why are you mapping to 0 to 9? The servo is capable of moving more than 10 degrees, isn't it?

Where is the data for the other servos to come from? Are you planning to add more potentiometers?

i just forgot to change while writing the comment ...it was actually from 0 to 180 ..sorry my mistake .. :slight_smile:
now how can we send multiple data over serial to control upto 4 servo independently ???..

ashh:
i just forgot to change while writing the comment ...it was actually from 0 to 180 ..sorry my mistake .. :slight_smile:
now how can we send multiple data over serial to control upto 4 servo independently ???..

#7 below will help you with your code. Bottom is some multi servo code.

http://arduino.cc/forum/index.php/topic,148850.0.html

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

hi friend .thanks for posting your code but does not work properly as expected..the servo freaks out and does not know what it's doing!..am posting the code for transmitter please check if it has any issues ..thanks for respond .

//Define Pins
int potPin = 0;
int potPin1 = 1;
int potPin2 = 2;
int potPin3 = 3;

void setup()
{
  //Create Serial Object (9600 Baud)
  Serial.begin(9600);
}

void loop()
{
  int val = map(analogRead(potPin), 0, 1023, 0, 180);
  int val1 = map(analogRead(potPin1), 0, 1023, 0,180);
  int val2 = map(analogRead(potPin2), 0, 1023, 0,180);
  int val3 = map(analogRead(potPin3), 0, 1023, 0,180);
  
  Serial.print(val);
  Serial.print("a ,");
  Serial.print(val1);
  Serial.print("b ,");
  Serial.print(val2);
  Serial.print("c ,");
  Serial.print(val3);
  Serial.print("d ,");
  delay(50);
  
}

Please read #7 below and learn how to properly post code using the # in the tool bar. You can go back and edit your previous post.

http://arduino.cc/forum/index.php/topic,148850.0.html

Interesting note: The single servo code looks almost exactly like that posted by Jeremy Blum in 2011. Tutorial 9 for Arduino: Wireless Communication – JeremyBlum.com

If you took his and cut-n-pasted it would be the right thing to note that used his directions to get your single servo to work. Even using the same components.