Thanks in advance for anyone who takes the time to help me.
Ok so I have a webcam on top of a servo that pans. The webcam is running a program from Roborealm that tracks motion and sends the data to the arduino as ascii. The arduino is supposed to convert these values and then adjust the servo's position accordingly. Here is what I'm getting: roborealm is sending the proper coordinates. The servo is responding to the data, but it sweeps with random jolts. I know it is acting somehow from the information from roborealm because when I stop the serial communication in roborealm the servo stops moving. Both of my programs should work fine because I got them from a tutorial. Here is my code on the arduino side...
#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; // countervoid 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*
}
[/quote]
Thanks to anyone more knowledgeable than me (it doesn't take much ;)) who helps me