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*
}