Programming shaft encoder and servo

I’m having some problems getting the output from an optical shaft encoder to tell a servo motor to reverse direction.

#include "UserAPI.h"

int loop = 1; 
int ultrasonic; 
int optenc; 
int limitswitch1; 

void main ( void )
{
      Wait ( 100 ) ; // Wait for 1 sec
      StartUltrasonic ( 1 , 11 ) ; // interrupt 1= output label on sensor, DO11=input label on sensor
      StartEncoder ( 1 ) ;
      while ( loop == 1 )
      {
            SetMotor ( 2 , 0 ) ; // Set left motor forward
            SetMotor ( 3 , 255 ) ; // Set right motor forward
            optenc = GetEncoder ( 2 ) ; // Get optical shaft encoder value
            if ( optenc <= 11.25 ) // If optenc is less than 11.25
            {
                  SetServo ( 4 , 191 ) ; // Then turn servo counter-clockwise
            }
            else
            {
                  SetServo ( 1 , 64 ) ; // Else servo reverses direction
            }
            if ( optenc >= 22.5 ) // If optenc is greater than 22.5
            {
                  PresetEncoder ( 1 , 0 ) ; // Reset optical shaft encoder to 0
                  SetServo ( 1 , 191 ) ; // Then servo reverses direction
            }
            ultrasonic = GetUltrasonic ( 1 , 11 ) ;
            if ( limitswitch1 == 0 ) // If limit switch1 is closed
            {
                  limitswitch1 = GetDigitalInput ( 1 ) ; // Get limit switch value
                  if ( ultrasonic <= 3 ) // If Ultrasonic is less than or equal to 3 inches
                  {
                        SetMotor ( 2 , 255 ) ; // Then left motor goes backward
                        SetMotor ( 1 , 0 ) ; // Then right motor goes backward
                        Wait ( 500 ) ; // Wait .5 sec
                        SetMotor ( 2 , 0 ) ; // Then left motor goes forward
                        SetMotor ( 1 , 0 ) ; // Then right motor goes backward
                        Wait ( 1000 ) ; // Wait 1 sec
                  }
            }
            PrintToScreen ( "%d\n" , (int)optenc ) ;
      }
}

For some reason, the servo just turns all the way over counter-clockwise then stops. Using the terminal window, the PrintToScreen function just produces a complete list of 0s even when I manually move the shaft on the encoder.
Thanks for any help,
Matt

Well if it’s just printing 0’s that would seem to me that you have a bad sensor.

Well, I checked it out using the test program for teh sensor and it worked like a champ. I think the problem in seeing the 0s is the way I’ve set the PrintToScreen function, but that’s not what the problem is.
I want a servo to rotate to reciprocate back and forth and was trying to use the encoder to turn a certain direction, then reverse, then repeat.


StartEncoder ( 1 ) ;
optenc = GetEncoder ( 2 );
PresetEncoder ( 1 , 0 );
PrintToScreen ( "%d\n" , (int)optenc );

Try using the same encoder number all the time,
instead of doing Get on the encoder that hasn’t started.
Some people might suggest using a global variable for encoder number,
so you can give it a meaningful name, and easily change all references
if you decide to swap interrupt positions.