Need Help: Zumo 32u4 Encoders / IR Obstacle Avoidance Routine

I’m trying to use the encoders to run a simple autonomous routine. In this case it is a 2ft x 2ft square. Now i am trying to integrate an IR avoidance routine while it is doing this square. That part seems to work. What i want to do now is take the encoder variables when it first enters the avoid routine and when it ends the routine subtract whatever the distance it used while in the avoid routine. I wrote some code to do this, but can not get it to work properly.

What i thought i needed to do was to change the local encoder variables from the driving forward function i am using and change them to global variables. The problem is when i do that it screws up my forward routine in an unexpected way.

Normally it goes
forward
left
forward
left
forward
left
forward

when i try to change it to a global variable it goes
forward
left
left
left

and when i do not push the reset button it goes
left
left
left

I am under the impression that either i did not set the global variable correctly or for some reason the encoders are not getting reset and when it goes to drive forward it thinks it has already drove that distance and so it turns instead and just repeats that. I’m confused why the encoder values might not be getting reset if that is the case. I’ve tried changing it over and over and i am getting frustrated.

If anyone is familiar with how to really take advantage of the zumo encoders and are equally good at arduino programming perhaps someone can take a look at my code and suggest a fix?

Thanks,
-AndrewObjectAvoidStateMachine.ino (11.4 KB)

Hello.

I skimmed through your code, and I would suggest adding some Serial print commands for debugging and running your robot tethered to your computer so you can see what is going on better. If you do that and still cannot figure it out where the error is, try simplifying your code to the simplest thing you think should work, but does not, and post that.

- Patrick