Help wih encoders

while ( LeftEncoder <= 1900 && RightEncoder <= 1900 ) // Drive forward
      {
            LeftEncoder = GetQuadEncoder ( 1 , 2 ) ;
            RightEncoder = GetQuadEncoder ( 3 , 4 ) ;
            SetMotor ( 6 , 127 ) ;
            SetMotor ( 7 , 127 ) ;
            SetMotor ( 8 , -127 ) ;
            SetMotor ( 9 , -127 ) ;
      }
      while ( LeftEncoder >= 360 && RightEncoder >= -360 ) // Drive forward
      {
            LeftEncoder = GetQuadEncoder ( 1 , 2 ) ;
            RightEncoder = GetQuadEncoder ( 3 , 4 ) ;
            SetMotor ( 6 , -127 ) ;
            SetMotor ( 7 , -127 ) ;
            SetMotor ( 8 , 127 ) ;
            SetMotor ( 9 , 127 ) ;
      }

This code was supposed to go back and forth but doesnt seem to work:confused:

is this for RobotC or EasyC

I am using EasyC

What “doesnt seem to work”? What does your robot do instead of going forward and backward?

it goes forward but does not stop going backward

Here’s how I would do it:


      StartQuadEncoder ( 1 , 2 , 0 ) ;
      StartQuadEncoder ( 3 , 4 , 0 ) ;
      PresetQuadEncoder ( 1 , 2 , 0) ;
      PresetQuadEncoder ( 3 , 4 , 0) ;
      LeftEncoder = GetQuadEncoder ( 1 , 2 ) ;
      RightEncoder = GetQuadEncoder ( 3 , 4 ) ;
      while ( LeftEncoder <= 1900 && RightEncoder <= 1900 )
      {
            LeftEncoder = GetQuadEncoder ( 1 , 2 ) ;
            RightEncoder = GetQuadEncoder ( 3 , 4 ) ;
            SetMotor ( 6 , 127 ) ;
            SetMotor ( 7 , 127 ) ;
            SetMotor ( 8 , -127 ) ;
            SetMotor ( 9 , -127 ) ;
      }
      SetMotor ( 6 , 0 ) ;
      SetMotor ( 7 , 0 ) ;
      SetMotor ( 8 , 0 ) ;
      SetMotor ( 9 , 0 ) ;
      Wait ( 150 ) ;
      while ( LeftEncoder >= 360 && RightEncoder >= 360 )
      {
            LeftEncoder = GetQuadEncoder ( 1 , 2 ) ;
            RightEncoder = GetQuadEncoder ( 3 , 4 ) ;
            SetMotor ( 6 , -127 ) ;
            SetMotor ( 7 , -127 ) ;
            SetMotor ( 8 , 127 ) ;
            SetMotor ( 9 , 127 ) ;
      }
      SetMotor ( 6 , 0 ) ;
      SetMotor ( 7 , 0 ) ;
      SetMotor ( 8 , 0 ) ;
      SetMotor ( 9 , 0 ) ;

just to tell you guy this part of the autonomous is going under the trough and back to the pile of five. If I try to reduce the amount of clicks, its still the same distance travelled before i changed it. What could be the problem

I’ve never used easyC before but I’ll try to help. I believe that in RobotC, if you provide a motor with a power, it will remain at that power until changed. If EasyC works similarly, this could be a potential reason for the infinite movement backwards since the motors are never set to zero after the second while loop has been executed. GuyFromGladstone addresses that problem in his code.

Assuming that your infinite movement issue is gone, you might have problem with your second while loop condition. Currently, it uses 360 as a threshold for the LeftEncoder and -360 for the RightEncoder. Of the two, the only threshold that actually makes a difference is the one for the LeftEncoder, the -360 value is far from reached. As for your problem with no change in distance, I don’t see any code for resetting the encoders after your first while loop. This means that you’re moving a distance of ~1540 clicks backwards. Is this what you intended to do or did you want to move back 360 clicks? In that case you’ll need to reset encoders and change both thresholds to -360.

looks very nice