Arduino Continuous Rotation Servo

Hello Everyone,

I am working on a bot for a project and have taken the portions of the robot and spread them out. Im working on the wheels so far. I have 2 continuous rotational ‘servos’ that will be powering my wheels. I am having trouble with the code for it. On my basic stamp I just used the pulsout() but it was unreliable and sketchy, on my arduino Uno rev3 that I am using, I am trying my best to get it rotating for a set amount of time. I know it is not possible to code in the degrees I want it to turn, but using the servo.Microseconds(X) I am able to delegate which direction and how fast it turns. X being 90, it stays still, anything above 90 and it turns clockwise, below goes counterclockwise.

My issue is the timing. I want to servo to turn for a set amount of time. I have tried Do loops, for loops, Do While Loops, and the such. For some reason the servo just keeps spinning.

Any suggestions would be helpful? anyone run into this issue before?

just for a quick example of one of the quick tests I tried,

#include <Servo.h> 

int time=0;
int degree;
Servo myservo;


void setup() 
{ 
  myservo.attach(9);    //Sets Pin 9 to my servo
 myservo.writeMicroseconds(90);   // Sets the servo to not spin
} 

void loop() 
{
  do
  {
    degree = 180;
    myservo.writeMicroseconds(degree);
    delay(500);
    time = time + 1;
  } while (time < 20);
  
  myservo.writeMicroseconds(90);
}

In this example, the servo just continuously rotates, it never stops and wont even do the delay that I have set. Seems like it gets stuck on the myservo.WriteMicroseconds()

I think you have two or three errors.

When you use writeMicroseconds() the parameter must be a number roughly between 1000 and 2000. You are still using degree values. 90 deg corresponds to about 1500 microsecs.

You have no delay() after the writeMicroseconds() that is after the WHILE loop so there is no time for the servo to react.

You don't reset time to 0 before the WHILE loop so the loop will only work once because after that time will always have a higher value than the test.

While it is not the cause of the problem you have no Serial.println() calls in your code to assist you to see what is going wrong.

...R

Once a servo is told to output a pulse it keeps outputing that pulse over and over until told to do something else.

So all you need is to look at blink without delay.

Mark

holmes4: So all you need is to look at blink without delay.

The OPs problem has nothing to do with delay() or blink without delay, however useful blink without delay may be. I got the code working before my reply.

...R

The op wants to have the servo do one thing and then after a given amount of time do something else. This is exactly what blink and blink without delay do.

That is a servo and not a Led is not relevant to anything.

Mark

@holmes4, have you downloaded the OPs code and modified it to make it work? I have. The problem has nothing to do with delay() in any of its variations..

...R

Ignore the code read the post. This is what the op wants.

I am trying my best to get it rotating for a set amount of time.

. theres just no point in trying to "fix" the mess of code the OP had.

Mark

If you read Reply #1 you will see that there is actually very little wrong with the code - a reasonable misunderstanding about the parameter for delayMicroseconds() and a missing delay().

...R

Some servo code that may be somewhat similar to what the OP wants.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
    delay(2000);
    servo1.write(20);
  }
}

I agree with zoomkat and holmes4 on the subject.

My autonomous robot used 4 continuous rotation servos and such commands worked like a charm. I also think 82wolfboy94 should be using simply the "servo.write" command... not servo.writeMicroseconds like zoomkat suggested.

I also think 82wolfboy94 should be using simply the “servo.write” command… not servo.writeMicroseconds like zoomkat suggested.

The below code should demonstrate the control differences between servo.write() and servo.writeMicroseconds().

// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a 
// to decrease) and enter to change servo position 
// use strings like 90x or 1500x for new servo position 
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continous rotation servo
//int pos=90;

void setup()
{
  myservo.attach(7, 400, 2600); //servo control pin, and range if desired
  Serial.begin(9600);
  Serial.println("serial servo incremental test code");
  Serial.println("type a character (s to increase or a to decrease)");
  Serial.println("and enter to change servo position");
  Serial.println("use strings like 90x or 1500x for new servo position");
  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) {
    if(readString.indexOf('x') >0) { 
      pos = readString.toInt();
    }

    if(readString =="a"){
      (pos=pos-1); //use larger numbers for larger increments
      if(pos<0) (pos=0); //prevent negative number
    }
    if (readString =="s"){
      (pos=pos+1);
    }

    if(pos >= 400) //determine servo write method
    {
      Serial.println(pos);
      myservo.writeMicroseconds(pos);
    }
    else
    {   
      Serial.println(pos);
      myservo.write(pos); 
    }
  }
  readString=""; //empty for next input
}

... not servo.writeMicroseconds like zoomkat suggested

This was worded wrong (or at least might be misunderstood). It should be...

"I also think 82wolfboy94 should be using simply the "servo.write" command, like zoomkat suggested... not servo.writeMicroseconds."

Thank you everyone for responding to this, while waiting for responses I went on and worked on the liquid display and that's working perfectly displaying text.

It was my mistake for putting 90 in the servo.writeMicroseconds() please don't get hung up on that. I understand the values. I just couldn't wrap my head around the loop portion and why it wouldn't stop. I very loosely based the code structure off zoomkat's code and I figured it out to work with my robot. Thank you guys so much!