plz help me in servo problem

Hello Arduino i have tried this code of controlling servo motor by pot wirelessly by 2 arduinos & 2 Xbees the servos making random moves without any control with the pots , so what's the error in these codes :

Pots Code @ the First Arduino :

//Define Pins
int potPin = 0;
int potPin2 = 1;

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

void loop()
{
  int val = map(analogRead(potPin), 0, 1023, 0, 179);
  int val2 = map(analogRead(potPin2), 0, 1023, 0, 179);
  Serial.println(val);
  Serial.println(val2);
  delay(50);
 
}

__________________________________________________________________

Motor Code @ the sec. Arduino :

#include <Servo.h>

//Define Pins
int servoPin = 9;
int servoPin2 = 10;

//Create Servo Object
Servo Servo1;
Servo Servo2;

void setup()
{
 //Start Serial
 Serial.begin(9600);
 
  //Attaches the Servo to our object
  Servo1.attach(servoPin);
  Servo2.attach(servoPin2);
 
  delay(500);
}

void loop()
{ 

  while( Serial.available() == 0);
  int data = Serial.read() -'0';

  int pos = map(data, 0, 1023, 0, 179);
  int pos2 = map(data, 0, 1023, 0, 179);
  pos = constrain(pos, 0, 179);
  pos2 = constrain(pos, 0, 179);

  //Turn the servo
  Serial.println(pos);
   Serial.println(pos2);
  Servo1.write(pos);
  Servo2.write(pos2);
  Serial.flush(); 
 
 
}

sincerely - amr

Your receiver takes a single decimal digit (when the transmitter is sending multiple digits) and maps each to a different range, and writes that to the servos. A millisecond or so later, a new digit arrives, you map that to the same range and send that to the servos.
What did you expect to happen?

  int data = Serial.read() -'0';

  int pos = map(data, 0, 1023, 0, 179);
  int pos2 = map(data, 0, 1023, 0, 179);
  pos = constrain(pos, 0, 179);
  pos2 = constrain(pos, 0, 179);

  //Turn the servo
  Serial.println(pos);
   Serial.println(pos2);
  Servo1.write(pos);
  Servo2.write(pos2);
  Serial.flush();

You are sending two strings of characters, representing each servo position, separated by a carriage return and line feed. Then, you read a single character, pretend it is two values (when in reality data is an int in the range 0 to 9), and throw away the rest of the data on the serial port (pre-1.0) or block until all data has been sent (1.0 and on).

Regardless of which version of the IDE you are using, you do NOT need the serial flush statement. Get rid of it. You do need to read ALL the data sent, properly.

ok , how can i change the single decimal digit to 4 digits what about this modification ( int data = Serial.read() -'0000'; )

regards - amr

Converting strings to decimal number comes up on this forum about once a week.
There are examples.

what about this modification ( int data = Serial.read() -'0000';

That will not work for more reasons than I have time to go into now.

thanks for reply but can u give me the link please , & what about this : int data = Serial.read() -'4';

best

Simple servo test code for two servos that converts a string into two numbers for servo position control.

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// 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
// use serial monitor to test

#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("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill 
    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 parsed results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
            
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
  } 
}

@ zoomkat i can't understand , sorry but can u modify my code to understand what u need

regards

 myservo2.attach(7);

I thought that the servo library only used outputs capable of PWM. Can you use any output?

I thought that the servo library only used outputs capable of PWM. Can you use any output?

Seemed to work for me. It doesn't work for you?

@ zoomkat i can't understand , sorry but can u modify my code to understand what u need

Sorry, no. I posted the code to show one way to capture a string from the serial port, parse the captured string into two seperate numeric strings (one for each servo), and convert the numeric string into a number to use to position a servo. You need to see if any of this is useful to you.

Seemed to work for me. It doesn't work for you?

Never tried it... made a rash assumption that because the sketch examples use pin 9 & 10 that the servo library only used the PWM outputs to control the servos... DUH!

Never tried it... made a rash assumption that because the sketch examples use pin 9 & 10 that the servo library only used the PWM outputs to control the servos... DUH!

The pins available for servo use can also depend on what is going on and possible conflicts with other hardware being used.

plz any one help me to correct my code , i am very newbie so , hardly i have written this code

thanks

Zoomkat has given you code.

plz any one help me to correct my code , i am very newbie so , hardly i have written this code

Have you gotten the below example code to work with your servo?

// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
}

there are no members in this forum can understand the meaning of word help , no one understand the meaning of newbie member like me , except the member sirbow when i asked him to help he offers to me alot of codes & we tried to modify it together & finally i reached my target

Special thanks to Sirbow2 , really i am very glad to know someone like u

thanks for all members

there are no members in this forum can understand the meaning of word help

Sure there are. But, your definition of help (write my code for me) and mine (assist you in learning to write code) are completely different.

no one understand the meaning of newbie member like me

A newbie ceases to be a newbie by learning to write code. You don't seem to want to loose the newbie tag.