Continuous Rotation Servo

Hi everyone,
I bought a continuous Rotation Servo and connected it with my Arduino. I made some code but the servo must stop when i push a button but that won't work... Can some one look at my code and help me out. Thanks and greetings Jeroen vdk

#include <Servo.h> 
 
Servo myservo;


const int buttonPin1 = 2;    
const int buttonPin2 = 3;
const int buttonPin3 = 4;



int buttonState1 = 0; 
int buttonState2 = 0; 
int buttonState3 = 0; 


 


void setup() {
  
   pinMode(buttonPin1, INPUT);     
   pinMode(buttonPin2, INPUT);   
   pinMode(buttonPin3, INPUT);  
   Serial.begin(9600);
 
}

void loop(){
  
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

//rotate left/right 
if (buttonState1 == HIGH) {     

    myservo.write(0);
    delay(400);
    Serial.println("button1");
    myservo.attach(10);
    
  } 
  //rotate left/right 
  if (buttonState2 == HIGH) {     
    
    myservo.write(180);  
    delay(400);
    Serial.println("button2");
    myservo.attach(10);
    
  } 
  //rotate stop
   if (buttonState3 == HIGH) {     
    
    myservo.write(90); 
   
    delay(400);
    Serial.println("button3");
    myservo.detach(); 
  } 
}

Do you know that 90 is the setting for stopping your servo?

Grumpy_Mike:
Do you know that 90 is the setting for stopping your servo?

Yes i know but its not working..

You should attach the servo in setup() and you should not detach it. I suspect the servo will have a mind of its own when it is detached. If you really need to detach it (though I can't see why) you might try putting a 4k7 resistor between the signal wire and ground - that stops my servo from wandering before it is attached.

Write a small sketch with no buttons that just makes the servo move and stop so that you can learn its peculiarities. You might get better control if you use servo.writeMicroseconds(). A number of about 1500 should cause it to stop, but every servo is different.

...R

In addition to the above comments, do not attempt to power the servo from the Arduino. Use a separate 5 or 6 V power supply for the servo and connect all the grounds together.

Depending on the servo and how it was made/modified, the actual stopping point can vary. Using the writeMicroseconds() is the better control format for most continuous rotation servos. The totally stopped dead band is often only about +-5us. Below is some servo test code you can try with your servo.

// zoomkat 12-25-13 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// Send an a to attach servo or d to detach servo
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h> 
String readString; //String captured from serial port
Servo myservo;  // create servo object to control a servo 
int n; //value to write to servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo all-in-one test code 12-25-13"); // so I can keep track of what is loaded
  Serial.println();
}

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

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 

      // attach or detach servo if desired
    if (readString == "d") { 
      myservo.detach(); //detach servo
      Serial.println("servo detached");
      goto bailout; //jump over writing to servo
    }
    if (readString == "a") {
      myservo.attach(7); //reattach servo to pin 7
      Serial.println("servo attached");
      goto bailout;
    }    

    n = readString.toInt();  //convert readString into a number

    // auto select appropriate value
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n); 
    }

bailout: //reenter code loop
    Serial.print("Last servo command position: ");    
    Serial.println(myservo.read());
    Serial.println();
    readString=""; //empty for next input
  }
}

Hi,

The servo is still not stopping who can help i tried all above no succes..

zoomkat:
Depending on the servo and how it was made/modified, the actual stopping point can vary. Using the writeMicroseconds() is the better control format for most continuous rotation servos. The totally stopped dead band is often only about +-5us. Below is some servo test code you can try with your servo.

// zoomkat 12-25-13 serial servo test

// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// Send an a to attach servo or d to detach servo
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually DOES NOT WORK.

#include <Servo.h>
String readString; //String captured from serial port
Servo myservo;  // create servo object to control a servo
int n; //value to write to servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo all-in-one test code 12-25-13"); // so I can keep track of what is loaded
  Serial.println();
}

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

if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string

// attach or detach servo if desired
    if (readString == "d") {
      myservo.detach(); //detach servo
      Serial.println("servo detached");
      goto bailout; //jump over writing to servo
    }
    if (readString == "a") {
      myservo.attach(7); //reattach servo to pin 7
      Serial.println("servo attached");
      goto bailout;
    }

n = readString.toInt();  //convert readString into a number

// auto select appropriate value
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

bailout: //reenter code loop
    Serial.print("Last servo command position: ");   
    Serial.println(myservo.read());
    Serial.println();
    readString=""; //empty for next input
  }
}

I tried this but at 94 he is still turning with 0,03 A my other servo stop by 96

If you use microseconds rather than degrees you will have finer control. You just need to experiment to find the actual zero point for each motor. They will be different.

Try a number that makes it go clockwise an another that makes it go counter clockwise. Then try a number midway between etc etc.

...R

Robin2:
If you use microseconds rather than degrees you will have finer control. You just need to experiment to find the actual zero point for each motor. They will be different.

Try a number that makes it go clockwise an another that makes it go counter clockwise. Then try a number midway between etc etc.

...R

Hi how do i use the microseconds?

thanks

writeMicroseconds()

Hi guys,

I fixed it thanks for your help.

Greetings Jeroen

#include <Servo.h> 
 
Servo myservo;



const int buttonPin1 = 2;    
const int buttonPin2 = 3;
const int buttonPin3 = 4;
int count = 0;


int buttonState1 = 0; 
int buttonState2 = 0; 
int buttonState3 = 0; 


 


void setup() {
  
   pinMode(buttonPin1, INPUT);     
   pinMode(buttonPin2, INPUT);   
   pinMode(buttonPin3, INPUT);  
   Serial.begin(9600);
 myservo.attach(7);
 myservo.writeMicroseconds(1508);  
}

void loop(){
  
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

if (buttonState1 == HIGH) {     

    myservo.write(0);
    delay(400);
    Serial.println("button1 naar voren");
    
    
  } 
  if (buttonState2 == HIGH) {     
    
    myservo.write(180);  
    delay(400);
    Serial.println("button2 naar achter");
    
    
  } 
    if (buttonState3 == HIGH) {     
    
    count++;
    delay(400);
    Serial.println("button3");
  } 
    if (buttonState3 == HIGH && count == 1) {     
    
    myservo.write(0); 
   
    delay(400);
    Serial.println("button3 start");
   
  } 
   if (buttonState3 == HIGH && count >= 2) {     
    
    myservo.writeMicroseconds(1508);    
    delay(400);
    Serial.println("button3 stop");
    
  } 
}

The only sure method of stopping a CR servo is to execute detach(). The servo will stop if not receiving pulses. I use CR servos on my robots. Modern digital servos may have a reliable neutral point, but the older (and cheaper!) analog servos drift with temperature and supply voltage.

Joe

If the continuous rotation servos still have pots instead of replacement resistors, the servo can be sent a 1500us signal, then carefully adjust the pot until the servo stops moving. Attach() and detach() may not work with some digital servos.