Need help in pan and tilt servo with roborealm....

I am able to make it pan and tilt, the "panning" is ok but the problem is in the "tilting"...here the code that i used....is there anything wrong ?? or do i have some setting problem ??

Another question is, why is it that the servo is moving relatively slow compare to what i see in youtube when tracking the object?? is it the type of servo ??

#include <Servo.h>

Servo panservo;
Servo tiltservo;
char incomingData[4] = {0, 0, 0, 0};
int distancePan = 0;
int distanceTilt = 0;
int currentAnglePan = 90;
int currentAngleTilt = 20;
int i = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("Serialport ready");
  panservo.attach(9); // attaches the servo on pin 9 to the servo object
tiltservo.attach (10);   
tiltservo.write(currentAngleTilt);
  panservo.write(currentAnglePan);
} 

void loop(){
  if (Serial.available()){
    while (i < 30){
      incomingData[i] = Serial.read();
      i++;
    }
    
    distancePan = atoi(incomingData);
    
    if (distancePan < -15){
      currentAnglePan++;
      currentAnglePan = constrain(currentAnglePan, 0, 180);
      panservo.write(currentAnglePan);
    }
    else if (distancePan > 15){
      currentAnglePan--;
      currentAnglePan = constrain(currentAnglePan, 0, 180);
      panservo.write (currentAnglePan);
    }
    Serial.println(currentAnglePan);
  }
  
  i = 0;
  delay(50);
  
  if (Serial.available()){
    while (i < 30){
      incomingData[i] = Serial.read();
      i++;
    }
    
    distanceTilt = atoi(incomingData);
    
    if (distanceTilt < -15){
      currentAngleTilt++;
      currentAngleTilt = constrain(currentAngleTilt, 0, 180);
      tiltservo.write(currentAngleTilt);
    }
    else if (distanceTilt > 15){
      currentAngleTilt--;
      currentAngleTilt = constrain(currentAngleTilt, 0, 180);
     tiltservo.write (currentAngleTilt);
    }
    Serial.println(currentAngleTilt);
  }
  
  i = 0;
  delay(50);
}

thank in advance ~~

  if (Serial.available()){
    while (i < 30){
      incomingData[i] = Serial.read();
      i++;
    }

If there is at least one byte available to read, read all 30 of them. Bzzzt. Wrong. Seriously wrong.

    distancePan = atoi(incomingData);

Wrong. incomingData is not a NULL terminated array of chars (also known as a string), so you can not pass it to a function that expects a string.

Another question is, why is it that the servo is moving relatively slow compare to what i see in youtube when tracking the object?? is it the type of servo ??

Or,, how the servo is powered.. How is the servo powered??

Do the double punctuation marks really add anything??

thank for the reply...

i am a new arduino user, so i dont know much about programming...sry

i changed the (i < 30) to (i < 4), i thought this is what adjust the speed T-T ~~

as for the the problem of the none accurate of either one axis is due to the incoming data...in roborealm i sent both cog_x and cog_y to arduino so maybe it cant differentiate data of which is for pan and which is for tilt...any help ??

as for the servo, is powered by connecting to the arduino 5v output...

i changed the (i < 30) to (i < 4), i thought this is what adjust the speed T-T

What else did you change?

as for the the problem of the none accurate of either one axis is due to the incoming data...in roborealm i sent both cog_x and cog_y to arduino so maybe it cant differentiate data of which is for pan and which is for tilt...any help ??

How? If you send two angles, 121 and 40, the serial stream would look like '1', '2', '1', '4', '0', unless you used some kind of separator. How is the Arduino supposed to decide if that is 12 and 140 or 121 and 40 or 1214 and 0 or 1 and 2140?

Telling half the story doesn't get you much help. We need the whole story.

as for the servo, is powered by connecting to the arduino 5v output...

Well, that's why your servo doesn't move very fast, then. It is severely underpowered. You need a separate power supply for the servo, matching the voltage expected by the servo, and capable of delivering the current that the servo needs.

that's the only thing i change,

initially the code is like this :

#include <Servo.h> // Include the servo library

Servo myServo; // Create a new servo object
char incomingData[4] = {
  0, 0, 0, 0}; // A buffer to store the ASCII value read in from the serial port
int distance = 0; // The distance of the object from the center of the screen
int currentAngle = 90; // The current angle of the servo
int i = 0; // counter

void setup(){
  Serial.begin(9600); // Open the serial port with a 9600 baud rate
  Serial.println("Serial port ready"); // Print on screen
  myServo.attach(9); // Attach the servo signal line to pin 9
  myServo.write(currentAngle); // Set the starting angle at 90 degrees
}

void loop(){
  // Wait for data to become available at the serial port
  if (Serial.available()){
    // Get the data coming through the serial port and store it in the buffer
    while (i < 4){
      incomingData = Serial.read(); // Assign the input value to the incomingData buffer
      i++; // Increment the counter
    }

    distance = atoi(incomingData); // Convert ASCII to Int

    // If the distance is negative then add 5 to the currentAngle and update the servo, and vise versa
    if (distance < -15){
      currentAngle++;
      currentAngle = constrain(currentAngle, 0, 180); // Constrain the value of currentAngle to within 0-180 degrees
      myServo.write(currentAngle);
    }
    else if (distance > 15){
      currentAngle--;
      currentAngle = constrain(currentAngle, 0, 180); // Constrain the value of currentAngle to within 0-180 degrees
      myServo.write(currentAngle);
    }
  }

  i = 0; // Reset the counter
  delay(50); // Delay 20ms to allow the servo to rotate to the position
}

Then from roborealm, I sent COG_X to arduino, so it can know the distance from centre for X, then i can adjust...here all work find ~~

but I modify it, as i need it for pan and tilt, so NOW it become like this :

#include <Servo.h>

Servo panservo;
Servo tiltservo;
char incomingData[4] = {0, 0, 0, 0};
int distancePan = 0;
int distanceTilt = 0;
int currentAnglePan = 90;
int currentAngleTilt = 20;
int i = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("Serialport ready");
  panservo.attach(9); // attaches the servo on pin 9 to the servo object
tiltservo.attach (10);   
tiltservo.write(currentAngleTilt);
  panservo.write(currentAnglePan);
} 

