Standard Servo to Continuous Servo

Hello everyone,

I am new to arduino and I am having a few problems. I'm currently working with roborealm, a software that does object recognition. I have attached my webcam on a standard servo(180 degrees). The current code functions perfectly with the servo, rotating my camera from 0 to 180 degrees. The servo rotates to find the object, once it's in range it stays lock on it following the object.

I don't want to use a standard servo, I would like to use a continuous servo. The base of my design is sitting on two continuous servo motors which maneuver the robot. I would like to use these servos to stay on the object, spinning the base left or right to stay with the object. If the object stays in a certain range, the base should not move but once the object passes this range the continuous servo should rotate CW or CCW. If you need any additional information, I am glad to give it. Thank you for your help =)

/*
Arduino Roborealm Object Following

A servo motor is connected to the Arduino board. Roborealm is tracking an object and
giving its X location relative to the center of the image. If the value received is
negative then turn the servo clockwise until the value is zero. If the value is positive
then turn the servo the other way until the value received is zero.

The circuit:

  • Signal(white/yellow) wire of servo to pin 9
  • Ground(black/brown) to gnd
  • Power(red) to 5V
  • Camera mounted on top of servo

*/

#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);*
  • }*
  • // Serial.println(currentAngle);*
  • }*
  • i = 0; // Reset the counter*
  • delay(50); // Delay 20ms to allow the servo to rotate to the position*
    }

I don't want to use a standard servo, I would like to use a continuous servo.

First thing to do is get the terminology right. A standard servo that has been modified for continuous rotation is no longer a servo. It's an electric motor that can be easily speed controlled and rotated in either direction.

If you need any additional information, I am glad to give it.

What, exactly do you need help with?

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 of serial data available, read all 4 of them. I don't think that will work.

You can't use a whole array on the left side of the equal sign in front of Serial.read(). You need incomingData there.
If you are reading 4 bytes, the array needs to be able to hold 5 bytes, so that there is room for the terminating NULL, which MUST be added before the call to atoi. The atoi function stops reading the array when it finds the terminating NULL.
Once you have modified the servo to be a continuous rotation motor, you can not position it using servo.write() to set its position. You then have to use servo.writeMicroseconds() to set its speed.
* *delay(50); // Delay 20ms to allow the servo to rotate to the position* *
You are doing this even if you didn't move the servo.

One other thing to keep in mind when converting a servo to continous rotation. You lose the ablility to have the motor stop at a specifc place. Your Arduino will have no feedback mechanism to determine where the servo wheel is while it's rotating or where it might stop when you issue a PWM value that stops all rotation.

Lefty