Controlling Continuous Rotation Servo

So today I began my negative feedback control using a rotary encoder and a continuous rotation servo.

Encoder Spec Sheet: http://www.mouser.com/ds/2/54/EMS22P-76098.pdf

The encoder reads between 0 and 1023

For the most part I have the system working. However, I cannot get the servo to stop when the rotary encoder reads between a certain value specified in the sketch.

Can someone look at the sketch and tell me why? The servo does not even slow down during that period.

Right now the servo moves clockwise between 0 and 512, and counterclockwise between 512 and 1024.

I want it to stop between 973 and 50.

Here is the sketch

#include <Servo.h>

Servo myservo; //create servo object to control a servo

int pos=0; // variable to store the servo position
int encoderValue; 

void readEncoder()
{
  encoderValue=analogRead(A0);
} 

void setup()
{
  Serial.begin(9600);
  myservo.attach(9); //attaches the servo on pin 9 to the servo object
}


void loop()
{
  // read input
  readEncoder();
  
  //processing
  if (encoderValue > 50 && encoderValue < 512)
  {
    //send 120 degrees to servo to get it moving clockwise
    myservo.write(120);
    if(encoderValue < 50 && encoderValue > 973) myservo.write(90); //if encoder reads between 974 and 50, the servo will stop
  }
  else if (encoderValue > 512 && encoderValue < 973)
  {
    //send 60 degrees to servo to get it moving counterclockwise
    myservo.write(60);
    if(encoderValue < 50 && encoderValue > 973) myservo.write(90);
  }
  //output
  Serial.println(encoderValue);
  delay(1);  //delay in between reads for stability
}
  /code]

Thanks all for your time.
if(encoderValue < 50 && encoderValue > 973) myservo.write(90);

Take a good look at this line. Tell me what number would possibly satisfy it.

PriceyTrash: Right now the servo moves clockwise between 0 and 512, and counterclockwise between 512 and 1024.

I want it to stop between 973 and 50.

Let me rephrase what you just said here and see if that helps you a little.

You want it to move clockwise between 0 and 49, not move between 50 and 973, and move counterclockwise between 974 and 1023.

How about this pseudocode:

if (position < 50) {
    
      // move clockwise
}

else if(position > 973) {
     
     // move counterclockwise
}

else {
    
     //  don't move
}

Delta_G:
Let me rephrase what you just said here and see if that helps you a little.

You want it to move clockwise between 0 and 49, not move between 50 and 973, and move counterclockwise between 974 and 1023.

I don’t understand this?

I want the servo moving clockwise between 50 and 512, and moving counterclockwise between 512 and 973. Between 973 and 50 I want the servo to stop.

I specified that during testing it moves clockwise between 0 and 512 and counterclockwise between 512 and 1023.

Delta_G:
How about this pseudocode:

if (position < 50) {

// move clockwise
}

else if(position > 973) {
   
    // move counterclockwise
}

else {
   
    //  don’t move
}

Wouldn’t this code cause the servo to be confused with which way to spin? Isn’t the code saying values between 50 and 973 can go both clockwise and counterclockwise?

No, it has an else clause that says for values that don't fit either the condtion of less than 50 or greater than 973 that it should stop.

OK, so I guess I misunderstood your intention.

S0 you want this:

Less than 50 - stop.

Between 50 and 512 - move clockwise

Between 512 and 973 - move counterclockwise

Greater than 973 - stop

So how about this?

if (position < 50) {
    
      // stop
}

else if(position < 512) {   // 50 or greater but less than 512
     
     // move clockwise
}

else if(position < 973) {  // 512 or greater but less than 973
    
     //  move counterclockwise
}

else {           // 974 and greater
    // stop
}

It did not work. The servo would not even change direction this time. Using what you recommended, here is the sketch.

#include <Servo.h>

Servo myservo; //create servo object to control a servo

int pos=0; // variable to store the servo position
int encoderValue; 

void readEncoder()
{
  encoderValue=analogRead(A0);
} 

void setup()
{
  Serial.begin(9600);
  myservo.attach(9); //attaches the servo on pin 9 to the servo object
}


void loop()
{
  // read input
  readEncoder();
  
  //processing
  if (encoderValue < 50 )
  {
    //stop servo
    myservo.write(90);
    
  }
  else if (encoderValue < 512 )
  {
    //send 120 degrees to servo to get it moving counterclockwise
    myservo.write(120);
  }
  else if (encoderValue < 973) {
    
    myservo.write(60);
  }
  else (encoderValue > 974); {
    
    myservo.write(90);
  }
  //output
  Serial.println(encoderValue);
  delay(1);  //delay in between reads for stability
}
  /code]
else (encoderValue > 974); {

I know I didn't write an else like that? What's the part in the parenthesis doing?

You are right, my mistake. Thank you for the help!

It works now, but whenever the encoder reads across 0, the servo slightly twitches. For example, the encoder reads 30. I spin the encoder so it reads down to 20, 10, 0, and 1020. Once it crosses 0 it twitches. Is there anyway to stop this?

Thanks again.

And if you can, what was wrong with my first code?

  else (encoderValue > 974); {
    
    myservo.write(90);
  }

Why is there a semicolon there? You are unconditionally writing 90 to the servo on every pass through loop().