I have been struggling with this function for a while.
I want my robot to turn left until it detects black then turn right until it hits black again. Then, I will divide the angle in half and call rotateTo to make the robot move that angle. I don't know why it is not working the way I want it to.
The reason I do that is because after I turn left or right it is not exactly 90 degrees. That is why I want to make sure it is facing the right direction before it continues moving. Here is the code:
int numberTurn = 0;
int numLeft = 0;
int numRight = 0;
RegulatedMotor Left = Motor.A;
RegulatedMotor Right = Motor.C;
while(color_id!=7){
moveForward(300, 50);
color_id = color_test.getColorID();
}
while(color_id==7){
moveForward(300, 50);
color_id = color_test.getColorID();
}
color_id = color_test.getColorID();
Left.resetTachoCount();
Right.resetTachoCount();
while(color_id!=7){
Left.rotate(100,true);
Right.rotate(-100,true);
color_id = color_test.getColorID();
}
Right.stop(true);
Left.stop(true);
numLeft += Left.getTachoCount();
while(color_id==7){
Left.rotate(-100,true);
Right.rotate(100,true);
color_id = color_test.getColorID();
}
Right.stop(true);
Left.stop(true);
numRight += Left.getTachoCount();
while(color_id!=7){
Left.rotate(-100,true);
Right.rotate(100,true);
color_id = color_test.getColorID();
}
Right.stop(true);
Left.stop(true);
numRight += Left.getTachoCount();
numberTurn = (numRight-numLeft)/2;
Left.rotateTo(-numberTurn,true);
Right.rotateTo(numberTurn,true);
while(Left.isMoving()){
}
Left and Right is motor. For color_id, 7 is black Anyone have any ideas why it doesn't work?