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