This is my first year coding in c++ and my teams first year using it to code a rotation sensor for autonomous
The goal is to create a loop where the rotation sensor # will increase as the robot runs, and then the motors will all stop once they reach a certain distance traveled
However, its just jumping straight to the end number its set to stop at and does not run at all(regardless of the number)
I’m not sure if they do what I think that do, but I want Currentdistance to be set to “0” each time the code starts to go to a new set distance, so that’s why I have it reset to “0” every time it runs a new number and I have the double positionleft read by the code every time it repeats in the while loop. I just initialize it in the beginning.
You are adding your distance since you started to the current distance every time.
Each time you loop, you are adding (CurrentLeft + CurrentRight)/2 to CurrentDistance. However, CurrentLeft and CurrentRight each represent how far you have driven in total since the function started (on that wheel). They are already cumulative, so the += is redundant. You should either change the += to an =, or keep track of the last position of each side every time.
Thank you for your advice. The problem with that is that when i do that, “Currentdistance” doesn’t update outside of the loop, and it will continue to read as 0 regardless of how far it actually goes. Is there a way I could code it differently so that it doesn’t do that?(I’ve been researching and I found a command called a “do while” loop. I don’t have access to my robot currently but I was wondering if that might work instead of a regular while loop?)