Op. Quad Encoder Program issue in EasyC

First off, I apologize if I’m posting this in the wrong section, I read the forum rules but never found a detailed explanation of the forum channels (official vs unofficial vs normal technical support?) and this is my first time using this forum.

Now to the question:

I’m going over a segment on the VEX Optical Quad Encoders using EasyC, and while in theory, it seems to be very self-explanatory, I have run into an issue with my code. The classroom assignment I’m creating is supposed to test the accuracy of different methods of autonomous driving, and I’m doing that by having the robot complete a square. In pseudo code, this resembles:

main {
     for (i = 0; i < number of squares * 4; i++) {
          doOneLeg(atSpeed, untilTicks)
     }
}
doOneLeg (int speed, int ticks) {
     startEncoder()
     presetEncoder = 0
     while (curTicks < ticks) {
          driveFWD
          curTicks = getEncoder()
     }
     stopEncoder() //This might not be needed, but I added it to try and fix the problem
     stop(500ms)
     startEncoder() //Same comment as above
     presetEncoder = 0
     curTicks = getEncoder()
     while(curTicks < ticks) {
          rotate
          curTicks = getEncoder()
     }
     stopEncoder()
     stop(500ms)
}

Aka, one “Leg” (in my program I called it a relay) is simply driving forward, then rotate (supposed to be ~90 degrees), then loop the leg four times the number of squares desired.

THE PROBLEM:
The robot does the for loop correctly, does the while loop to move forward correctly, pauses correctly, but does NOT correctly get the value of the encoder during the second while loop (to rotate). Instead, the encoder returns a value of 0 for about 30 loops, then the loop exits with a value of -1. The wheels move in the correct direction, but only for a fraction of a second (despite my change in the code to make them rotate for 4,000 ticks). -1 sounds like an error code, but I don’t know why it’s happening.

MY SETUP:
I’m using a modified SquareBot model for my class, where the rightEncoder A and B cables are plugged into Digital ports 5 and 6 respectively, and leftMotor is in port 2, rightMotor is in port3. The encoder is directly attached to the driving wheel.

I’ve attached my EasyC code, and any help would be appreciated.
EncoderTesting.zip (60.6 KB)

UPDATE:
After messing around, I discovered that I chose to use unsigned long for the desiredTicks (was supposed to for the input to always be positive) and long for the currentTicks. The problem is that when I do a comparison between long < unsigned long, it will only work if both values are positive; if I try and compare a negative value (for long) with an unsigned long, the comparison acts really weird - it passes at first, then fails after 30 loops?

One of the variables is getting cast funny, and two’s complement is weird.

You figured out the root issue, don’t worry about it.