IME Preseting

My program seems to occasionally skip the PresetIntegratedMotorEncoder function. I could easily work around this, but I’m curious as to why or how it happened, the piece of code in which the PresetIntegratedMotorEncoder function skipped is more or less the following:

PresetIntegratedMotorEncoder (1, 0) ; 
PresetIntegratedMotorEncoder (2, 0) ;
PresetIntegratedMotorEncoder (3, 0) ; 
PresetIntegratedMotorEncoder (4, 0) ; 
while (Abs(GetIntegratedMotorEncoder(1)) < Distance )
{
        ...
}

Distance being a variable that holds the value of the amount of clicks that I’d like to go.
I first noticed this when the while loop wasn’t being executed, I added a PrintToScreen that displayed the encoder value just after the Preset functions and the encoder wasn’t preset to 0… at least most of the time. I solved this by adding another group of preset functions before AND after each time I use the encoders. This way, it’s harder for the program to “miss” the preset function call. I also plan to make a function that doesn’t return until the encoders are all preset.

So… why does this happen?

It sounds like the encoders aren’t catching the request for reset. Try adding a wait statement.

PresetIntegratedMotorEncoder (1, 0) ;
Wait(5);
PresetIntegratedMotorEncoder (2, 0) ;
Wait(5);
PresetIntegratedMotorEncoder (3, 0) ;
Wait(5);
PresetIntegratedMotorEncoder (4, 0) ;
Wait(5);
while (Abs(GetIntegratedMotorEncoder(1)) < Distance )
{

}

Thanks! I’ll try this out. So the encoders would be the problem here. That’s a relief, for a moment there I thought that the microcontroller was actually skipping instructions.

Please let me know if it fixes the issue and we will build a small wait into the function.