void loop(){
  if (Serial.available()){
    while (i < 4){
      incomingData[i] = Serial.read();
      i++;
    }
    
    distancePan = atoi(incomingData);
    
    if (distancePan < -15){
      currentAnglePan++;
      currentAnglePan = constrain(currentAnglePan, 0, 180);
      panservo.write(currentAnglePan);
    }
    else if (distancePan > 15){
      currentAnglePan--;
      currentAnglePan = constrain(currentAnglePan, 0, 180);
      panservo.write (currentAnglePan);
    }
    Serial.println(currentAnglePan);
  }
  
  i = 0;
  delay(50);
  
  if (Serial.available()){
    while (i < 4){
      incomingData[i] = Serial.read();
      i++;
    }
    
    distanceTilt = atoi(incomingData);
    
    if (distanceTilt < -15){
      currentAngleTilt++;
      currentAngleTilt = constrain(currentAngleTilt, 0, 180);
      tiltservo.write(currentAngleTilt);
    }
    else if (distanceTilt > 15){
      currentAngleTilt--;
      currentAngleTilt = constrain(currentAngleTilt, 0, 180);
     tiltservo.write (currentAngleTilt);
    }
    Serial.println(currentAngleTilt);
  }
  
  i = 0;
  delay(50);
}

in this case i need to sent both the COG_X and COG_Y to arduino to let it know the distance from centre for both x-axis and y-axis, but i felt that there is some thing missing in the code, as it seems like I am unable to specifically sent the x-axis data to the pan servo and the y-axis data to the tilt servo...the data just sent into both servo randomly....

so this is the problem I am facing now...any solution for it ??

by the ways thank for the solution for servo...^^ ~~

if (Serial.available()){
    // Get the data coming through the serial port and store it in the buffer
    while (i < 4){
      incomingData = Serial.read(); // Assign the input value to the incomingData buffer
      i++; // Increment the counter
    }

If there is one byte available to read, read all 4 of them. Don't you see that this is a problem? Changing 30 to 4 did nothing to solve the problem.

    distance = atoi(incomingData); // Convert ASCII to Int

incomingData is still not a NULL terminated character array, so it still can't be passed to atoi(). Changing 30 to 4 did nothing to address this issue.

in this case i need to sent both the COG_X and COG_Y to arduino to let it know the distance from centre for both x-axis and y-axis, but i felt that there is some thing missing in the code, as it seems like I am unable to specifically sent the x-axis data to the pan servo and the y-axis data to the tilt servo...the data just sent into both servo randomly....

This says nothing about HOW you are sending the data in roborealm. Until you tell us that, we can't help you.

I sent it through "serial" in roborealm, where there is the send sequence, i sent both the distance from centre for x and y to arduino...i connected the computer to arduino through usb (com3)...

I sent it through "serial" in roborealm

I didn't think you were having roborealm wave a pair of servos with semaphore flags...

i sent both the distance from centre for x and y to arduino

The key is HOW? As text? If so, formatted how and separated how? Or, as binary? If so, how many bytes per value?

Sry...for not being so detail in things....

i will post the picture instead...sry dont know how to insert photo...these are the links ~~

http://www.facebook.com/photo.php?fbid=2989235623938&set=a.2989222143601.105632.1653536817&type=3

http://www.facebook.com/photo.php?fbid=2989236143951&set=a.2989222143601.105632.1653536817&type=3

thx for your patience ~~

I'm sorry. The pictures do not clarify anything. I can see if there is any separator between values, of how the value is actually send (ASCII or binary).

Sorry, I am not so sure about that myself....I learn it from this link

hope it can be of some use...