# Count wheel encoders

Hello everybody,
I have a little robot(http://arduino.cc/forum/index.php/topic,92533.0.html) it has two driving wheels and third “lazy” metal ball/wheel. All so it has wheel encoders on each wheel. I want that the robot drives forward one meter then turns 90 degrees left, drives 50cm forward and then turns 90 degrees left, and drives one meter forward, and turns 90 degrees left, drives 50cm forward and stops. And then turn around 180 degrees and the whole route/track back. So the rout will be something like rectangle(1m*0.5m).

When the robot goes straight forward 10cm, left and right wheel encoders counts 20 times. So if distance = 10cm, left and right wheel counter will show = 20;

When I turn the robot 90 degrees, left wheel counter will show = -4; and right wheel counter will show = 4; (Before turning 90degrees left, left and right counters were 0).

To drive the robot I have those loops:
forward();
stop();
left();
right();

What you think how should look like the code to do this mission?
The first line should be something like this or not?..:
if(left wheel counter <= 200 && right wheel counter <= 200)
forward();
but what should come now?..

P.S. I don’t want to use delays.

so im guessing you have the encoders writing changing some "int"s as they turn? if you dont want to do anyting else while using the encoders, just do something like this:

``````void GoFWD(int ticks)
{
RencoderVal = 0; //reset encoder value to 0 so we can count how many ticks we want to move
while(ticks > RencoderVal)
{
//go forward both motors
}
//stop motors here
}

void GoREV(int ticks)
{
while(ticks < RencoderVal)
{
//go reverse both motors
}
//stop motors here
}

void GoRight(int ticks) //right wheel doesnt move, only left going FWD( which makes robot go right)
{
while(ticks > LencoderVal)
{
//go FWD left motor
}
//stop motor here
}
``````

now to do both encoders at once...(just fwd and rev)

``````void Move(int ticks)
{
//reset position found booleans
LposFound = false; //...
RposFound = false; //...

//750 ticks is no where in my robot, would never want to move that slow, so i must have put in a value in  inches
if(MoveVal < 750)  //if its less than 750, in inches
{
CurrentTicks = ReturnTicks(MoveVal); //convert inches to ticks
}
else //greater than 750, in ticks
{
CurrentTicks = MoveVal; //set input to ticks
}

while(LposFound == false || RposFound == false) //repeat till both are found
{
{
if(LencVal >= CurrentTicks) //need to go back
{
//rev left motor
}
else //pos found
{
//stop left motor
LposFound = true; //left pos found
}

if(LencVal <= CurrentTicks && LposFound == false) //need to go forward
{
//fwd left
}
else //pos found
{
//stop left motor
LposFound = true; //left pos found
}
}

{
//if enc val is greater than or = to than wanted pos
if(RencVal >= CurrentTicks) //need to go reverse
{
// rev right motor
}
else //pos found
{
//stop right motor
RposFound = true; //right pos found
}

//if enc val is less than or = to than wanted pos and pos has not already been found
if(LencVal <= CurrentTicks && RposFound == false)
{
//fwd rmotor
}
else //pos found
{
//stop right motor
RposFound = true; //right pos found
}
}
} //end of while loop

//once pos is reached, stop motors
}

int ReturnTicks(int Inches) // returns a int value
{
return (Inches * 11.388); //converts inches to ticks for encoders
}
``````