Oscillating Servo Problems

I’m trying to put a line tracker on an oscillating servo on the front of my squarebot. My program isn’t running the way I want it to, however.

while(1)
{
angleset == angle 1;
SetServo(3, angleset);
tracker = GetAnalogInput (2);
PrintToScreen ("%d\n", (int)tracker);
angleset == angle2;
SetServo(3, angleset);


My code goes into an infinite loop at the PrintToScreen line and just outputs the value the line tracker sends. Why doesn’t the program move to the next line? Also, is there an easier way to make the servo oscillate besides repeating the block of code for multiple angles? My goal is to find where the black line is, and if it’s off to the left slow down the left motor and if it’s off the to right slow down the right motor.

Any help would be appreciated.

Without seeing the rest of your code I can’t be sure but my guess is that you aren’t printing the right thing to the screen. Does the servo keep moving? Otherwise I’m not sure.

No the servo goes to the initial position and stops. The rest of my code just repeats the code for various angles stored as variables in angle1, angle2, etc.

Are you doing this in MpLab, EasyC, or something else.

Looking at it I can’t see anything obviously wrong.

However to shorten things up and remove a spot where you could run into an error you can get rid of “angleset == angle1;” and make “SetServo(3, angleset);” into “SetServo(3, angle1);” and so on for the rest of the code.

Off the Top of my head, I think that the while loop is running so fast, that the Servos don’t have time do anything.

Your using EasyC 2.x???

For the first test, try something like:


while(1)
{
angleset == angle1;
SetServo(3, angleset);
tracker = GetAnalogInput (2);
PrintToScreen ("%d\n", (int)tracker);
Wait(1000);

angleset == angle2;
SetServo(3, angleset);
tracker = GetAnalogInput (2);
PrintToScreen ("%d\n", (int)tracker);
Wait(1000);

angleset == angle3;
SetServo(3, angleset);
tracker = GetAnalogInput (2);
PrintToScreen ("%d\n", (int)tracker);
Wait(1000);
 
}

I like loops, so I would do something like:


#include "Main.h"

void main ( void )
{
      unsigned char position[5]; 
      unsigned char loop; 
      unsigned int tracker; 

      position[0] = 0 ; // Angle #1
      position[1] = 64 ; // Angle #2
      position[2] = 128 ; // Angle #3
      position[3] = 192 ; // Angle #4
      position[4] = 255 ; // Angle #5
      while ( 1 )
      {
            for ( loop = 0 ; loop < 5 ; loop++ )
            {
                  SetServo ( 3 , position[loop] ) ;
                  tracker = GetAnalogInput ( 2 ) ;
                  PrintToScreen ( "%d\n" , (int)tracker ) ;
                  Wait ( 1000 ) ;
            }
      }
}


If you need the Tracker Values later, store them in another Array for later.
Remember, IF you create an Array in C/C++ of say 50 Items, the Subscripts to access the Array is from 0 (Zero) to One Less than the Array Size.

I’m using EasyC. So there’s no reason why the code shouldn’t keep going?

Okay I’ll give it a try. Thanks a lot guys!

Oh one more question. Is there any way to just type the code yourself without having to drag each command onto the flowchart?

Only if you have easyC pro.

I don’t get why this isn’t working. I tried your 2nd code using the loop and it does the same thing. The servo goes all the way to the right and continuously outputs the line tracker reading with the 1 second delay between each. The servo only moves once.

More Debugging Time…

Try adding an output for the Servo Position to your Display, like:


#include "Main.h"

void main ( void )
{
      unsigned char position[5]; 
      unsigned char loop; 
      unsigned int tracker; 

      position[0] = 0 ; // Angle #1
      position[1] = 64 ; // Angle #2
      position[2] = 128 ; // Angle #3
      position[3] = 192 ; // Angle #4
      position[4] = 255 ; // Angle #5
      while ( 1 )
      {
            for ( loop = 0 ; loop < 5 ; loop++ )
            {
                  SetServo ( 3 , position[loop] ) ;
                  tracker = GetAnalogInput ( 2 ) ;
                  PrintToScreen ( "%d\n" , (int)tracker ) ;
                  PrintToScreen ( "%d\n" , (int)position[loop] ) ;
                  Wait ( 1000 ) ;
            }
      }
}

When Run, does each tracker position show a different number from the position Array??

Have you tried a different Servo, just to see if maybe you have an issue with that one Servo??

Have you tried a Motor on Port #3, just to see that it will Turn Full Speed one way (0) than Stop (127) and Full Speed the Reverse direction(255)??