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...^^ ~~