Troubles with servos !

Hi,

We've got a Final Cursus project. So we decided to use an Arduino. We have a Arduino Uno, a L298N motor shield and a sensor shield. So, the sensors work perfectly, but we can't control the Servo.It does everything it wants. So, we want him to run until the sensor see something. But it doesn't obey the Servo.write(X) command.

Have you got any idea ?

Post your code, post your schematic - hard to help much without them

Have you got any idea ?

Take the servo aside, and give it a stern talking to.

Or, post some code, a link to the servo, and some details about how it is powered and how it is connected to the Arduino.

You don’t use an L298N motor controller with an RC hobby servo. How do you have the servo connected? Usually it has three wires: black for ground, red for +5-6V and a white or yellow wire that connects directly to an Arduino data pin. The servo.attach(pin) function tells the library what pin you connected the control wire to.

The basic way your servo should be connected to your arduino.

Here’s the code used :

#include "Ultrasonic.h"
#include <Servo.h>

Ultrasonic Ultra1(12, 13); Ultrasonic Ultra2(11, 10); Ultrasonic Ultra3(7, 6);
Servo Motor1;

void setup() {
  Serial.begin(9600);
  Serial.println("");
  Serial.println("Init...");
  Motor1.attach(9);
  Serial.println("U1\tU2\tU3");
  Motor1.write(40);  
}
void loop() {
  Serial.print(Ultra1.Ranging(CM)); Serial.print("\t");
  delay(100);  
  Serial.print(Ultra2.Ranging(CM)); Serial.print("\t");
  delay(100);  
  Serial.print(Ultra3.Ranging(CM)); Serial.println("");
  delay(500);
}

Servo used : http://arduino-direct.com/sunshop/index.php?l=product_detail&p=128
Ultrasonic’s are on 13 to 10 and 7, 6.
The servo is on the pin 9.
When we use motor1.write(40); the servo works but it doesn’t do what we ask him to, i mean he do not stop after 40º.

note : we tried the “sweep” example and the motor doesn’t stop after 180º. (it makes 160º ~ 300º, then reversing).

Thanks for your help and dismiss our English.

That servo is kinda malfunctioning. It should actually continue around and around. It is a continuous rotation servo. Check the link.

Edit: My bad. It is not malfunctioning. The sweep program goes one way for a while, then the other. Probably just long enough to get almost all the way around before reversing direction.

With continuous rotation servos, which really are not servos, you use Servo.writeMicroseconds() to control them. A value around 1500 will make the servo speed 0. Larger values will cause the servo to rotate in one direction - the larger the value, the faster the rotation. Smaller values will cause the servo to rotate in the other direction - the smaller the value, the faster the rotation.

You will need to experiment to find the value that causes your servo to stop, and to find the maximum speed in each direction (typically, around 2200 defines top speed one way).

Simple servo test code for the serial monitor you can use to find the servo neutral point and the points for max directional speed.

// zoomkat 10-14-11 serial servo test
// use a microseconds value like 1500 in serial monitor
// for IDE 0022 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  // allow buffer to fill with next character
    }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    myservo.writeMicroseconds(readString.toInt()); //convert readString to number for servo
    readString=""; //empty for next input
  } 
}

Thanks everybody !

We maked test and found the good value for them to stop

Thanks

You can lock

Just for future info, if you start having trouble with the servos starting to drift and starting to slowly turn at a neutral point, you can detach the servos when you want them to stop turning like below.

// zoomkat 10-16-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use a value <0500 to detach the servo

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.writeMicroseconds(2000); //set initial servo position if desired
  myservo2.writeMicroseconds(1000); //set initial servo position if desired
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("servo-test-22_detach2"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString); //see what was received

    // expect a string like 07002100 containing the two servo positions      
    servo1 = readString.substring(0, 4); //get the first four characters
    servo2 = readString.substring(4, 8); //get the next four characters 

    Serial.println(servo1);  //print to serial monitor to see results
    Serial.println(servo2);

    int n1 = servo1.toInt();
    int n2 = servo2.toInt();

    if (n1 < 500) { // allows for sending a low value to detach servo
      myservo1.detach(); 
    }
    else {
      myservo1.attach(6);
      myservo1.writeMicroseconds(n1); //convert readString to number for servo

      if (n2 < 500) {
        myservo2.detach(); 
      }
      else {
        myservo2.attach(7);
        myservo2.writeMicroseconds(n2); //convert readString to number for servo
      }
      readString="";
    } 
  }
}